description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
d = collections.Counter(A)
if 0 in d:
if d[0] & 1 == 1:
return False
d.pop(0)
keys = sorted(d.keys())
for i in keys:
if i in d:
if i < 0:
if i / 2 not in d:
return False
if d[i] > d[i / 2]:
return False
d[i / 2] -= d[i]
if d[i / 2] == 0:
d.pop(i / 2)
d.pop(i)
else:
if i * 2 not in d:
return False
if d[i] > d[i * 2]:
return False
d[i * 2] -= d[i]
if d[i * 2] == 0:
d.pop(i * 2)
d.pop(i)
return True | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF NUMBER VAR IF BIN_OP VAR NUMBER NUMBER NUMBER RETURN NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER VAR RETURN NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR RETURN NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
cnt = collections.Counter(A)
for k in sorted(cnt, key=abs):
a = cnt[k]
if cnt[k] == 0:
continue
b = cnt.get(2 * k, 0)
if b < a:
return False
cnt[2 * k] -= a
return True | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER VAR BIN_OP NUMBER VAR VAR RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
A = [abs(num) for num in A]
A.sort()
dic = collections.defaultdict(collections.deque)
for i, num in enumerate(A):
dic[num].append(i)
visited = [0] * len(A)
cnt = 0
i = 0
while i < len(A):
if visited[i]:
i += 1
continue
val, val_double = A[i], 2 * A[i]
if val_double in dic and dic[val_double]:
if val not in dic or not dic[val]:
return False
dic[val].popleft()
if not dic[val_double]:
return False
index = dic[val_double].popleft()
visited[index] = 1
else:
return False
i += 1
return True | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP NUMBER VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
counts = defaultdict(int)
for x in sorted(A):
counts[x] += 1
if x < 0 and x * 2 in counts:
while counts[x] > 0 and counts[x * 2] > 0:
counts[x] -= 1
counts[x * 2] -= 1
if x >= 0 and x / 2 in counts:
while counts[x] > 0 and counts[x / 2] > 0:
counts[x] -= 1
counts[x / 2] -= 1
return sum(c for v, c in counts.items()) == 0 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER VAR WHILE VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER BIN_OP VAR NUMBER VAR WHILE VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
pos_set = set()
neg_set = set()
pos_dict = {}
neg_dict = {}
pos_count = 0
for num in A:
if num >= 0:
pos_set.add(num)
pos_dict[num] = 1 + pos_dict.get(num, 0)
pos_count += 1
else:
neg_set.add(abs(num))
neg_dict[abs(num)] = 1 + neg_dict.get(abs(num), 0)
if pos_count % 2 != 0:
return False
else:
return self.helper(pos_set, pos_dict) and self.helper(neg_set, neg_dict)
def helper(self, set_, dict_):
sorted_ = sorted(list(set_))
for num in sorted_:
if num * 2 in sorted_ and num != 0:
small_ = dict_[num]
large_ = dict_[num * 2]
usage = min(small_, large_)
dict_[num] -= usage
dict_[num * 2] -= usage
elif num == 0:
if dict_[0] % 2 != 0:
return False
else:
dict_[0] = 0
for key in dict_:
if dict_[key] != 0:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
count = {}
zero_count = 0
for num in A:
if num == 0:
zero_count += 1
elif num in count:
count[num] += 1
else:
count[num] = 1
if zero_count % 2 != 0:
return False
A.sort()
for num in A:
double = num * 2
if num in count and double in count:
count[double] -= 1
count[num] -= 1
if count[double] == 0:
count.pop(double)
if count[num] == 0:
count.pop(num)
return count == {} | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR DICT VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
counter = collections.Counter(A)
counter = dict(sorted(counter.items(), key=lambda x: abs(x[0])))
nums = []
for num in counter.keys():
for _ in range(counter[num]):
nums.append(num)
for num in nums:
if counter[num]:
if 2 * num in counter and counter[2 * num] != 0:
counter[num] -= 1
counter[num * 2] -= 1
else:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR IF BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
counter = Counter(A)
for a in sorted(A, key=abs):
if counter[a] == 0:
continue
if counter[2 * a] == 0:
return False
counter[a] -= 1
counter[2 * a] -= 1
return True | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER IF VAR BIN_OP NUMBER VAR NUMBER RETURN NUMBER VAR VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
c = collections.Counter(A)
for x in sorted(c):
if c[x] == 0:
continue
if c[x] < 0:
return False
if x == 0:
if c[x] % 2 != 0:
return False
else:
continue
if x > 0:
temp = 2 * x
elif x % 2:
return False
else:
temp = x // 2
if c[temp] < c[x]:
return False
else:
c[temp] -= c[x]
return True | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER VAR VAR VAR VAR RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
d = {}
for i in A:
if i in d:
d[i] += 1
else:
d[i] = 1
A.sort()
if 0 in d:
if d[0] % 2 != 0:
return False
d.pop(0)
for i in A:
if i not in d:
continue
if i * 2 in d:
if d[i] > d[i * 2]:
d[i] -= d[i * 2]
d[i * 2] = 0
else:
d[i * 2] -= d[i]
d[i] = 0
if d[i] == 0:
d.pop(i)
if d[i * 2] == 0:
d.pop(i * 2)
elif i % 2 == 0 and i // 2 in d:
if d[i] > d[i // 2]:
d[i] -= d[i // 2]
d[i // 2] = 0
else:
d[i // 2] -= d[i]
d[i] = 0
if d[i] == 0:
d.pop(i)
if d[i // 2] == 0:
d.pop(i // 2)
else:
return False
return len(d) == 0 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR IF NUMBER VAR IF BIN_OP VAR NUMBER NUMBER NUMBER RETURN NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR VAR IF BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
def factorize(num):
power = 0
while num % 2 == 0:
num, power = num // 2, power + 1
return num, power
counts = defaultdict(lambda: [0] * 17)
zeros = 0
for a in A:
if a == 0:
zeros += 1
else:
n, p = factorize(a)
counts[n][p] += 1
if zeros % 2 == 1:
return False
for key in counts:
carry = 0
for p in counts[key]:
carry = p - carry
if carry < 0:
return False
if carry != 0:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
st = {}
A = sorted(A, reverse=True)
for i in A:
if i in st:
st[i] += 1
else:
st[i] = 1
for num in A:
if num * 2 in st and st[num] > 0 and st[num * 2] > 0:
st[num * 2] -= 1
st[num] -= 1
for num in st:
if st[num] > 0:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
seen = {}
A.sort()
for i in A:
if i / 2 in seen:
seen[i / 2] -= 1
if seen[i / 2] == 0:
del seen[i / 2]
elif 2 * i in seen:
seen[2 * i] -= 1
if seen[2 * i] == 0:
del seen[2 * i]
elif i in seen:
seen[i] += 1
else:
seen[i] = 1
return not bool(seen) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT EXPR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR NUMBER IF VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
table = collections.defaultdict(int)
for num in A:
table[num] += 1
A.sort()
for num in A:
if table[num] == 0:
continue
pair = num * 2
if num < 0:
pair = num / 2
if table[pair] < 1:
return False
table[num] -= 1
table[pair] -= 1
return True | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER RETURN NUMBER VAR VAR NUMBER VAR VAR NUMBER RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
neg = dict()
pos = dict()
zero = 0
for n in A:
if n < 0:
if -n not in neg:
neg[-n] = 0
neg[-n] += 1
elif n > 0:
if n not in pos:
pos[n] = 0
pos[n] += 1
else:
zero += 1
if zero % 2 != 0:
return False
def helper(nums):
num_sorted = sorted(list(nums.keys()), reverse=True)
while num_sorted:
a = num_sorted.pop()
if nums[a] == 0:
continue
if 2 * a not in nums or nums[a] > nums[2 * a]:
return False
nums[2 * a] -= nums[a]
return True
return helper(pos) and helper(neg) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER IF BIN_OP NUMBER VAR VAR VAR VAR VAR BIN_OP NUMBER VAR RETURN NUMBER VAR BIN_OP NUMBER VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
A.sort()
N = len(A)
count = collections.Counter(A)
for i in range(N):
if A[i] == 0 or A[i] not in count:
continue
elif A[i] < 0:
if A[i] % 2 == 1 or count[A[i] / 2] == 0:
return False
else:
count[A[i] / 2] -= count[A[i]]
if count[A[i] / 2] == 0:
del count[A[i] / 2]
del count[A[i]]
elif count[A[i] * 2] == 0:
return False
else:
count[A[i] * 2] -= count[A[i]]
if count[A[i] * 2] == 0:
del count[A[i] * 2]
del count[A[i]]
return True | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER RETURN NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER NUMBER RETURN NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR VAR RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
freq = dict()
for i in A:
freq[i] = freq.get(i, 0) + 1
for k in sorted(list(freq.keys()), key=lambda x: abs(x)):
if freq[k] == 0:
continue
if freq[k] > freq.get(2 * k, 0):
return False
freq[2 * k] -= freq[k]
return True | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER RETURN NUMBER VAR BIN_OP NUMBER VAR VAR VAR RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
A.sort()
map = {}
for item in A:
if item in map:
map[item] += 1
else:
map[item] = 1
for item in A:
if map[item] == 0:
continue
if item < 0:
if item / 2 in map:
if map[item / 2] == 0:
return False
map[item] -= 1
map[item / 2] -= 1
else:
return False
elif item * 2 in map:
if map[2 * item] == 0:
return False
map[item] -= 1
map[item * 2] -= 1
else:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER VAR IF VAR BIN_OP NUMBER VAR NUMBER RETURN NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
D = {}
for x in A:
D[x] = D.get(x, 0) + 1
D = dict([x for x in sorted(list(D.items()), key=lambda x: x[0])])
for x in D:
while D[x] > 0:
D[x] -= 1
if x <= 0:
pair_x = x / 2
else:
pair_x = x * 2
if D.get(pair_x, 0) > 0:
D[pair_x] -= 1
else:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR WHILE VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER RETURN NUMBER RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
A.sort()
seen = Counter()
for num in A:
if num > 0 and num % 2:
seen[num] += 1
continue
elem = 2 * num if num < 0 else num // 2
if seen[elem] > 0:
seen[elem] -= 1
else:
seen[num] += 1
if sum(seen.values()):
return False
return True | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR RETURN NUMBER RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
seen = dict()
for x in A:
if x not in seen:
seen[x] = 0
seen[x] += 1
for x in sorted(seen, key=lambda x: x**2):
if seen[x] == 0:
continue
if 2 * x not in seen or seen[x] > seen[2 * x]:
return False
else:
seen[2 * x] -= seen[x]
return True | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER IF BIN_OP NUMBER VAR VAR VAR VAR VAR BIN_OP NUMBER VAR RETURN NUMBER VAR BIN_OP NUMBER VAR VAR VAR RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
n = len(A)
neg = sorted(list([x for x in A if x < 0]))
pos = sorted(list([x for x in A if x > 0]), reverse=True)
nl, pl = len(neg), len(pos)
if nl % 2 or pl % 2:
return False
d1 = collections.Counter(neg)
cnt = 0
for i in range(nl):
if d1[2 * neg[i]] > 0:
d1[2 * neg[i]] -= 1
d1[neg[i]] -= 1
cnt += 1
if cnt < nl // 2:
return False
d2 = collections.Counter(pos)
cnt = 0
for i in range(pl):
if d2[2 * pos[i]] > 0:
print((pos[i] * 2, pos[i]))
d2[2 * pos[i]] -= 1
d2[pos[i]] -= 1
cnt += 1
if cnt < pl // 2:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
if len(A) == 0:
return True
mydict = {}
for _ in range(0, len(A)):
if A[_] in mydict:
mydict[A[_]] += 1
else:
mydict[A[_]] = 1
A_sorted = sorted(A)
not_valid = set()
for num in A_sorted:
if len(mydict) == 0:
return True
if num in not_valid:
continue
if 2 * num in mydict and num in mydict:
double = 2 * num
mydict[num] -= 1
mydict[double] -= 1
if mydict[num] == 0:
mydict.pop(num)
not_valid.add(num)
if num == double:
continue
elif mydict[2 * num] == 0:
mydict.pop(2 * num)
not_valid.add(2 * num)
elif num / 2 in mydict and num in mydict:
half = num / 2
mydict[num] -= 1
mydict[num / 2] -= 1
if mydict[num] == 0:
mydict.pop(num)
not_valid.add(num)
if mydict[num / 2] == 0:
mydict.pop(num / 2)
not_valid.add(num / 2)
else:
return False | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR VAR IF BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR IF VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
counter = collections.Counter(A)
for e in sorted(A, key=abs):
if 2 * e in counter and counter[2 * e] > 0 and counter[e] > 0:
counter[e] -= 1
counter[2 * e] -= 1
print(counter)
return sum(counter.values()) == 0 | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
A.sort()
freq = dict()
for i in A:
freq[i] = freq.get(i, 0) + 1
for i in A:
if freq.get(i, 0) == 0:
continue
if i < 0:
if i % 2 != 0:
return False
elif not freq.get(i // 2, 0) > 0:
return False
elif i > 0 and not freq.get(i * 2, 0) > 0:
return False
if i < 0:
freq[i // 2] -= 1
else:
freq[i * 2] -= 1
freq[i] -= 1
return True | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER RETURN NUMBER IF VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER RETURN NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
hashtable = {}
zero_count = 0
for i in range(len(A)):
if A[i] == 0:
zero_count += 1
continue
if A[i] in list(hashtable.keys()):
hashtable[A[i]] += 1
else:
hashtable[A[i]] = 1
if zero_count % 2 == 1:
return False
else:
key_list = []
pair_count = 0
for key in list(hashtable.keys()):
key_list.append(key)
key_list.sort()
for key in key_list:
if key % 2 == 0 and key // 2 in list(hashtable.keys()):
m = min(hashtable[key], hashtable[key // 2])
hashtable[key] -= m
hashtable[key // 2] -= m
pair_count += m
if pair_count * 2 + zero_count == len(A):
return True
else:
return False | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
A.sort()
B = OrderedDict()
for x in A:
if x in B:
B[x] += 1
else:
B[x] = 1
while B:
x = next(iter(B))
freq = B.pop(x)
if x < 0:
if x % 2:
return False
if x // 2 not in B or B[x // 2] < freq:
return False
B[x // 2] -= freq
if B[x // 2] == 0:
B.pop(x // 2)
elif x == 0:
if freq % 2:
return False
else:
if x * 2 not in B or B[x * 2] < freq:
return False
B[x * 2] -= freq
if B[x * 2] == 0:
B.pop(x * 2)
return True | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, arr):
arr.sort()
negl = [x for x in arr if x < 0][::-1]
posl = [x for x in arr if x >= 0]
cnt = {}
for x in arr:
if x not in cnt:
cnt[x] = 1
else:
cnt[x] = cnt[x] + 1
while True:
x = None
if negl != []:
x = negl.pop(0)
elif posl != []:
x = posl.pop(0)
else:
break
if cnt[x] == 0:
continue
y = x * 2
if y not in cnt:
return False
elif cnt[y] > 0 and cnt[x] > 0:
cnt[y] = cnt[y] - 1
cnt[x] = cnt[x] - 1
else:
return False
return True | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER WHILE NUMBER ASSIGN VAR NONE IF VAR LIST ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR LIST ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
A.sort(key=lambda x: (x > 0, abs(x)))
right = 1
size = len(A)
for i in range(size):
if A[i] is None:
continue
while right < size and 2 * A[i] != A[right]:
right += 1
if right == size:
return False
A[right] = None
return True | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NONE WHILE VAR VAR BIN_OP NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR NONE RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
c = Counter(A)
for n in sorted(list(c.keys()), key=abs):
while c[n] > 0 and c[(double := 2 * n)] > 0:
c[n] -= 1
c[double] -= 1
return all(not v for v in list(c.values())) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR VAR BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
D1 = collections.Counter()
D2 = collections.Counter()
zero = 0
for a in A:
if a < 0:
D1[a] += 1
else:
D2[a] += 1
for d in sorted(D1, reverse=1):
if D1[d]:
if D1[d * 2] < D1[d]:
return False
D1[d * 2] -= D1[d]
for d in sorted(D2):
if D2[d]:
if D2[d * 2] < D2[d]:
return False
D2[d * 2] -= D2[d]
return True | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, arr: List[int]) -> bool:
dicti = dict()
for var in arr:
if var in list(dicti.keys()):
dicti[var] += 1
else:
dicti[var] = 1
arr.sort(key=lambda x: abs(x))
for var in arr:
if dicti[var] == 0:
continue
if 2 * var not in list(dicti.keys()):
return False
if dicti[2 * var] <= 0:
return False
dicti[var] -= 1
dicti[2 * var] -= 1
return True | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR NUMBER IF BIN_OP NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR RETURN NUMBER IF VAR BIN_OP NUMBER VAR NUMBER RETURN NUMBER VAR VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
def update_map(count, small, large):
small_count = count[small]
large_count = count[large]
min_count = min(small_count, large_count)
count[small] += -min_count
if small != large:
count[large] += -min_count
if not A:
return True
element_count = {}
ordered_elements = list(set(A))
ordered_elements.sort()
for number in A:
if number not in element_count:
element_count[number] = 0
element_count[number] += 1
for number in ordered_elements:
number_count = element_count[number]
double_number = number * 2
if double_number in element_count:
update_map(element_count, number, double_number)
for number in element_count:
if element_count[number] != 0:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
A.sort()
d = collections.defaultdict(int)
for a in A:
if d[2 * a] > 0:
d[2 * a] -= 1
elif d[a / 2] > 0:
d[a / 2] -= 1
else:
d[a] += 1
return not any(d.values()) | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
cache = dict()
for a in A:
cache[a] = cache.get(a, 0) + 1
while len(cache) > 0:
temp = [abs(k) for k in cache.keys()]
curr = min(temp)
if curr == 0:
if cache[0] % 2 > 0:
return False
else:
cache.pop(0)
else:
if curr in cache:
if cache[curr] == 0:
cache.pop(curr)
elif 2 * curr not in cache or cache[2 * curr] < cache[curr]:
return False
else:
cache[2 * curr] -= cache[curr]
cache.pop(curr)
if -curr in cache:
if cache[-curr] == 0:
cache.pop(-curr)
elif -2 * curr not in cache or cache[-2 * curr] < cache[-curr]:
return False
else:
cache[-2 * curr] -= cache[-curr]
cache.pop(-curr)
return True | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER RETURN NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR VAR RETURN NUMBER VAR BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR VAR RETURN NUMBER VAR BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
B = []
for x in A:
if x > 0:
B.append(x)
else:
B.append(-x)
count = collections.Counter(B)
for x in sorted(B):
if count[x] == 0:
continue
if count[2 * x] == 0:
return False
else:
count[x] -= 1
count[2 * x] -= 1
return True | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR BIN_OP NUMBER VAR NUMBER RETURN NUMBER VAR VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
A.sort(key=lambda x: abs(x))
c = {}
for v in A:
if v / 2 in c:
c[v / 2] -= 1
if not c[v / 2]:
del c[v / 2]
else:
if v not in c:
c[v] = 0
c[v] += 1
return len(c) == 0 | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
if not A:
return True
d = defaultdict(int)
for x in A:
d[x] += 1
n = len(A) // 2
A.sort()
for x in A:
if d[x] > 0:
if 2 * x in d and d[2 * x] > 0:
d[2 * x] -= 1
d[x] -= 1
n -= 1
if n == 0:
return True
return False | CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR NUMBER IF BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
A = sorted(A, key=lambda x: abs(x))
M = Counter()
for e in A:
M[e] += 1
for e in A:
if M[e] and M[2 * e]:
M[2 * e] -= 1
M[e] -= 1
elif M[e]:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
if len(A) % 2 == 1:
return False
count = collections.defaultdict(int)
for num in A:
count[num] += 1
A.sort()
for num in A:
if count[num] > 0:
double = num * 2
if double in count and count[double] > 0:
count[num] -= 1
count[double] -= 1
for num in count:
if count[num] != 0:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def get_result(self, sl, d):
for k in sl:
if d[k] == 0:
continue
if 2 * k not in d:
return False
v = d[k]
v2 = d[2 * k]
if v > v2:
return False
d[k] -= v
d[2 * k] -= v
return True
def canReorderDoubled(self, A: List[int]) -> bool:
d = dict()
num_zeros = 0
for a in A:
if a == 0:
num_zeros += 1
continue
if a in d:
d[a] += 1
else:
d[a] = 1
neg = [k for k in list(d.keys()) if k < 0]
pos = [k for k in list(d.keys()) if k > 0]
if num_zeros % 2 != 0:
return False
sorted_pos = sorted(pos)
sorted_neg = sorted(neg, reverse=True)
res = self.get_result(sorted_pos, d)
if not res:
return False
res = self.get_result(sorted_neg, d)
if not res:
return False
return sum(list(d.values())) == 0 | CLASS_DEF FUNC_DEF FOR VAR VAR IF VAR VAR NUMBER IF BIN_OP NUMBER VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR IF VAR VAR RETURN NUMBER VAR VAR VAR VAR BIN_OP NUMBER VAR VAR RETURN NUMBER FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR RETURN NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
dd = Counter(A)
A.sort(key=lambda a: abs(a))
for a in A:
if a in dd and dd[a]:
if 2 * a in dd and dd[2 * a]:
dd[a] -= 1
dd[2 * a] -= 1
if dd[a] == 0:
del dd[a]
if dd[a * 2] == 0:
del dd[a * 2]
else:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR VAR IF BIN_OP NUMBER VAR VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER VAR BIN_OP NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER VAR |
Given an array of integers A with even length, return true if and only if it is possible to reorder it such that A[2 * i + 1] = 2 * A[2 * i] for every 0 <= i < len(A) / 2.
Example 1:
Input: A = [3,1,3,6]
Output: false
Example 2:
Input: A = [2,1,2,6]
Output: false
Example 3:
Input: A = [4,-2,2,-4]
Output: true
Explanation: We can take two groups, [-2,-4] and [2,4] to form [-2,-4,2,4] or [2,4,-2,-4].
Example 4:
Input: A = [1,2,4,16,8,4]
Output: false
Constraints:
0 <= A.length <= 3 * 104
A.length is even.
-105 <= A[i] <= 105 | class Solution:
def canReorderDoubled(self, A: List[int]) -> bool:
counts = collections.Counter(A)
count = 0
A.sort()
for num in A:
if counts[num] and counts[num * 2]:
counts[num * 2] -= 1
counts[num] -= 1
count += 1
return count * 2 == len(A) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR |
There are n banks in the city where Vasya lives, they are located in a circle, such that any two banks are neighbouring if their indices differ by no more than 1. Also, bank 1 and bank n are neighbours if n > 1. No bank is a neighbour of itself.
Vasya has an account in each bank. Its balance may be negative, meaning Vasya owes some money to this bank.
There is only one type of operations available: transfer some amount of money from any bank to account in any neighbouring bank. There are no restrictions on the size of the sum being transferred or balance requirements to perform this operation.
Vasya doesn't like to deal with large numbers, so he asks you to determine the minimum number of operations required to change the balance of each bank account to zero. It's guaranteed, that this is possible to achieve, that is, the total balance of Vasya in all banks is equal to zero.
-----Input-----
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of banks.
The second line contains n integers a_{i} ( - 10^9 ≤ a_{i} ≤ 10^9), the i-th of them is equal to the initial balance of the account in the i-th bank. It's guaranteed that the sum of all a_{i} is equal to 0.
-----Output-----
Print the minimum number of operations required to change balance in each bank to zero.
-----Examples-----
Input
3
5 0 -5
Output
1
Input
4
-1 0 1 0
Output
2
Input
4
1 2 3 -6
Output
3
-----Note-----
In the first sample, Vasya may transfer 5 from the first bank to the third.
In the second sample, Vasya may first transfer 1 from the third bank to the second, and then 1 from the second to the first.
In the third sample, the following sequence provides the optimal answer: transfer 1 from the first bank to the second bank; transfer 3 from the second bank to the third; transfer 6 from the third bank to the fourth. | n = int(input())
arr = input().split()
arr = [int(i) for i in arr]
di = {}
s = 0
m = 0
for i in arr:
s = s + i
if s in di.keys():
di[s] += 1
if di[s] > m:
m = di[s]
else:
di[s] = 1
if di[s] > m:
m = di[s]
ans = n - m
print(ans) | 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 DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
There are n banks in the city where Vasya lives, they are located in a circle, such that any two banks are neighbouring if their indices differ by no more than 1. Also, bank 1 and bank n are neighbours if n > 1. No bank is a neighbour of itself.
Vasya has an account in each bank. Its balance may be negative, meaning Vasya owes some money to this bank.
There is only one type of operations available: transfer some amount of money from any bank to account in any neighbouring bank. There are no restrictions on the size of the sum being transferred or balance requirements to perform this operation.
Vasya doesn't like to deal with large numbers, so he asks you to determine the minimum number of operations required to change the balance of each bank account to zero. It's guaranteed, that this is possible to achieve, that is, the total balance of Vasya in all banks is equal to zero.
-----Input-----
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of banks.
The second line contains n integers a_{i} ( - 10^9 ≤ a_{i} ≤ 10^9), the i-th of them is equal to the initial balance of the account in the i-th bank. It's guaranteed that the sum of all a_{i} is equal to 0.
-----Output-----
Print the minimum number of operations required to change balance in each bank to zero.
-----Examples-----
Input
3
5 0 -5
Output
1
Input
4
-1 0 1 0
Output
2
Input
4
1 2 3 -6
Output
3
-----Note-----
In the first sample, Vasya may transfer 5 from the first bank to the third.
In the second sample, Vasya may first transfer 1 from the third bank to the second, and then 1 from the second to the first.
In the third sample, the following sequence provides the optimal answer: transfer 1 from the first bank to the second bank; transfer 3 from the second bank to the third; transfer 6 from the third bank to the fourth. | n = int(input())
l = list(map(int, input().split()))
sum = 0
d = dict()
for i in range(n):
sum += l[i]
if sum not in d.keys():
d[sum] = 1
else:
d[sum] += 1
print(n - max(d.values())) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR |
There are n banks in the city where Vasya lives, they are located in a circle, such that any two banks are neighbouring if their indices differ by no more than 1. Also, bank 1 and bank n are neighbours if n > 1. No bank is a neighbour of itself.
Vasya has an account in each bank. Its balance may be negative, meaning Vasya owes some money to this bank.
There is only one type of operations available: transfer some amount of money from any bank to account in any neighbouring bank. There are no restrictions on the size of the sum being transferred or balance requirements to perform this operation.
Vasya doesn't like to deal with large numbers, so he asks you to determine the minimum number of operations required to change the balance of each bank account to zero. It's guaranteed, that this is possible to achieve, that is, the total balance of Vasya in all banks is equal to zero.
-----Input-----
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of banks.
The second line contains n integers a_{i} ( - 10^9 ≤ a_{i} ≤ 10^9), the i-th of them is equal to the initial balance of the account in the i-th bank. It's guaranteed that the sum of all a_{i} is equal to 0.
-----Output-----
Print the minimum number of operations required to change balance in each bank to zero.
-----Examples-----
Input
3
5 0 -5
Output
1
Input
4
-1 0 1 0
Output
2
Input
4
1 2 3 -6
Output
3
-----Note-----
In the first sample, Vasya may transfer 5 from the first bank to the third.
In the second sample, Vasya may first transfer 1 from the third bank to the second, and then 1 from the second to the first.
In the third sample, the following sequence provides the optimal answer: transfer 1 from the first bank to the second bank; transfer 3 from the second bank to the third; transfer 6 from the third bank to the fourth. | n = int(input(""))
l = input("").split(" ")
for i in range(len(l)):
l[i] = int(l[i])
s = 0
m = 0
ps = {}
for i in range(len(l)):
s += l[i]
if s in ps:
ps[s] += 1
else:
ps[s] = 1
if ps[s] > m:
m = ps[s]
print(n - m) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
There are n banks in the city where Vasya lives, they are located in a circle, such that any two banks are neighbouring if their indices differ by no more than 1. Also, bank 1 and bank n are neighbours if n > 1. No bank is a neighbour of itself.
Vasya has an account in each bank. Its balance may be negative, meaning Vasya owes some money to this bank.
There is only one type of operations available: transfer some amount of money from any bank to account in any neighbouring bank. There are no restrictions on the size of the sum being transferred or balance requirements to perform this operation.
Vasya doesn't like to deal with large numbers, so he asks you to determine the minimum number of operations required to change the balance of each bank account to zero. It's guaranteed, that this is possible to achieve, that is, the total balance of Vasya in all banks is equal to zero.
-----Input-----
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of banks.
The second line contains n integers a_{i} ( - 10^9 ≤ a_{i} ≤ 10^9), the i-th of them is equal to the initial balance of the account in the i-th bank. It's guaranteed that the sum of all a_{i} is equal to 0.
-----Output-----
Print the minimum number of operations required to change balance in each bank to zero.
-----Examples-----
Input
3
5 0 -5
Output
1
Input
4
-1 0 1 0
Output
2
Input
4
1 2 3 -6
Output
3
-----Note-----
In the first sample, Vasya may transfer 5 from the first bank to the third.
In the second sample, Vasya may first transfer 1 from the third bank to the second, and then 1 from the second to the first.
In the third sample, the following sequence provides the optimal answer: transfer 1 from the first bank to the second bank; transfer 3 from the second bank to the third; transfer 6 from the third bank to the fourth. | n = int(input())
arr = list(map(int, input().split()))
ans = n - 1
sm = 0
freq = {}
for i in arr:
sm += i
freq[sm] = freq.get(sm, 0) + 1
ans = min(ans, n - freq[sm])
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR |
There are n banks in the city where Vasya lives, they are located in a circle, such that any two banks are neighbouring if their indices differ by no more than 1. Also, bank 1 and bank n are neighbours if n > 1. No bank is a neighbour of itself.
Vasya has an account in each bank. Its balance may be negative, meaning Vasya owes some money to this bank.
There is only one type of operations available: transfer some amount of money from any bank to account in any neighbouring bank. There are no restrictions on the size of the sum being transferred or balance requirements to perform this operation.
Vasya doesn't like to deal with large numbers, so he asks you to determine the minimum number of operations required to change the balance of each bank account to zero. It's guaranteed, that this is possible to achieve, that is, the total balance of Vasya in all banks is equal to zero.
-----Input-----
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of banks.
The second line contains n integers a_{i} ( - 10^9 ≤ a_{i} ≤ 10^9), the i-th of them is equal to the initial balance of the account in the i-th bank. It's guaranteed that the sum of all a_{i} is equal to 0.
-----Output-----
Print the minimum number of operations required to change balance in each bank to zero.
-----Examples-----
Input
3
5 0 -5
Output
1
Input
4
-1 0 1 0
Output
2
Input
4
1 2 3 -6
Output
3
-----Note-----
In the first sample, Vasya may transfer 5 from the first bank to the third.
In the second sample, Vasya may first transfer 1 from the third bank to the second, and then 1 from the second to the first.
In the third sample, the following sequence provides the optimal answer: transfer 1 from the first bank to the second bank; transfer 3 from the second bank to the third; transfer 6 from the third bank to the fourth. | d = {}
n = int(input())
l = list(map(int, input().split(" ")))
k = [l[0]]
d[l[0]] = 0
for i in range(1, n):
k.append(k[-1] + l[i])
d[k[-1]] = 0
maxx = -1
for i in k:
d[i] += 1
maxx = max(maxx, d[i])
print(n - maxx) | ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
There are n banks in the city where Vasya lives, they are located in a circle, such that any two banks are neighbouring if their indices differ by no more than 1. Also, bank 1 and bank n are neighbours if n > 1. No bank is a neighbour of itself.
Vasya has an account in each bank. Its balance may be negative, meaning Vasya owes some money to this bank.
There is only one type of operations available: transfer some amount of money from any bank to account in any neighbouring bank. There are no restrictions on the size of the sum being transferred or balance requirements to perform this operation.
Vasya doesn't like to deal with large numbers, so he asks you to determine the minimum number of operations required to change the balance of each bank account to zero. It's guaranteed, that this is possible to achieve, that is, the total balance of Vasya in all banks is equal to zero.
-----Input-----
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of banks.
The second line contains n integers a_{i} ( - 10^9 ≤ a_{i} ≤ 10^9), the i-th of them is equal to the initial balance of the account in the i-th bank. It's guaranteed that the sum of all a_{i} is equal to 0.
-----Output-----
Print the minimum number of operations required to change balance in each bank to zero.
-----Examples-----
Input
3
5 0 -5
Output
1
Input
4
-1 0 1 0
Output
2
Input
4
1 2 3 -6
Output
3
-----Note-----
In the first sample, Vasya may transfer 5 from the first bank to the third.
In the second sample, Vasya may first transfer 1 from the third bank to the second, and then 1 from the second to the first.
In the third sample, the following sequence provides the optimal answer: transfer 1 from the first bank to the second bank; transfer 3 from the second bank to the third; transfer 6 from the third bank to the fourth. | n = int(input())
a = list(map(int, input().split()))
presum, s = {}, 0
for x in a:
s += x
if s in presum:
presum[s] += 1
else:
presum[s] = 1
print(n - max(presum.values())) | 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 DICT NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR |
There are n banks in the city where Vasya lives, they are located in a circle, such that any two banks are neighbouring if their indices differ by no more than 1. Also, bank 1 and bank n are neighbours if n > 1. No bank is a neighbour of itself.
Vasya has an account in each bank. Its balance may be negative, meaning Vasya owes some money to this bank.
There is only one type of operations available: transfer some amount of money from any bank to account in any neighbouring bank. There are no restrictions on the size of the sum being transferred or balance requirements to perform this operation.
Vasya doesn't like to deal with large numbers, so he asks you to determine the minimum number of operations required to change the balance of each bank account to zero. It's guaranteed, that this is possible to achieve, that is, the total balance of Vasya in all banks is equal to zero.
-----Input-----
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of banks.
The second line contains n integers a_{i} ( - 10^9 ≤ a_{i} ≤ 10^9), the i-th of them is equal to the initial balance of the account in the i-th bank. It's guaranteed that the sum of all a_{i} is equal to 0.
-----Output-----
Print the minimum number of operations required to change balance in each bank to zero.
-----Examples-----
Input
3
5 0 -5
Output
1
Input
4
-1 0 1 0
Output
2
Input
4
1 2 3 -6
Output
3
-----Note-----
In the first sample, Vasya may transfer 5 from the first bank to the third.
In the second sample, Vasya may first transfer 1 from the third bank to the second, and then 1 from the second to the first.
In the third sample, the following sequence provides the optimal answer: transfer 1 from the first bank to the second bank; transfer 3 from the second bank to the third; transfer 6 from the third bank to the fourth. | input()
l = list(map(int, input().split()))
d = {}
sum = 0
for I in range(len(l)):
if sum not in d:
d[sum] = 0
d[sum] += 1
sum += l[I]
a = -100000
for I in d:
a = max(a, d[I])
print(len(l) - a) | EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR |
There are n banks in the city where Vasya lives, they are located in a circle, such that any two banks are neighbouring if their indices differ by no more than 1. Also, bank 1 and bank n are neighbours if n > 1. No bank is a neighbour of itself.
Vasya has an account in each bank. Its balance may be negative, meaning Vasya owes some money to this bank.
There is only one type of operations available: transfer some amount of money from any bank to account in any neighbouring bank. There are no restrictions on the size of the sum being transferred or balance requirements to perform this operation.
Vasya doesn't like to deal with large numbers, so he asks you to determine the minimum number of operations required to change the balance of each bank account to zero. It's guaranteed, that this is possible to achieve, that is, the total balance of Vasya in all banks is equal to zero.
-----Input-----
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of banks.
The second line contains n integers a_{i} ( - 10^9 ≤ a_{i} ≤ 10^9), the i-th of them is equal to the initial balance of the account in the i-th bank. It's guaranteed that the sum of all a_{i} is equal to 0.
-----Output-----
Print the minimum number of operations required to change balance in each bank to zero.
-----Examples-----
Input
3
5 0 -5
Output
1
Input
4
-1 0 1 0
Output
2
Input
4
1 2 3 -6
Output
3
-----Note-----
In the first sample, Vasya may transfer 5 from the first bank to the third.
In the second sample, Vasya may first transfer 1 from the third bank to the second, and then 1 from the second to the first.
In the third sample, the following sequence provides the optimal answer: transfer 1 from the first bank to the second bank; transfer 3 from the second bank to the third; transfer 6 from the third bank to the fourth. | n = int(input())
a = list(map(int, input().split()))
if a == [0] * n:
print(0)
exit(0)
dp1 = [0] * n
dp1[0] = a[0]
for i in range(1, n):
dp1[i] = dp1[i - 1] + a[i]
res = 0
counter = {}
for i in range(n):
try:
counter[dp1[i]] += 1
except:
counter[dp1[i]] = 1
for elem in counter:
res = max(res, counter[elem])
print(n - res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
There are n banks in the city where Vasya lives, they are located in a circle, such that any two banks are neighbouring if their indices differ by no more than 1. Also, bank 1 and bank n are neighbours if n > 1. No bank is a neighbour of itself.
Vasya has an account in each bank. Its balance may be negative, meaning Vasya owes some money to this bank.
There is only one type of operations available: transfer some amount of money from any bank to account in any neighbouring bank. There are no restrictions on the size of the sum being transferred or balance requirements to perform this operation.
Vasya doesn't like to deal with large numbers, so he asks you to determine the minimum number of operations required to change the balance of each bank account to zero. It's guaranteed, that this is possible to achieve, that is, the total balance of Vasya in all banks is equal to zero.
-----Input-----
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of banks.
The second line contains n integers a_{i} ( - 10^9 ≤ a_{i} ≤ 10^9), the i-th of them is equal to the initial balance of the account in the i-th bank. It's guaranteed that the sum of all a_{i} is equal to 0.
-----Output-----
Print the minimum number of operations required to change balance in each bank to zero.
-----Examples-----
Input
3
5 0 -5
Output
1
Input
4
-1 0 1 0
Output
2
Input
4
1 2 3 -6
Output
3
-----Note-----
In the first sample, Vasya may transfer 5 from the first bank to the third.
In the second sample, Vasya may first transfer 1 from the third bank to the second, and then 1 from the second to the first.
In the third sample, the following sequence provides the optimal answer: transfer 1 from the first bank to the second bank; transfer 3 from the second bank to the third; transfer 6 from the third bank to the fourth. | n = int(input())
a = list(map(int, input().split()))
sums = [0] * (n + 1)
for i in range(n):
sums[i + 1] = sums[i] + a[i]
occs = {}
for i in range(1, n + 1):
if sums[i] in occs:
last = occs[sums[i]][-1]
if last[1] == i:
last[1] = i + 1
else:
occs[sums[i]].append([i, i + 1])
else:
occs[sums[i]] = [[i, i + 1]]
ans = n - 1
for k in occs:
cur = 0
for i in range(len(occs[k])):
elem = occs[k][i]
next = occs[k][(i + 1) % len(occs[k])]
if next[0] >= elem[1]:
cur += next[0] - elem[1]
else:
cur += next[0] + n - elem[1]
ans = min(ans, cur)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR LIST LIST VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
There are n banks in the city where Vasya lives, they are located in a circle, such that any two banks are neighbouring if their indices differ by no more than 1. Also, bank 1 and bank n are neighbours if n > 1. No bank is a neighbour of itself.
Vasya has an account in each bank. Its balance may be negative, meaning Vasya owes some money to this bank.
There is only one type of operations available: transfer some amount of money from any bank to account in any neighbouring bank. There are no restrictions on the size of the sum being transferred or balance requirements to perform this operation.
Vasya doesn't like to deal with large numbers, so he asks you to determine the minimum number of operations required to change the balance of each bank account to zero. It's guaranteed, that this is possible to achieve, that is, the total balance of Vasya in all banks is equal to zero.
-----Input-----
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of banks.
The second line contains n integers a_{i} ( - 10^9 ≤ a_{i} ≤ 10^9), the i-th of them is equal to the initial balance of the account in the i-th bank. It's guaranteed that the sum of all a_{i} is equal to 0.
-----Output-----
Print the minimum number of operations required to change balance in each bank to zero.
-----Examples-----
Input
3
5 0 -5
Output
1
Input
4
-1 0 1 0
Output
2
Input
4
1 2 3 -6
Output
3
-----Note-----
In the first sample, Vasya may transfer 5 from the first bank to the third.
In the second sample, Vasya may first transfer 1 from the third bank to the second, and then 1 from the second to the first.
In the third sample, the following sequence provides the optimal answer: transfer 1 from the first bank to the second bank; transfer 3 from the second bank to the third; transfer 6 from the third bank to the fourth. | def main():
n = int(input())
amounts = [int(i) for i in input().split()]
cnt = dict()
prefix = 0
for a in amounts:
prefix += a
if prefix in cnt:
cnt[prefix] += 1
else:
cnt[prefix] = 1
ans = 0
for k in cnt:
if ans < cnt[k]:
ans = cnt[k]
print(n - ans)
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR |
There are n banks in the city where Vasya lives, they are located in a circle, such that any two banks are neighbouring if their indices differ by no more than 1. Also, bank 1 and bank n are neighbours if n > 1. No bank is a neighbour of itself.
Vasya has an account in each bank. Its balance may be negative, meaning Vasya owes some money to this bank.
There is only one type of operations available: transfer some amount of money from any bank to account in any neighbouring bank. There are no restrictions on the size of the sum being transferred or balance requirements to perform this operation.
Vasya doesn't like to deal with large numbers, so he asks you to determine the minimum number of operations required to change the balance of each bank account to zero. It's guaranteed, that this is possible to achieve, that is, the total balance of Vasya in all banks is equal to zero.
-----Input-----
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of banks.
The second line contains n integers a_{i} ( - 10^9 ≤ a_{i} ≤ 10^9), the i-th of them is equal to the initial balance of the account in the i-th bank. It's guaranteed that the sum of all a_{i} is equal to 0.
-----Output-----
Print the minimum number of operations required to change balance in each bank to zero.
-----Examples-----
Input
3
5 0 -5
Output
1
Input
4
-1 0 1 0
Output
2
Input
4
1 2 3 -6
Output
3
-----Note-----
In the first sample, Vasya may transfer 5 from the first bank to the third.
In the second sample, Vasya may first transfer 1 from the third bank to the second, and then 1 from the second to the first.
In the third sample, the following sequence provides the optimal answer: transfer 1 from the first bank to the second bank; transfer 3 from the second bank to the third; transfer 6 from the third bank to the fourth. | n = int(input())
a = list(map(int, input().split()))
d = dict()
total = 0
for i in range(n):
total += a[i]
d[total] = 0
total = 0
ans = 0
for i in range(n):
total += a[i]
d[total] += 1
ans = max(ans, d[total])
print(n - ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
There are n banks in the city where Vasya lives, they are located in a circle, such that any two banks are neighbouring if their indices differ by no more than 1. Also, bank 1 and bank n are neighbours if n > 1. No bank is a neighbour of itself.
Vasya has an account in each bank. Its balance may be negative, meaning Vasya owes some money to this bank.
There is only one type of operations available: transfer some amount of money from any bank to account in any neighbouring bank. There are no restrictions on the size of the sum being transferred or balance requirements to perform this operation.
Vasya doesn't like to deal with large numbers, so he asks you to determine the minimum number of operations required to change the balance of each bank account to zero. It's guaranteed, that this is possible to achieve, that is, the total balance of Vasya in all banks is equal to zero.
-----Input-----
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of banks.
The second line contains n integers a_{i} ( - 10^9 ≤ a_{i} ≤ 10^9), the i-th of them is equal to the initial balance of the account in the i-th bank. It's guaranteed that the sum of all a_{i} is equal to 0.
-----Output-----
Print the minimum number of operations required to change balance in each bank to zero.
-----Examples-----
Input
3
5 0 -5
Output
1
Input
4
-1 0 1 0
Output
2
Input
4
1 2 3 -6
Output
3
-----Note-----
In the first sample, Vasya may transfer 5 from the first bank to the third.
In the second sample, Vasya may first transfer 1 from the third bank to the second, and then 1 from the second to the first.
In the third sample, the following sequence provides the optimal answer: transfer 1 from the first bank to the second bank; transfer 3 from the second bank to the third; transfer 6 from the third bank to the fourth. | n = int(input())
s, p, ans = 0, {}, n - 1
for i in map(int, input().split()):
s += i
if s not in p:
p[s] = 1
else:
p[s] += 1
ans = min(ans, n - p[s])
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER DICT BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR |
There are n banks in the city where Vasya lives, they are located in a circle, such that any two banks are neighbouring if their indices differ by no more than 1. Also, bank 1 and bank n are neighbours if n > 1. No bank is a neighbour of itself.
Vasya has an account in each bank. Its balance may be negative, meaning Vasya owes some money to this bank.
There is only one type of operations available: transfer some amount of money from any bank to account in any neighbouring bank. There are no restrictions on the size of the sum being transferred or balance requirements to perform this operation.
Vasya doesn't like to deal with large numbers, so he asks you to determine the minimum number of operations required to change the balance of each bank account to zero. It's guaranteed, that this is possible to achieve, that is, the total balance of Vasya in all banks is equal to zero.
-----Input-----
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of banks.
The second line contains n integers a_{i} ( - 10^9 ≤ a_{i} ≤ 10^9), the i-th of them is equal to the initial balance of the account in the i-th bank. It's guaranteed that the sum of all a_{i} is equal to 0.
-----Output-----
Print the minimum number of operations required to change balance in each bank to zero.
-----Examples-----
Input
3
5 0 -5
Output
1
Input
4
-1 0 1 0
Output
2
Input
4
1 2 3 -6
Output
3
-----Note-----
In the first sample, Vasya may transfer 5 from the first bank to the third.
In the second sample, Vasya may first transfer 1 from the third bank to the second, and then 1 from the second to the first.
In the third sample, the following sequence provides the optimal answer: transfer 1 from the first bank to the second bank; transfer 3 from the second bank to the third; transfer 6 from the third bank to the fourth. | (n,) = map(int, input().split())
a = map(int, input().split())
s = 0
d = {}
for x in a:
s += x
d[s] = d.get(s, 0) + 1
print(n - max([d[k] for k in d])) | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR |
There are n banks in the city where Vasya lives, they are located in a circle, such that any two banks are neighbouring if their indices differ by no more than 1. Also, bank 1 and bank n are neighbours if n > 1. No bank is a neighbour of itself.
Vasya has an account in each bank. Its balance may be negative, meaning Vasya owes some money to this bank.
There is only one type of operations available: transfer some amount of money from any bank to account in any neighbouring bank. There are no restrictions on the size of the sum being transferred or balance requirements to perform this operation.
Vasya doesn't like to deal with large numbers, so he asks you to determine the minimum number of operations required to change the balance of each bank account to zero. It's guaranteed, that this is possible to achieve, that is, the total balance of Vasya in all banks is equal to zero.
-----Input-----
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of banks.
The second line contains n integers a_{i} ( - 10^9 ≤ a_{i} ≤ 10^9), the i-th of them is equal to the initial balance of the account in the i-th bank. It's guaranteed that the sum of all a_{i} is equal to 0.
-----Output-----
Print the minimum number of operations required to change balance in each bank to zero.
-----Examples-----
Input
3
5 0 -5
Output
1
Input
4
-1 0 1 0
Output
2
Input
4
1 2 3 -6
Output
3
-----Note-----
In the first sample, Vasya may transfer 5 from the first bank to the third.
In the second sample, Vasya may first transfer 1 from the third bank to the second, and then 1 from the second to the first.
In the third sample, the following sequence provides the optimal answer: transfer 1 from the first bank to the second bank; transfer 3 from the second bank to the third; transfer 6 from the third bank to the fourth. | n = int(input())
A = list(map(int, input().split()))
S = {}
s = 0
k = 1
for i in range(n):
s += A[i]
try:
S[s] += 1
except KeyError:
S[s] = 1
k = max(k, S[s])
print(n - k) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
There are n banks in the city where Vasya lives, they are located in a circle, such that any two banks are neighbouring if their indices differ by no more than 1. Also, bank 1 and bank n are neighbours if n > 1. No bank is a neighbour of itself.
Vasya has an account in each bank. Its balance may be negative, meaning Vasya owes some money to this bank.
There is only one type of operations available: transfer some amount of money from any bank to account in any neighbouring bank. There are no restrictions on the size of the sum being transferred or balance requirements to perform this operation.
Vasya doesn't like to deal with large numbers, so he asks you to determine the minimum number of operations required to change the balance of each bank account to zero. It's guaranteed, that this is possible to achieve, that is, the total balance of Vasya in all banks is equal to zero.
-----Input-----
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of banks.
The second line contains n integers a_{i} ( - 10^9 ≤ a_{i} ≤ 10^9), the i-th of them is equal to the initial balance of the account in the i-th bank. It's guaranteed that the sum of all a_{i} is equal to 0.
-----Output-----
Print the minimum number of operations required to change balance in each bank to zero.
-----Examples-----
Input
3
5 0 -5
Output
1
Input
4
-1 0 1 0
Output
2
Input
4
1 2 3 -6
Output
3
-----Note-----
In the first sample, Vasya may transfer 5 from the first bank to the third.
In the second sample, Vasya may first transfer 1 from the third bank to the second, and then 1 from the second to the first.
In the third sample, the following sequence provides the optimal answer: transfer 1 from the first bank to the second bank; transfer 3 from the second bank to the third; transfer 6 from the third bank to the fourth. | def sol1(arr):
mp = {}
cnt = 0
n = len(arr)
ans = 0
for a in arr:
ans += a
if ans in mp:
mp[ans] += 1
else:
mp[ans] = 1
cnt = max(cnt, int(mp[ans]))
return n - cnt
n = int(input())
arr = list(map(int, input().split()))
print(sol1(arr)) | FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun of him and hid the string s. Ivan preferred making a new string to finding the old one.
Ivan knows some information about the string s. Namely, he remembers, that string ti occurs in string s at least ki times or more, he also remembers exactly ki positions where the string ti occurs in string s: these positions are xi, 1, xi, 2, ..., xi, ki. He remembers n such strings ti.
You are to reconstruct lexicographically minimal string s such that it fits all the information Ivan remembers. Strings ti and string s consist of small English letters only.
Input
The first line contains single integer n (1 ≤ n ≤ 105) — the number of strings Ivan remembers.
The next n lines contain information about the strings. The i-th of these lines contains non-empty string ti, then positive integer ki, which equal to the number of times the string ti occurs in string s, and then ki distinct positive integers xi, 1, xi, 2, ..., xi, ki in increasing order — positions, in which occurrences of the string ti in the string s start. It is guaranteed that the sum of lengths of strings ti doesn't exceed 106, 1 ≤ xi, j ≤ 106, 1 ≤ ki ≤ 106, and the sum of all ki doesn't exceed 106. The strings ti can coincide.
It is guaranteed that the input data is not self-contradictory, and thus at least one answer always exists.
Output
Print lexicographically minimal string that fits all the information Ivan remembers.
Examples
Input
3
a 4 1 3 5 7
ab 2 1 5
ca 1 4
Output
abacaba
Input
1
a 1 3
Output
aaa
Input
3
ab 1 1
aba 1 3
ab 2 3 5
Output
ababab | import sys
input = sys.stdin.readline
n = int(input())
L = 0
t, x = [], []
for i in range(n):
v = input().split()
t.append(v[0])
pos = list(map(lambda x: int(x) - 1, v[2:]))
x.append(pos)
L = max(L, pos[-1] + len(t[i]))
nxt = list(range(L + 1))
ans = ["a"] * L
def find(j):
jcopy = j
while nxt[j] != j:
j = nxt[j]
j, jcopy = jcopy, j
while nxt[j] != j:
tmp = nxt[j]
nxt[j] = jcopy
j = tmp
return j
for i in range(n):
for j in x[i]:
l = j
j = find(nxt[j])
while j < l + len(t[i]):
ans[j] = t[i][j - l]
nxt[j] = j + 1
j = find(nxt[j])
print(*ans, sep="") | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST STRING VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING |
Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun of him and hid the string s. Ivan preferred making a new string to finding the old one.
Ivan knows some information about the string s. Namely, he remembers, that string ti occurs in string s at least ki times or more, he also remembers exactly ki positions where the string ti occurs in string s: these positions are xi, 1, xi, 2, ..., xi, ki. He remembers n such strings ti.
You are to reconstruct lexicographically minimal string s such that it fits all the information Ivan remembers. Strings ti and string s consist of small English letters only.
Input
The first line contains single integer n (1 ≤ n ≤ 105) — the number of strings Ivan remembers.
The next n lines contain information about the strings. The i-th of these lines contains non-empty string ti, then positive integer ki, which equal to the number of times the string ti occurs in string s, and then ki distinct positive integers xi, 1, xi, 2, ..., xi, ki in increasing order — positions, in which occurrences of the string ti in the string s start. It is guaranteed that the sum of lengths of strings ti doesn't exceed 106, 1 ≤ xi, j ≤ 106, 1 ≤ ki ≤ 106, and the sum of all ki doesn't exceed 106. The strings ti can coincide.
It is guaranteed that the input data is not self-contradictory, and thus at least one answer always exists.
Output
Print lexicographically minimal string that fits all the information Ivan remembers.
Examples
Input
3
a 4 1 3 5 7
ab 2 1 5
ca 1 4
Output
abacaba
Input
1
a 1 3
Output
aaa
Input
3
ab 1 1
aba 1 3
ab 2 3 5
Output
ababab | from sys import stdin, stdout
sze = 10**6 + 1
n = int(stdin.readline())
challengers = []
strings = []
sze = 10**6 + 1
cnt = [[] for i in range(sze)]
for i in range(n):
s = stdin.readline().strip().split()
num = int(s[1])
values = list(map(int, s[2:]))
strings.append(s[0])
for j in range(num):
cnt[values[j]].append(i)
previous = 1
for i in range(sze):
if not cnt[i]:
continue
ind, s = i, max(cnt[i], key=lambda x: len(strings[x]))
s = strings[s]
if previous < ind:
stdout.write(str("a") * (ind - previous))
previous = ind
if previous > ind + len(s) - 1:
continue
else:
stdout.write(s[previous - ind : len(s)])
previous = ind + len(s) | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING BIN_OP VAR VAR ASSIGN VAR VAR IF VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR |
Alice has a cake, and she is going to cut it. She will perform the following operation $n-1$ times: choose a piece of the cake (initially, the cake is all one piece) with weight $w\ge 2$ and cut it into two smaller pieces of weight $\lfloor\frac{w}{2}\rfloor$ and $\lceil\frac{w}{2}\rceil$ ($\lfloor x \rfloor$ and $\lceil x \rceil$ denote floor and ceiling functions , respectively).
After cutting the cake in $n$ pieces, she will line up these $n$ pieces on a table in an arbitrary order. Let $a_i$ be the weight of the $i$-th piece in the line.
You are given the array $a$. Determine whether there exists an initial weight and sequence of operations which results in $a$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$).
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print a single line: print YES if the array $a$ could have resulted from Alice's operations, otherwise print NO.
You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
-----Examples-----
Input
14
1
327
2
869 541
2
985214736 985214737
3
2 3 1
3
2 3 3
6
1 1 1 1 1 1
6
100 100 100 100 100 100
8
100 100 100 100 100 100 100 100
8
2 16 1 8 64 1 4 32
10
1 2 4 7 1 1 1 1 7 2
10
7 1 1 1 3 1 3 3 2 3
10
1 4 4 1 1 1 3 3 3 1
10
2 3 2 2 1 2 2 2 2 2
4
999999999 999999999 999999999 999999999
Output
YES
NO
YES
YES
NO
YES
NO
YES
YES
YES
YES
NO
NO
YES
-----Note-----
In the first test case, it's possible to get the array $a$ by performing $0$ operations on a cake with weight $327$.
In the second test case, it's not possible to get the array $a$.
In the third test case, it's possible to get the array $a$ by performing $1$ operation on a cake with weight $1\,970\,429\,473$:
Cut it in half, so that the weights are $[985\,214\,736, 985\,214\,737]$.
Note that the starting weight can be greater than $10^9$.
In the fourth test case, it's possible to get the array $a$ by performing $2$ operations on a cake with weight $6$:
Cut it in half, so that the weights are $[3,3]$.
Cut one of the two pieces with weight $3$, so that the new weights are $[1, 2, 3]$ which is equivalent to $[2, 3, 1]$ up to reordering. | t = int(input())
for _ in range(t):
n = int(input())
a = [int(x) for x in input().split()]
pieces = [sum(a)]
frequency = {}
for i in a:
frequency[i] = frequency.get(i, 0) + 1
operation = 0
while pieces and operation < n:
piece = pieces.pop()
if frequency.get(piece):
frequency[piece] -= 1
continue
pieces.append(piece // 2)
pieces.append((piece + 1) // 2)
operation += 1
print("YES") if operation == n - 1 else print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING FUNC_CALL VAR STRING |
Alice has a cake, and she is going to cut it. She will perform the following operation $n-1$ times: choose a piece of the cake (initially, the cake is all one piece) with weight $w\ge 2$ and cut it into two smaller pieces of weight $\lfloor\frac{w}{2}\rfloor$ and $\lceil\frac{w}{2}\rceil$ ($\lfloor x \rfloor$ and $\lceil x \rceil$ denote floor and ceiling functions , respectively).
After cutting the cake in $n$ pieces, she will line up these $n$ pieces on a table in an arbitrary order. Let $a_i$ be the weight of the $i$-th piece in the line.
You are given the array $a$. Determine whether there exists an initial weight and sequence of operations which results in $a$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$).
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print a single line: print YES if the array $a$ could have resulted from Alice's operations, otherwise print NO.
You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
-----Examples-----
Input
14
1
327
2
869 541
2
985214736 985214737
3
2 3 1
3
2 3 3
6
1 1 1 1 1 1
6
100 100 100 100 100 100
8
100 100 100 100 100 100 100 100
8
2 16 1 8 64 1 4 32
10
1 2 4 7 1 1 1 1 7 2
10
7 1 1 1 3 1 3 3 2 3
10
1 4 4 1 1 1 3 3 3 1
10
2 3 2 2 1 2 2 2 2 2
4
999999999 999999999 999999999 999999999
Output
YES
NO
YES
YES
NO
YES
NO
YES
YES
YES
YES
NO
NO
YES
-----Note-----
In the first test case, it's possible to get the array $a$ by performing $0$ operations on a cake with weight $327$.
In the second test case, it's not possible to get the array $a$.
In the third test case, it's possible to get the array $a$ by performing $1$ operation on a cake with weight $1\,970\,429\,473$:
Cut it in half, so that the weights are $[985\,214\,736, 985\,214\,737]$.
Note that the starting weight can be greater than $10^9$.
In the fourth test case, it's possible to get the array $a$ by performing $2$ operations on a cake with weight $6$:
Cut it in half, so that the weights are $[3,3]$.
Cut one of the two pieces with weight $3$, so that the new weights are $[1, 2, 3]$ which is equivalent to $[2, 3, 1]$ up to reordering. | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
total = sum(a)
st = dict()
for i in a:
st[i] = st.get(i, 0) + 1
nums = [total]
ans = True
while len(st) > 0 and len(nums) > 0:
lt = nums.pop()
if st.get(lt, 0) > 0:
st[lt] = st.get(lt) - 1
if st[lt] == 0:
st.pop(lt)
continue
if lt == 1:
ans = False
break
nums.append(lt // 2)
nums.append((lt + 1) // 2)
if len(st) > 0 or len(nums) > 0:
ans = False
print("YES" if ans else "NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING |
Alice has a cake, and she is going to cut it. She will perform the following operation $n-1$ times: choose a piece of the cake (initially, the cake is all one piece) with weight $w\ge 2$ and cut it into two smaller pieces of weight $\lfloor\frac{w}{2}\rfloor$ and $\lceil\frac{w}{2}\rceil$ ($\lfloor x \rfloor$ and $\lceil x \rceil$ denote floor and ceiling functions , respectively).
After cutting the cake in $n$ pieces, she will line up these $n$ pieces on a table in an arbitrary order. Let $a_i$ be the weight of the $i$-th piece in the line.
You are given the array $a$. Determine whether there exists an initial weight and sequence of operations which results in $a$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$).
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print a single line: print YES if the array $a$ could have resulted from Alice's operations, otherwise print NO.
You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
-----Examples-----
Input
14
1
327
2
869 541
2
985214736 985214737
3
2 3 1
3
2 3 3
6
1 1 1 1 1 1
6
100 100 100 100 100 100
8
100 100 100 100 100 100 100 100
8
2 16 1 8 64 1 4 32
10
1 2 4 7 1 1 1 1 7 2
10
7 1 1 1 3 1 3 3 2 3
10
1 4 4 1 1 1 3 3 3 1
10
2 3 2 2 1 2 2 2 2 2
4
999999999 999999999 999999999 999999999
Output
YES
NO
YES
YES
NO
YES
NO
YES
YES
YES
YES
NO
NO
YES
-----Note-----
In the first test case, it's possible to get the array $a$ by performing $0$ operations on a cake with weight $327$.
In the second test case, it's not possible to get the array $a$.
In the third test case, it's possible to get the array $a$ by performing $1$ operation on a cake with weight $1\,970\,429\,473$:
Cut it in half, so that the weights are $[985\,214\,736, 985\,214\,737]$.
Note that the starting weight can be greater than $10^9$.
In the fourth test case, it's possible to get the array $a$ by performing $2$ operations on a cake with weight $6$:
Cut it in half, so that the weights are $[3,3]$.
Cut one of the two pieces with weight $3$, so that the new weights are $[1, 2, 3]$ which is equivalent to $[2, 3, 1]$ up to reordering. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n = int(input())
l = list(map(int, input().strip().split()))
a = sum(l)
lk = [a]
d = {}
b = min(l)
for i in l:
if i in d:
d[i] += 1
else:
d[i] = 1
while len(lk) > 0:
a = lk[0]
p, q = a // 2, a // 2 + a % 2
if a in d and d[a] > 0:
d[a] -= 1
del lk[0]
elif p < b:
print("NO")
break
else:
del lk[0]
lk = [p, q] + lk
else:
print("YES") | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR VAR EXPR FUNC_CALL VAR STRING |
Alice has a cake, and she is going to cut it. She will perform the following operation $n-1$ times: choose a piece of the cake (initially, the cake is all one piece) with weight $w\ge 2$ and cut it into two smaller pieces of weight $\lfloor\frac{w}{2}\rfloor$ and $\lceil\frac{w}{2}\rceil$ ($\lfloor x \rfloor$ and $\lceil x \rceil$ denote floor and ceiling functions , respectively).
After cutting the cake in $n$ pieces, she will line up these $n$ pieces on a table in an arbitrary order. Let $a_i$ be the weight of the $i$-th piece in the line.
You are given the array $a$. Determine whether there exists an initial weight and sequence of operations which results in $a$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$).
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print a single line: print YES if the array $a$ could have resulted from Alice's operations, otherwise print NO.
You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
-----Examples-----
Input
14
1
327
2
869 541
2
985214736 985214737
3
2 3 1
3
2 3 3
6
1 1 1 1 1 1
6
100 100 100 100 100 100
8
100 100 100 100 100 100 100 100
8
2 16 1 8 64 1 4 32
10
1 2 4 7 1 1 1 1 7 2
10
7 1 1 1 3 1 3 3 2 3
10
1 4 4 1 1 1 3 3 3 1
10
2 3 2 2 1 2 2 2 2 2
4
999999999 999999999 999999999 999999999
Output
YES
NO
YES
YES
NO
YES
NO
YES
YES
YES
YES
NO
NO
YES
-----Note-----
In the first test case, it's possible to get the array $a$ by performing $0$ operations on a cake with weight $327$.
In the second test case, it's not possible to get the array $a$.
In the third test case, it's possible to get the array $a$ by performing $1$ operation on a cake with weight $1\,970\,429\,473$:
Cut it in half, so that the weights are $[985\,214\,736, 985\,214\,737]$.
Note that the starting weight can be greater than $10^9$.
In the fourth test case, it's possible to get the array $a$ by performing $2$ operations on a cake with weight $6$:
Cut it in half, so that the weights are $[3,3]$.
Cut one of the two pieces with weight $3$, so that the new weights are $[1, 2, 3]$ which is equivalent to $[2, 3, 1]$ up to reordering. | def solve():
n = int(input())
a = []
counts = {}
for x in input().split():
x = int(x)
a.append(x)
counts.setdefault(x, 0)
counts[x] += 1
initial = sum(a)
stop = min(a)
unwanted = [initial]
while unwanted != []:
sl = unwanted.pop(0)
if sl in counts and counts[sl] > 0:
counts[sl] -= 1
else:
w, y = sl // 2, sl // 2 + sl % 2
if w < stop:
print("NO")
return
unwanted = [w, y] + unwanted
print("YES")
for tc in range(int(input())):
solve() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR WHILE VAR LIST ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR BIN_OP LIST VAR VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Alice has a cake, and she is going to cut it. She will perform the following operation $n-1$ times: choose a piece of the cake (initially, the cake is all one piece) with weight $w\ge 2$ and cut it into two smaller pieces of weight $\lfloor\frac{w}{2}\rfloor$ and $\lceil\frac{w}{2}\rceil$ ($\lfloor x \rfloor$ and $\lceil x \rceil$ denote floor and ceiling functions , respectively).
After cutting the cake in $n$ pieces, she will line up these $n$ pieces on a table in an arbitrary order. Let $a_i$ be the weight of the $i$-th piece in the line.
You are given the array $a$. Determine whether there exists an initial weight and sequence of operations which results in $a$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$).
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print a single line: print YES if the array $a$ could have resulted from Alice's operations, otherwise print NO.
You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
-----Examples-----
Input
14
1
327
2
869 541
2
985214736 985214737
3
2 3 1
3
2 3 3
6
1 1 1 1 1 1
6
100 100 100 100 100 100
8
100 100 100 100 100 100 100 100
8
2 16 1 8 64 1 4 32
10
1 2 4 7 1 1 1 1 7 2
10
7 1 1 1 3 1 3 3 2 3
10
1 4 4 1 1 1 3 3 3 1
10
2 3 2 2 1 2 2 2 2 2
4
999999999 999999999 999999999 999999999
Output
YES
NO
YES
YES
NO
YES
NO
YES
YES
YES
YES
NO
NO
YES
-----Note-----
In the first test case, it's possible to get the array $a$ by performing $0$ operations on a cake with weight $327$.
In the second test case, it's not possible to get the array $a$.
In the third test case, it's possible to get the array $a$ by performing $1$ operation on a cake with weight $1\,970\,429\,473$:
Cut it in half, so that the weights are $[985\,214\,736, 985\,214\,737]$.
Note that the starting weight can be greater than $10^9$.
In the fourth test case, it's possible to get the array $a$ by performing $2$ operations on a cake with weight $6$:
Cut it in half, so that the weights are $[3,3]$.
Cut one of the two pieces with weight $3$, so that the new weights are $[1, 2, 3]$ which is equivalent to $[2, 3, 1]$ up to reordering. | t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
s = set()
k = {}
su = 0
for i in a:
if i in s:
k[i] += 1
else:
k[i] = 1
s.add(i)
su += i
vec = [su]
p = 0
i = 0
flag = True
while flag and i < len(vec) and n >= 0:
c = vec[i]
if c in s and k[c] > 0:
k[c] -= 1
if k[c] == 0:
p += 1
else:
if c == 1:
flag = False
break
vec.append(int(c / 2))
vec.append(round(c / 2 + 0.1))
n -= 1
i += 1
if flag and p == len(s):
print("YES")
else:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
There are $n$ robots driving along an OX axis. There are also two walls: one is at coordinate $0$ and one is at coordinate $m$.
The $i$-th robot starts at an integer coordinate $x_i~(0 < x_i < m)$ and moves either left (towards the $0$) or right with the speed of $1$ unit per second. No two robots start at the same coordinate.
Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.
Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.
For each robot find out if it ever explodes and print the time of explosion if it happens and $-1$ otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the descriptions of $t$ testcases follow.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 3 \cdot 10^5$; $2 \le m \le 10^8$) — the number of robots and the coordinate of the right wall.
The second line of each testcase contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 < x_i < m$) — the starting coordinates of the robots.
The third line of each testcase contains $n$ space-separated characters 'L' or 'R' — the starting directions of the robots ('L' stands for left and 'R' stands for right).
All coordinates $x_i$ in the testcase are distinct.
The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each testcase print $n$ integers — for the $i$-th robot output the time it explodes at if it does and $-1$ otherwise.
-----Examples-----
Input
5
7 12
1 2 3 4 9 10 11
R R L L R R R
2 10
1 6
R R
2 10
1 3
L L
1 10
5
R
7 8
6 1 7 2 3 5 4
R L R L L L L
Output
1 1 1 1 2 -1 2
-1 -1
2 2
-1
-1 2 7 3 2 7 3
-----Note-----
Here is the picture for the seconds $0, 1, 2$ and $3$ of the first testcase:
Notice that robots $2$ and $3$ don't collide because they meet at the same point $2.5$, which is not integer.
After second $3$ robot $6$ just drive infinitely because there's no robot to collide with. | t = int(input())
for tc in range(t):
n, m = map(int, input().split())
p = list(map(int, input().split()))
d = input().split()
a = [(p[i], d[i], i) for i in range(n)]
a = sorted(a)
sol = [(-1) for _ in range(n)]
odd, even = [], []
for i in range(n):
q = odd if a[i][0] % 2 else even
if len(q) == 0:
q.append(a[i])
elif q[-1][1] == "R" and a[i][1] == "L":
sol[a[i][2]] = sol[q[-1][2]] = (a[i][0] - q[-1][0]) // 2
q.pop()
else:
q.append(a[i])
for q in (odd, even):
while len(q) >= 2 and q[-2][1] == "R":
sol[q[-2][2]] = sol[q[-1][2]] = m - q[-1][0] + (q[-1][0] - q[-2][0]) // 2
q.pop()
q.pop()
i = 1
while i < len(q):
if q[i][1] != "L":
break
sol[q[i - 1][2]] = sol[q[i][2]] = q[i - 1][0] + (q[i][0] - q[i - 1][0]) // 2
i += 2
if i + 1 <= len(q):
sol[q[-1][2]] = sol[q[-2][2]] = m - (q[-1][0] - q[-2][0]) // 2
print(" ".join(map(str, sol))) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER NUMBER STRING VAR VAR NUMBER STRING ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER STRING ASSIGN VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
There are $n$ robots driving along an OX axis. There are also two walls: one is at coordinate $0$ and one is at coordinate $m$.
The $i$-th robot starts at an integer coordinate $x_i~(0 < x_i < m)$ and moves either left (towards the $0$) or right with the speed of $1$ unit per second. No two robots start at the same coordinate.
Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.
Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.
For each robot find out if it ever explodes and print the time of explosion if it happens and $-1$ otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the descriptions of $t$ testcases follow.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 3 \cdot 10^5$; $2 \le m \le 10^8$) — the number of robots and the coordinate of the right wall.
The second line of each testcase contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 < x_i < m$) — the starting coordinates of the robots.
The third line of each testcase contains $n$ space-separated characters 'L' or 'R' — the starting directions of the robots ('L' stands for left and 'R' stands for right).
All coordinates $x_i$ in the testcase are distinct.
The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each testcase print $n$ integers — for the $i$-th robot output the time it explodes at if it does and $-1$ otherwise.
-----Examples-----
Input
5
7 12
1 2 3 4 9 10 11
R R L L R R R
2 10
1 6
R R
2 10
1 3
L L
1 10
5
R
7 8
6 1 7 2 3 5 4
R L R L L L L
Output
1 1 1 1 2 -1 2
-1 -1
2 2
-1
-1 2 7 3 2 7 3
-----Note-----
Here is the picture for the seconds $0, 1, 2$ and $3$ of the first testcase:
Notice that robots $2$ and $3$ don't collide because they meet at the same point $2.5$, which is not integer.
After second $3$ robot $6$ just drive infinitely because there's no robot to collide with. | t = int(input())
for _ in range(t):
n, m = map(int, input().split())
x = [*map(int, input().split())]
d = [*input().split()]
ind = sorted(range(n), key=lambda i: x[i])
a = [-1] * n
ls = []
rs = []
for j in range(n):
i = ind[j]
if x[i] % 2 == 1:
continue
if d[i] == "R":
rs.append(i)
elif not ls and not rs:
ls.append(i)
elif rs:
a[i] = a[rs[-1]] = (x[i] - x[rs[-1]]) // 2
rs.pop()
else:
a[i] = a[ls[-1]] = x[ls[-1]] + (x[i] - x[ls[-1]]) // 2
ls.pop()
while len(rs) >= 2:
i = rs[-1]
j = rs[-2]
rs.pop()
rs.pop()
di = m - x[i]
dj = m - x[j]
a[i] = a[j] = di + (dj - di) // 2
if ls and rs:
a[ls[0]] = a[rs[0]] = (m + x[ls[0]] + (m - x[rs[0]])) // 2
ls = []
rs = []
for j in range(n):
i = ind[j]
if x[i] % 2 == 0:
continue
if d[i] == "R":
rs.append(i)
elif not ls and not rs:
ls.append(i)
elif rs:
a[i] = a[rs[-1]] = (x[i] - x[rs[-1]]) // 2
rs.pop()
else:
a[i] = a[ls[-1]] = x[ls[-1]] + (x[i] - x[ls[-1]]) // 2
ls.pop()
while len(rs) >= 2:
i = rs[-1]
j = rs[-2]
rs.pop()
rs.pop()
di = m - x[i]
dj = m - x[j]
a[i] = a[j] = di + (dj - di) // 2
if ls and rs:
a[ls[0]] = a[rs[0]] = (m + x[ls[0]] + (m - x[rs[0]])) // 2
print(*a) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
There are $n$ robots driving along an OX axis. There are also two walls: one is at coordinate $0$ and one is at coordinate $m$.
The $i$-th robot starts at an integer coordinate $x_i~(0 < x_i < m)$ and moves either left (towards the $0$) or right with the speed of $1$ unit per second. No two robots start at the same coordinate.
Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.
Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.
For each robot find out if it ever explodes and print the time of explosion if it happens and $-1$ otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the descriptions of $t$ testcases follow.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 3 \cdot 10^5$; $2 \le m \le 10^8$) — the number of robots and the coordinate of the right wall.
The second line of each testcase contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 < x_i < m$) — the starting coordinates of the robots.
The third line of each testcase contains $n$ space-separated characters 'L' or 'R' — the starting directions of the robots ('L' stands for left and 'R' stands for right).
All coordinates $x_i$ in the testcase are distinct.
The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each testcase print $n$ integers — for the $i$-th robot output the time it explodes at if it does and $-1$ otherwise.
-----Examples-----
Input
5
7 12
1 2 3 4 9 10 11
R R L L R R R
2 10
1 6
R R
2 10
1 3
L L
1 10
5
R
7 8
6 1 7 2 3 5 4
R L R L L L L
Output
1 1 1 1 2 -1 2
-1 -1
2 2
-1
-1 2 7 3 2 7 3
-----Note-----
Here is the picture for the seconds $0, 1, 2$ and $3$ of the first testcase:
Notice that robots $2$ and $3$ don't collide because they meet at the same point $2.5$, which is not integer.
After second $3$ robot $6$ just drive infinitely because there's no robot to collide with. | import sys
for _ in range(int(input())):
n, m = [int(x) for x in input().split()]
arr = [int(x) for x in input().split()]
temp = [x for x in input().split()]
for i in range(n):
arr[i] = tuple([arr[i], temp[i], i])
arr.sort()
odd, even = [], []
for i in range(n):
if arr[i][0] % 2:
odd.append(tuple([arr[i][2], arr[i][0], arr[i][1]]))
else:
even.append(tuple([arr[i][2], arr[i][0], arr[i][1]]))
ans = [(-1) for i in range(n)]
s = []
temp = []
for i in odd:
if i[2] == "R":
s.append([i[1], i[0], i[2]])
elif s:
curr = s.pop()
ans[curr[1]] = (i[1] - curr[0]) // 2
ans[i[0]] = (i[1] - curr[0]) // 2
else:
temp.append([i[1], i[0], i[2]])
i = 0
temp = temp[::-1]
while temp:
a = temp.pop()
if temp:
b = temp.pop()
ans[a[1]] = (b[0] - a[0]) // 2 + a[0]
ans[b[1]] = (b[0] - a[0]) // 2 + a[0]
else:
temp.append(a)
break
while s:
a = s.pop()
if s:
b = s.pop()
ans[a[1]] = (a[0] - b[0]) // 2 + m - a[0]
ans[b[1]] = (a[0] - b[0]) // 2 + m - a[0]
else:
s.append(a)
break
if s and temp:
a = temp.pop()
b = s.pop()
if a[0] > m - b[0]:
ans[a[1]] = (m - (a[0] - (m - b[0]))) // 2 + a[0]
ans[b[1]] = (m - (a[0] - (m - b[0]))) // 2 + a[0]
else:
ans[a[1]] = (m - (m - b[0] - a[0])) // 2 + (m - b[0])
ans[b[1]] = (m - (m - b[0] - a[0])) // 2 + (m - b[0])
temp = []
s = []
for i in even:
if i[2] == "R":
s.append([i[1], i[0], i[2]])
elif s:
curr = s.pop()
ans[curr[1]] = (i[1] - curr[0]) // 2
ans[i[0]] = (i[1] - curr[0]) // 2
else:
temp.append([i[1], i[0], i[2]])
i = 0
temp = temp[::-1]
while temp:
a = temp.pop()
if temp:
b = temp.pop()
ans[a[1]] = (b[0] - a[0]) // 2 + a[0]
ans[b[1]] = (b[0] - a[0]) // 2 + a[0]
else:
temp.append(a)
break
while s:
a = s.pop()
if s:
b = s.pop()
ans[a[1]] = (a[0] - b[0]) // 2 + m - a[0]
ans[b[1]] = (a[0] - b[0]) // 2 + m - a[0]
else:
s.append(a)
break
if s and temp:
a = temp.pop()
b = s.pop()
if a[0] > m - b[0]:
ans[a[1]] = (m - (a[0] - (m - b[0]))) // 2 + a[0]
ans[b[1]] = (m - (a[0] - (m - b[0]))) // 2 + a[0]
else:
ans[a[1]] = (m - (m - b[0] - a[0])) // 2 + (m - b[0])
ans[b[1]] = (m - (m - b[0] - a[0])) // 2 + (m - b[0])
print(*ans) | IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
There are $n$ robots driving along an OX axis. There are also two walls: one is at coordinate $0$ and one is at coordinate $m$.
The $i$-th robot starts at an integer coordinate $x_i~(0 < x_i < m)$ and moves either left (towards the $0$) or right with the speed of $1$ unit per second. No two robots start at the same coordinate.
Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.
Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.
For each robot find out if it ever explodes and print the time of explosion if it happens and $-1$ otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the descriptions of $t$ testcases follow.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 3 \cdot 10^5$; $2 \le m \le 10^8$) — the number of robots and the coordinate of the right wall.
The second line of each testcase contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 < x_i < m$) — the starting coordinates of the robots.
The third line of each testcase contains $n$ space-separated characters 'L' or 'R' — the starting directions of the robots ('L' stands for left and 'R' stands for right).
All coordinates $x_i$ in the testcase are distinct.
The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each testcase print $n$ integers — for the $i$-th robot output the time it explodes at if it does and $-1$ otherwise.
-----Examples-----
Input
5
7 12
1 2 3 4 9 10 11
R R L L R R R
2 10
1 6
R R
2 10
1 3
L L
1 10
5
R
7 8
6 1 7 2 3 5 4
R L R L L L L
Output
1 1 1 1 2 -1 2
-1 -1
2 2
-1
-1 2 7 3 2 7 3
-----Note-----
Here is the picture for the seconds $0, 1, 2$ and $3$ of the first testcase:
Notice that robots $2$ and $3$ don't collide because they meet at the same point $2.5$, which is not integer.
After second $3$ robot $6$ just drive infinitely because there's no robot to collide with. | for _ in range(int(input())):
n, m = map(int, input().split())
arr = list(zip(map(int, input().split()), input().split()))
r = [[], []]
for i in range(n):
j = arr[i][0] % 2
r[j].append((arr[i][0], arr[i][1], i))
odd, even = r[1], r[0]
time = [-1] * n
s = []
for c, d, idx in sorted(odd):
if d == "R":
s.append((idx, c))
elif s == []:
s.append((idx, -c))
else:
tidx, tc = s.pop()
time[idx] = (c - tc) // 2
time[tidx] = time[idx]
while len(s) > 1:
idx1, c1 = s.pop()
idx2, c2 = s.pop()
time[idx1] = (2 * m - c1 - c2) // 2
time[idx2] = time[idx1]
s = []
for c, d, idx in sorted(even):
if d == "R":
s.append((idx, c))
elif s == []:
s.append((idx, -c))
else:
tidx, tc = s.pop()
time[idx] = (c - tc) // 2
time[tidx] = time[idx]
while len(s) > 1:
idx1, c1 = s.pop()
idx2, c2 = s.pop()
time[idx1] = (2 * m - c1 - c2) // 2
time[idx2] = time[idx1]
print(" ".join(map(str, time))) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR VAR IF VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR VAR IF VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
There are $n$ robots driving along an OX axis. There are also two walls: one is at coordinate $0$ and one is at coordinate $m$.
The $i$-th robot starts at an integer coordinate $x_i~(0 < x_i < m)$ and moves either left (towards the $0$) or right with the speed of $1$ unit per second. No two robots start at the same coordinate.
Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.
Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.
For each robot find out if it ever explodes and print the time of explosion if it happens and $-1$ otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the descriptions of $t$ testcases follow.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 3 \cdot 10^5$; $2 \le m \le 10^8$) — the number of robots and the coordinate of the right wall.
The second line of each testcase contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 < x_i < m$) — the starting coordinates of the robots.
The third line of each testcase contains $n$ space-separated characters 'L' or 'R' — the starting directions of the robots ('L' stands for left and 'R' stands for right).
All coordinates $x_i$ in the testcase are distinct.
The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each testcase print $n$ integers — for the $i$-th robot output the time it explodes at if it does and $-1$ otherwise.
-----Examples-----
Input
5
7 12
1 2 3 4 9 10 11
R R L L R R R
2 10
1 6
R R
2 10
1 3
L L
1 10
5
R
7 8
6 1 7 2 3 5 4
R L R L L L L
Output
1 1 1 1 2 -1 2
-1 -1
2 2
-1
-1 2 7 3 2 7 3
-----Note-----
Here is the picture for the seconds $0, 1, 2$ and $3$ of the first testcase:
Notice that robots $2$ and $3$ don't collide because they meet at the same point $2.5$, which is not integer.
After second $3$ robot $6$ just drive infinitely because there's no robot to collide with. | def gns():
return list(map(int, input().split()))
def gn():
return int(input())
t = gn()
def ctime(a, b, m):
if a[0] > b[0]:
a, b = b, a
if a[1] == b[1]:
if a[1] == "L":
return a[0] + b[0]
else:
return 2 * m - a[0] - b[0]
elif a[1] == "R":
return b[0] - a[0]
else:
return a[0] + m + m - b[0]
def one_d(ns, ans, m):
ns.sort(key=lambda x: x[0])
if len(ns) == 0:
return
if len(ns) == 1:
ans[ns[0][-1]] = -1
return
tmp = []
for i in range(len(ns)):
if len(tmp) == 0 or ns[i][1] == "R":
tmp.append(ns[i])
continue
ct = ctime(tmp[-1], ns[i], m)
ans[tmp[-1][-1]] = ct // 2
ans[ns[i][-1]] = ct // 2
tmp.pop()
while len(tmp) > 1:
ct = ctime(tmp[-1], tmp[-2], m)
ans[tmp[-1][-1]] = ct // 2
ans[tmp[-2][-1]] = ct // 2
tmp.pop()
tmp.pop()
if len(tmp) == 1:
ans[tmp[0][-1]] = -1
def one():
n, m = gns()
ns = gns()
ds = list(input().split())
ans = [-2] * n
odd = []
evn = []
for i in range(n):
if ns[i] % 2 == 0:
evn.append((ns[i], ds[i], i))
else:
odd.append((ns[i], ds[i], i))
one_d(odd, ans, m)
one_d(evn, ans, m)
return " ".join(map(str, ans))
for i in range(t):
print(one()) | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER STRING RETURN BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER STRING RETURN BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER RETURN ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL 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 IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
There are $n$ robots driving along an OX axis. There are also two walls: one is at coordinate $0$ and one is at coordinate $m$.
The $i$-th robot starts at an integer coordinate $x_i~(0 < x_i < m)$ and moves either left (towards the $0$) or right with the speed of $1$ unit per second. No two robots start at the same coordinate.
Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.
Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.
For each robot find out if it ever explodes and print the time of explosion if it happens and $-1$ otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the descriptions of $t$ testcases follow.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 3 \cdot 10^5$; $2 \le m \le 10^8$) — the number of robots and the coordinate of the right wall.
The second line of each testcase contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 < x_i < m$) — the starting coordinates of the robots.
The third line of each testcase contains $n$ space-separated characters 'L' or 'R' — the starting directions of the robots ('L' stands for left and 'R' stands for right).
All coordinates $x_i$ in the testcase are distinct.
The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each testcase print $n$ integers — for the $i$-th robot output the time it explodes at if it does and $-1$ otherwise.
-----Examples-----
Input
5
7 12
1 2 3 4 9 10 11
R R L L R R R
2 10
1 6
R R
2 10
1 3
L L
1 10
5
R
7 8
6 1 7 2 3 5 4
R L R L L L L
Output
1 1 1 1 2 -1 2
-1 -1
2 2
-1
-1 2 7 3 2 7 3
-----Note-----
Here is the picture for the seconds $0, 1, 2$ and $3$ of the first testcase:
Notice that robots $2$ and $3$ don't collide because they meet at the same point $2.5$, which is not integer.
After second $3$ robot $6$ just drive infinitely because there's no robot to collide with. | import sys
pl = 1
sys.setrecursionlimit(10**5)
if pl:
input = sys.stdin.readline
else:
sys.stdin = open("input.txt", "r")
sys.stdout = open("outpt.txt", "w")
def li():
return [int(xxx) for xxx in input().split()]
def fi():
return int(input())
def si():
return list(input().rstrip())
def mi():
return map(int, input().split())
def ff():
sys.stdout.flush()
t = fi()
while t > 0:
t -= 1
n, m = mi()
a = li()
b = list(input().split())
c = []
d = []
for i in range(n):
if a[i] % 2:
c.append([a[i], b[i], i])
else:
d.append([a[i], b[i], i])
c.sort()
d.sort()
st = []
ans = [-1] * n
for i in range(len(c)):
if len(st) == 0:
st.append(c[i])
elif st[-1][1] == "R" and c[i][1] == "L":
ans[st[-1][2]] = ans[c[i][2]] = abs(c[i][0] - st[-1][0]) // 2
st.pop()
else:
st.append(c[i])
i = 0
p = []
q = []
r = []
for i in range(len(st)):
if st[i][1] == "L":
p.append(st[i])
else:
q.append(st[i])
if len(p) % 2:
r.append(p[-1])
p.pop()
if len(q) % 2:
r.append(q[0])
q.pop(0)
for i in range(0, len(p), 2):
ans[p[i][2]] = ans[p[i + 1][2]] = p[i][0] + abs(p[i][0] - p[i + 1][0]) // 2
for i in range(len(q) - 2, -1, -2):
ans[q[i][2]] = ans[q[i + 1][2]] = (
m - q[i + 1][0] + abs(q[i][0] - q[i + 1][0]) // 2
)
if len(r) > 1:
ans[r[-1][2]] = ans[r[-2][2]] = m - abs(r[0][0] - r[1][0]) // 2
st = []
for i in range(len(d)):
if len(st) == 0:
st.append(d[i])
elif st[-1][1] == "R" and d[i][1] == "L":
ans[st[-1][2]] = ans[d[i][2]] = abs(d[i][0] - st[-1][0]) // 2
st.pop()
else:
st.append(d[i])
p = []
q = []
r = []
for i in range(len(st)):
if st[i][1] == "L":
p.append(st[i])
else:
q.append(st[i])
if len(p) % 2:
r.append(p[-1])
p.pop()
if len(q) % 2:
r.append(q[0])
q.pop(0)
for i in range(0, len(p), 2):
ans[p[i][2]] = ans[p[i + 1][2]] = p[i][0] + abs(p[i][0] - p[i + 1][0]) // 2
for i in range(len(q) - 2, -1, -2):
ans[q[i][2]] = ans[q[i + 1][2]] = (
m - q[i + 1][0] + abs(q[i][0] - q[i + 1][0]) // 2
)
if len(r) > 1:
ans[r[-1][2]] = ans[r[-2][2]] = m - abs(r[0][0] - r[1][0]) // 2
print(*ans) | IMPORT ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER NUMBER STRING VAR VAR NUMBER STRING ASSIGN VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER NUMBER STRING VAR VAR NUMBER STRING ASSIGN VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
There are $n$ robots driving along an OX axis. There are also two walls: one is at coordinate $0$ and one is at coordinate $m$.
The $i$-th robot starts at an integer coordinate $x_i~(0 < x_i < m)$ and moves either left (towards the $0$) or right with the speed of $1$ unit per second. No two robots start at the same coordinate.
Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.
Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.
For each robot find out if it ever explodes and print the time of explosion if it happens and $-1$ otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the descriptions of $t$ testcases follow.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 3 \cdot 10^5$; $2 \le m \le 10^8$) — the number of robots and the coordinate of the right wall.
The second line of each testcase contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 < x_i < m$) — the starting coordinates of the robots.
The third line of each testcase contains $n$ space-separated characters 'L' or 'R' — the starting directions of the robots ('L' stands for left and 'R' stands for right).
All coordinates $x_i$ in the testcase are distinct.
The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each testcase print $n$ integers — for the $i$-th robot output the time it explodes at if it does and $-1$ otherwise.
-----Examples-----
Input
5
7 12
1 2 3 4 9 10 11
R R L L R R R
2 10
1 6
R R
2 10
1 3
L L
1 10
5
R
7 8
6 1 7 2 3 5 4
R L R L L L L
Output
1 1 1 1 2 -1 2
-1 -1
2 2
-1
-1 2 7 3 2 7 3
-----Note-----
Here is the picture for the seconds $0, 1, 2$ and $3$ of the first testcase:
Notice that robots $2$ and $3$ don't collide because they meet at the same point $2.5$, which is not integer.
After second $3$ robot $6$ just drive infinitely because there's no robot to collide with. | for i in range(int(input())):
n, m = list(map(int, input().split()))
x = list(map(int, input().split()))
a = input().split()
z = [-1] * n
b = []
c = []
for i in range(n):
[b, c][x[i] % 2].append([x[i], a[i], i])
d = []
b.sort()
c.sort()
b1 = len(b)
for i in range(b1):
if d and b[i][1] == "L" and d[-1][1] == "R":
z[d[-1][2]] = (b[i][0] - d[-1][0]) // 2
z[b[i][2]] = (b[i][0] - d[-1][0]) // 2
d.pop()
else:
d.append(b[i])
e = -1
for i in d:
if i[1] == "R":
break
if e == -1:
e = list(i)
else:
z[i[2]] = (i[0] + e[0]) // 2
z[e[2]] = (i[0] + e[0]) // 2
e = -1
f = -1
for i in d[::-1]:
if i[1] == "L":
break
if f == -1:
f = list(i)
else:
z[i[2]] = (2 * m - i[0] - f[0]) // 2
z[f[2]] = (2 * m - i[0] - f[0]) // 2
f = -1
if e != -1 and f != -1:
z[e[2]] = (e[0] + m - f[0] + m) // 2
z[f[2]] = (e[0] + m - f[0] + m) // 2
c1 = len(c)
d = []
for i in range(c1):
if d and c[i][1] == "L" and d[-1][1] == "R":
z[d[-1][2]] = (c[i][0] - d[-1][0]) // 2
z[c[i][2]] = (c[i][0] - d[-1][0]) // 2
d.pop()
else:
d.append(c[i])
e = -1
for i in d:
if i[1] == "R":
break
if e == -1:
e = list(i)
else:
z[i[2]] = (i[0] + e[0]) // 2
z[e[2]] = (i[0] + e[0]) // 2
e = -1
f = -1
for i in d[::-1]:
if i[1] == "L":
break
if f == -1:
f = list(i)
else:
z[i[2]] = (2 * m - i[0] - f[0]) // 2
z[f[2]] = (2 * m - i[0] - f[0]) // 2
f = -1
if e != -1 and f != -1:
z[e[2]] = (e[0] + m - f[0] + m) // 2
z[f[2]] = (e[0] + m - f[0] + m) // 2
print(*z) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL LIST VAR VAR BIN_OP VAR VAR NUMBER LIST VAR VAR VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER STRING VAR NUMBER NUMBER STRING ASSIGN VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR NUMBER STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER STRING VAR NUMBER NUMBER STRING ASSIGN VAR VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR NUMBER STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
There are $n$ robots driving along an OX axis. There are also two walls: one is at coordinate $0$ and one is at coordinate $m$.
The $i$-th robot starts at an integer coordinate $x_i~(0 < x_i < m)$ and moves either left (towards the $0$) or right with the speed of $1$ unit per second. No two robots start at the same coordinate.
Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.
Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.
For each robot find out if it ever explodes and print the time of explosion if it happens and $-1$ otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the descriptions of $t$ testcases follow.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 3 \cdot 10^5$; $2 \le m \le 10^8$) — the number of robots and the coordinate of the right wall.
The second line of each testcase contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 < x_i < m$) — the starting coordinates of the robots.
The third line of each testcase contains $n$ space-separated characters 'L' or 'R' — the starting directions of the robots ('L' stands for left and 'R' stands for right).
All coordinates $x_i$ in the testcase are distinct.
The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each testcase print $n$ integers — for the $i$-th robot output the time it explodes at if it does and $-1$ otherwise.
-----Examples-----
Input
5
7 12
1 2 3 4 9 10 11
R R L L R R R
2 10
1 6
R R
2 10
1 3
L L
1 10
5
R
7 8
6 1 7 2 3 5 4
R L R L L L L
Output
1 1 1 1 2 -1 2
-1 -1
2 2
-1
-1 2 7 3 2 7 3
-----Note-----
Here is the picture for the seconds $0, 1, 2$ and $3$ of the first testcase:
Notice that robots $2$ and $3$ don't collide because they meet at the same point $2.5$, which is not integer.
After second $3$ robot $6$ just drive infinitely because there's no robot to collide with. | def partner(robots):
robots.sort()
partnerships = []
stack = []
for robot in robots:
if len(stack) == 0 or robot[1] == "R":
stack.append(robot)
else:
poppedRobot = stack.pop()
partnerships.append((poppedRobot, robot))
while len(stack) > 1:
poppedRobot1 = stack.pop()
poppedRobot2 = stack.pop()
partnerships.append((poppedRobot2, poppedRobot1))
return partnerships
NoOfTestCases = int(input())
for t in range(NoOfTestCases):
n, m = [int(i) for i in input().strip().split(" ")]
co_ordinates = [int(x) for x in input().strip().split(" ")]
directions = [x for x in input().split(" ")]
robots = list(zip(co_ordinates, directions, range(n)))
odd = []
even = []
for robot in robots:
if robot[0] % 2 == 0:
even.append(robot)
else:
odd.append(robot)
partnerships = partner(even) + partner(odd)
distances = [-1] * n
for a, b in partnerships:
if a[1] == "R" and b[1] == "L":
distance = (b[0] - a[0]) // 2
elif a[1] == "L" and b[1] == "L":
distance = (b[0] + a[0]) // 2
elif a[1] == "R" and b[1] == "R":
distance = (2 * m - (a[0] + b[0])) // 2
else:
distance = (2 * m + a[0] - b[0]) // 2
distances[a[2]] = distance
distances[b[2]] = distance
print(*distances) | FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR IF VAR NUMBER STRING VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER STRING VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER STRING VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
There are $n$ robots driving along an OX axis. There are also two walls: one is at coordinate $0$ and one is at coordinate $m$.
The $i$-th robot starts at an integer coordinate $x_i~(0 < x_i < m)$ and moves either left (towards the $0$) or right with the speed of $1$ unit per second. No two robots start at the same coordinate.
Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.
Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.
For each robot find out if it ever explodes and print the time of explosion if it happens and $-1$ otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the descriptions of $t$ testcases follow.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 3 \cdot 10^5$; $2 \le m \le 10^8$) — the number of robots and the coordinate of the right wall.
The second line of each testcase contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 < x_i < m$) — the starting coordinates of the robots.
The third line of each testcase contains $n$ space-separated characters 'L' or 'R' — the starting directions of the robots ('L' stands for left and 'R' stands for right).
All coordinates $x_i$ in the testcase are distinct.
The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each testcase print $n$ integers — for the $i$-th robot output the time it explodes at if it does and $-1$ otherwise.
-----Examples-----
Input
5
7 12
1 2 3 4 9 10 11
R R L L R R R
2 10
1 6
R R
2 10
1 3
L L
1 10
5
R
7 8
6 1 7 2 3 5 4
R L R L L L L
Output
1 1 1 1 2 -1 2
-1 -1
2 2
-1
-1 2 7 3 2 7 3
-----Note-----
Here is the picture for the seconds $0, 1, 2$ and $3$ of the first testcase:
Notice that robots $2$ and $3$ don't collide because they meet at the same point $2.5$, which is not integer.
After second $3$ robot $6$ just drive infinitely because there's no robot to collide with. | import sys
input = sys.stdin.readline
def solve(N, M, X, D):
for i in range(len(X)):
X[i] = X[i], D[i], i
X.sort()
ans = [(-1) for _ in range(N)]
prevRight = [[] for _ in range(2)]
for i in range(len(X)):
x, d, idx = X[i]
if d == 1:
prevRight[x % 2].append((idx, x))
elif prevRight[x % 2]:
colliderIdx, colliderPos = prevRight[x % 2][-1]
prevRight[x % 2].pop()
dist = x - colliderPos
ans[colliderIdx] = dist // 2
ans[idx] = dist // 2
prevLeft = [[] for _ in range(2)]
for i in range(len(X) - 1, -1, -1):
x, d, idx = X[i]
if ans[idx] != -1:
continue
if d == -1:
prevLeft[x % 2].append((idx, x))
elif prevLeft[x % 2]:
colliderIdx, colliderPos = prevLeft[x % 2][-1]
prevLeft[x % 2].pop()
dist = colliderPos - x
ans[colliderIdx] = dist // 2
ans[idx] = dist // 2
prevLeft = [[] for _ in range(2)]
for i in range(len(X)):
x, d, idx = X[i]
if ans[idx] != -1:
continue
if d == -1:
if prevLeft[x % 2]:
colliderIdx, colliderPos = prevLeft[x % 2][-1]
prevLeft[x % 2].pop()
dist = x - colliderPos + colliderPos * 2
ans[colliderIdx] = ans[idx] = dist // 2
else:
prevLeft[x % 2].append((idx, x))
prevRight = [[] for _ in range(2)]
for i in range(len(X) - 1, -1, -1):
x, d, idx = X[i]
if ans[idx] != -1:
continue
if d == 1:
if prevRight[x % 2]:
colliderIdx, colliderPos = prevRight[x % 2][-1]
prevRight[x % 2].pop()
dist = colliderPos - x + (M - colliderPos) * 2
ans[colliderIdx] = dist // 2
ans[idx] = dist // 2
else:
prevRight[x % 2].append((idx, x))
prevLeft = [[] for _ in range(2)]
for i in range(len(X)):
x, d, idx = X[i]
if ans[idx] != -1:
continue
if d == -1:
prevLeft[x % 2].append((idx, x))
for x in prevLeft:
x.reverse()
for i in range(len(X) - 1, -1, -1):
x, d, idx = X[i]
if ans[idx] != -1:
continue
if d == 1:
if prevLeft[x % 2]:
colliderIdx, colliderPos = prevLeft[x % 2][-1]
prevLeft[x % 2].pop()
dist = x - colliderPos + colliderPos * 2 + (M - x) * 2
ans[idx] = ans[colliderIdx] = dist // 2
print(*ans)
T = int(input())
for _ in range(T):
N, M = map(int, input().split())
X = list(map(int, input().split()))
D = list(map(lambda x: -1 if x == "L" else 1, input().split()))
solve(N, M, X, D) | IMPORT ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING NUMBER NUMBER FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR |
There are $n$ robots driving along an OX axis. There are also two walls: one is at coordinate $0$ and one is at coordinate $m$.
The $i$-th robot starts at an integer coordinate $x_i~(0 < x_i < m)$ and moves either left (towards the $0$) or right with the speed of $1$ unit per second. No two robots start at the same coordinate.
Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.
Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.
For each robot find out if it ever explodes and print the time of explosion if it happens and $-1$ otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the descriptions of $t$ testcases follow.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 3 \cdot 10^5$; $2 \le m \le 10^8$) — the number of robots and the coordinate of the right wall.
The second line of each testcase contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 < x_i < m$) — the starting coordinates of the robots.
The third line of each testcase contains $n$ space-separated characters 'L' or 'R' — the starting directions of the robots ('L' stands for left and 'R' stands for right).
All coordinates $x_i$ in the testcase are distinct.
The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each testcase print $n$ integers — for the $i$-th robot output the time it explodes at if it does and $-1$ otherwise.
-----Examples-----
Input
5
7 12
1 2 3 4 9 10 11
R R L L R R R
2 10
1 6
R R
2 10
1 3
L L
1 10
5
R
7 8
6 1 7 2 3 5 4
R L R L L L L
Output
1 1 1 1 2 -1 2
-1 -1
2 2
-1
-1 2 7 3 2 7 3
-----Note-----
Here is the picture for the seconds $0, 1, 2$ and $3$ of the first testcase:
Notice that robots $2$ and $3$ don't collide because they meet at the same point $2.5$, which is not integer.
After second $3$ robot $6$ just drive infinitely because there's no robot to collide with. | import sys
from sys import stdin
tt = int(stdin.readline())
for loop in range(tt):
n, m = map(int, stdin.readline().split())
xtmp = list(map(int, stdin.readline().split()))
stmp = list(stdin.readline().split())
xs = [(xtmp[i], stmp[i], i) for i in range(n)]
xs.sort()
x = [xs[i][0] for i in range(n)]
s = [xs[i][1] for i in range(n)]
ind = [xs[i][2] for i in range(n)]
ans = [-1] * n
ostk = []
estk = []
for i in range(n):
if x[i] % 2 == 1:
if s[i] == "R":
estk.append((x[i], s[i], ind[i]))
elif len(estk) > 0 and estk[-1][1] == "R":
lx, ls, li = estk[-1]
del estk[-1]
rx, rs, ri = x[i], s[i], ind[i]
ans[li] = ans[ri] = abs(rx - lx) // 2
else:
estk.append((x[i], s[i], ind[i]))
elif s[i] == "R":
ostk.append((x[i], s[i], ind[i]))
elif len(ostk) > 0 and ostk[-1][1] == "R":
lx, ls, li = ostk[-1]
del ostk[-1]
rx, rs, ri = x[i], s[i], ind[i]
ans[li] = ans[ri] = abs(rx - lx) // 2
else:
ostk.append((x[i], s[i], ind[i]))
OL = []
OR = []
for i in ostk:
if i[1] == "L":
OL.append(i)
else:
OR.append(i)
OL.reverse()
while len(OL) >= 2:
lx, ls, li = OL[-1]
del OL[-1]
rx, rs, ri = OL[-1]
del OL[-1]
ans[li] = ans[ri] = (lx + rx) // 2
while len(OR) >= 2:
lx, ls, li = OR[-1]
del OR[-1]
rx, rs, ri = OR[-1]
del OR[-1]
ans[li] = ans[ri] = (m - lx + (m - rx)) // 2
EL = []
ER = []
for i in estk:
if i[1] == "L":
EL.append(i)
else:
ER.append(i)
EL.reverse()
while len(EL) >= 2:
lx, ls, li = EL[-1]
del EL[-1]
rx, rs, ri = EL[-1]
del EL[-1]
ans[li] = ans[ri] = (lx + rx) // 2
while len(ER) >= 2:
lx, ls, li = ER[-1]
del ER[-1]
rx, rs, ri = ER[-1]
del ER[-1]
ans[li] = ans[ri] = (m - lx + (m - rx)) // 2
if True:
if len(OL) > 0 and len(OR) > 0:
lx, ls, li = OL[0]
rx, rs, ri = OR[0]
ans[li] = ans[ri] = (lx + m + (m - rx)) // 2
if len(EL) > 0 and len(ER) > 0:
lx, ls, li = EL[0]
rx, rs, ri = ER[0]
ans[li] = ans[ri] = (lx + m + (m - rx)) // 2
print(*ans) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER STRING ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER STRING ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER IF NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
There are $n$ robots driving along an OX axis. There are also two walls: one is at coordinate $0$ and one is at coordinate $m$.
The $i$-th robot starts at an integer coordinate $x_i~(0 < x_i < m)$ and moves either left (towards the $0$) or right with the speed of $1$ unit per second. No two robots start at the same coordinate.
Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.
Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.
For each robot find out if it ever explodes and print the time of explosion if it happens and $-1$ otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the descriptions of $t$ testcases follow.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 3 \cdot 10^5$; $2 \le m \le 10^8$) — the number of robots and the coordinate of the right wall.
The second line of each testcase contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 < x_i < m$) — the starting coordinates of the robots.
The third line of each testcase contains $n$ space-separated characters 'L' or 'R' — the starting directions of the robots ('L' stands for left and 'R' stands for right).
All coordinates $x_i$ in the testcase are distinct.
The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each testcase print $n$ integers — for the $i$-th robot output the time it explodes at if it does and $-1$ otherwise.
-----Examples-----
Input
5
7 12
1 2 3 4 9 10 11
R R L L R R R
2 10
1 6
R R
2 10
1 3
L L
1 10
5
R
7 8
6 1 7 2 3 5 4
R L R L L L L
Output
1 1 1 1 2 -1 2
-1 -1
2 2
-1
-1 2 7 3 2 7 3
-----Note-----
Here is the picture for the seconds $0, 1, 2$ and $3$ of the first testcase:
Notice that robots $2$ and $3$ don't collide because they meet at the same point $2.5$, which is not integer.
After second $3$ robot $6$ just drive infinitely because there's no robot to collide with. | def main():
N, M = map(int, input().split())
A = list(map(int, input().split()))
LR = input().split()
Odd = []
Even = []
Index = {}
Ans = [-1] * N
for i in range(N):
Index[A[i]] = i
if A[i] % 2:
Odd.append((A[i], 0 if LR[i] == "R" else 1))
else:
Even.append((A[i], 0 if LR[i] == "R" else 1))
Stack = []
Odd.sort()
for X, D in Odd:
if D:
if Stack:
X2 = Stack.pop()
Time = (X - X2) // 2
Ans[Index[X]] = Time
Ans[Index[abs(X2)]] = Time
else:
Stack.append(-X)
else:
Stack.append(X)
while len(Stack) >= 2:
X = Stack.pop()
X2 = Stack.pop()
Time = (M + M - X - X2) // 2
Ans[Index[X]] = Time
Ans[Index[abs(X2)]] = Time
Stack = []
Even.sort()
for X, D in Even:
if D:
if Stack:
X2 = Stack.pop()
Time = (X - X2) // 2
Ans[Index[X]] = Time
Ans[Index[abs(X2)]] = Time
else:
Stack.append(-X)
else:
Stack.append(X)
while len(Stack) >= 2:
X = Stack.pop()
X2 = Stack.pop()
Time = (M + M - X - X2) // 2
Ans[Index[X]] = Time
Ans[Index[abs(X2)]] = Time
print(*Ans)
T = int(input())
for _ in range(T):
main() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR STRING NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR STRING NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR FOR VAR VAR VAR IF VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FOR VAR VAR VAR IF VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
There are $n$ robots driving along an OX axis. There are also two walls: one is at coordinate $0$ and one is at coordinate $m$.
The $i$-th robot starts at an integer coordinate $x_i~(0 < x_i < m)$ and moves either left (towards the $0$) or right with the speed of $1$ unit per second. No two robots start at the same coordinate.
Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.
Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.
For each robot find out if it ever explodes and print the time of explosion if it happens and $-1$ otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the descriptions of $t$ testcases follow.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 3 \cdot 10^5$; $2 \le m \le 10^8$) — the number of robots and the coordinate of the right wall.
The second line of each testcase contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 < x_i < m$) — the starting coordinates of the robots.
The third line of each testcase contains $n$ space-separated characters 'L' or 'R' — the starting directions of the robots ('L' stands for left and 'R' stands for right).
All coordinates $x_i$ in the testcase are distinct.
The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each testcase print $n$ integers — for the $i$-th robot output the time it explodes at if it does and $-1$ otherwise.
-----Examples-----
Input
5
7 12
1 2 3 4 9 10 11
R R L L R R R
2 10
1 6
R R
2 10
1 3
L L
1 10
5
R
7 8
6 1 7 2 3 5 4
R L R L L L L
Output
1 1 1 1 2 -1 2
-1 -1
2 2
-1
-1 2 7 3 2 7 3
-----Note-----
Here is the picture for the seconds $0, 1, 2$ and $3$ of the first testcase:
Notice that robots $2$ and $3$ don't collide because they meet at the same point $2.5$, which is not integer.
After second $3$ robot $6$ just drive infinitely because there's no robot to collide with. | t = int(input())
for casen in range(t):
n, m = map(int, input().split())
x = list(map(int, input().split()))
lr = input().split()
xlr = [[x[i], lr[i], i] for i in range(len(x))]
maps = [(0) for i in range(len(xlr))]
xlr.sort(key=lambda x: x[0])
for i in range(len(xlr)):
maps[xlr[i][2]] = i
unmatched = [[], []]
out = [(-1) for i in range(len(xlr))]
for i in range(len(xlr)):
parity = xlr[i][0] % 2
if xlr[i][1] == "R":
unmatched[parity].append(i)
elif len(unmatched[parity]) > 0:
pair = unmatched[parity][-1]
unmatched[parity].pop()
if xlr[pair][1] == "R":
out[i] = (xlr[i][0] - xlr[pair][0]) // 2
out[pair] = out[i]
else:
out[i] = xlr[pair][0] + (xlr[i][0] - xlr[pair][0]) // 2
out[pair] = out[i]
else:
unmatched[parity].append(i)
unmatched2 = [[], []]
for p in range(2):
for i in range(len(unmatched[p]) - 1, -1, -1):
if len(unmatched2[p]) == 0:
unmatched2[p].append(unmatched[p][i])
else:
cur = unmatched[p][i]
prev = unmatched2[p][-1]
unmatched2[p].pop()
if xlr[cur][1] == "L" and xlr[prev][1] == "R":
out[cur] = (xlr[cur][0] + (m - xlr[prev][0]) + m) // 2
out[prev] = out[cur]
else:
out[cur] = m - xlr[prev][0] + (xlr[prev][0] - xlr[cur][0]) // 2
out[prev] = out[cur]
out2 = [out[maps[i]] for i in range(len(xlr))]
print(" ".join(list(map(str, out2)))) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR LIST LIST LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST LIST LIST FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING VAR VAR NUMBER STRING ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
There are $n$ robots driving along an OX axis. There are also two walls: one is at coordinate $0$ and one is at coordinate $m$.
The $i$-th robot starts at an integer coordinate $x_i~(0 < x_i < m)$ and moves either left (towards the $0$) or right with the speed of $1$ unit per second. No two robots start at the same coordinate.
Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.
Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.
For each robot find out if it ever explodes and print the time of explosion if it happens and $-1$ otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the descriptions of $t$ testcases follow.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 3 \cdot 10^5$; $2 \le m \le 10^8$) — the number of robots and the coordinate of the right wall.
The second line of each testcase contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 < x_i < m$) — the starting coordinates of the robots.
The third line of each testcase contains $n$ space-separated characters 'L' or 'R' — the starting directions of the robots ('L' stands for left and 'R' stands for right).
All coordinates $x_i$ in the testcase are distinct.
The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each testcase print $n$ integers — for the $i$-th robot output the time it explodes at if it does and $-1$ otherwise.
-----Examples-----
Input
5
7 12
1 2 3 4 9 10 11
R R L L R R R
2 10
1 6
R R
2 10
1 3
L L
1 10
5
R
7 8
6 1 7 2 3 5 4
R L R L L L L
Output
1 1 1 1 2 -1 2
-1 -1
2 2
-1
-1 2 7 3 2 7 3
-----Note-----
Here is the picture for the seconds $0, 1, 2$ and $3$ of the first testcase:
Notice that robots $2$ and $3$ don't collide because they meet at the same point $2.5$, which is not integer.
After second $3$ robot $6$ just drive infinitely because there's no robot to collide with. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n, m = map(int, input().split())
time = [-1] * n
x = list(map(int, input().split()))
dir = input().split()
a = sorted(list(zip(x, dir, list(range(n)))))
x, dir, idx = map(list, list(zip(*a)))
s = []
s2 = []
for i in range(n):
if x[i] & 1:
if dir[i] == "R":
s.append(i)
elif s:
j = s.pop()
time[j] = time[i] = abs(x[j] - x[i]) // 2
elif dir[i] == "R":
s2.append(i)
elif s2:
j = s2.pop()
time[j] = time[i] = abs(x[j] - x[i]) // 2
s = []
s2 = []
for i in range(n):
if time[i] == -1 and dir[i] == "L":
if x[i] & 1:
if s:
j = s.pop()
time[j] = time[i] = abs(x[j] - x[i]) // 2 + x[j]
else:
s.append(i)
elif s2:
j = s2.pop()
time[j] = time[i] = abs(x[j] - x[i]) // 2 + x[j]
else:
s2.append(i)
s = []
s2 = []
for i in range(n - 1, -1, -1):
if time[i] == -1 and dir[i] == "R":
if x[i] & 1:
if s:
j = s.pop()
time[j] = time[i] = abs(x[j] - x[i]) // 2 + m - x[j]
else:
s.append(i)
elif s2:
j = s2.pop()
time[j] = time[i] = abs(x[j] - x[i]) // 2 + m - x[j]
else:
s2.append(i)
L_loc = R_loc = -1
L_loc2 = R_loc2 = -1
for i in range(n):
if x[i] & 1:
if time[i] == -1 and dir[i] == "L":
L_loc = i
elif time[i] == -1 and dir[i] == "L":
L_loc2 = i
for i in range(n - 1, -1, -1):
if x[i] & 1:
if time[i] == -1 and dir[i] == "R":
R_loc = i
elif time[i] == -1 and dir[i] == "R":
R_loc2 = i
if L_loc != -1 and R_loc != -1:
time[L_loc] = time[R_loc] = (2 * m - (x[R_loc] - x[L_loc])) // 2
if L_loc2 != -1 and R_loc2 != -1:
time[L_loc2] = time[R_loc2] = (2 * m - (x[R_loc2] - x[L_loc2])) // 2
true_time = [0] * n
for i in range(n):
true_time[idx[i]] = time[i]
print(*true_time) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR STRING IF BIN_OP VAR VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR STRING IF BIN_OP VAR VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR STRING ASSIGN VAR VAR IF VAR VAR NUMBER VAR VAR STRING ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR STRING ASSIGN VAR VAR IF VAR VAR NUMBER VAR VAR STRING ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
There are $n$ robots driving along an OX axis. There are also two walls: one is at coordinate $0$ and one is at coordinate $m$.
The $i$-th robot starts at an integer coordinate $x_i~(0 < x_i < m)$ and moves either left (towards the $0$) or right with the speed of $1$ unit per second. No two robots start at the same coordinate.
Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.
Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.
For each robot find out if it ever explodes and print the time of explosion if it happens and $-1$ otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the descriptions of $t$ testcases follow.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 3 \cdot 10^5$; $2 \le m \le 10^8$) — the number of robots and the coordinate of the right wall.
The second line of each testcase contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 < x_i < m$) — the starting coordinates of the robots.
The third line of each testcase contains $n$ space-separated characters 'L' or 'R' — the starting directions of the robots ('L' stands for left and 'R' stands for right).
All coordinates $x_i$ in the testcase are distinct.
The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each testcase print $n$ integers — for the $i$-th robot output the time it explodes at if it does and $-1$ otherwise.
-----Examples-----
Input
5
7 12
1 2 3 4 9 10 11
R R L L R R R
2 10
1 6
R R
2 10
1 3
L L
1 10
5
R
7 8
6 1 7 2 3 5 4
R L R L L L L
Output
1 1 1 1 2 -1 2
-1 -1
2 2
-1
-1 2 7 3 2 7 3
-----Note-----
Here is the picture for the seconds $0, 1, 2$ and $3$ of the first testcase:
Notice that robots $2$ and $3$ don't collide because they meet at the same point $2.5$, which is not integer.
After second $3$ robot $6$ just drive infinitely because there's no robot to collide with. | t = int(input())
for _ in range(t):
n, m = map(int, input().split())
X = list(map(int, input().split()))
S = list(map(str, input().split()))
even = []
odd = []
for i in range(n):
if X[i] % 2 == 0:
even.append((X[i], S[i], i))
else:
odd.append((X[i], S[i], i))
even.sort()
odd.sort()
ret = [-1] * n
stack = []
for x, s, idx in even:
if s == "R":
stack.append((x, idx))
elif not stack:
stack.append((-x, idx))
else:
col, pre_idx = stack.pop()
time = abs(x - col) // 2
ret[pre_idx] = time
ret[idx] = time
while len(stack) >= 2:
first, f_idx = stack.pop()
second, s_idx = stack.pop()
time = ((m - first) * 2 + (first - second)) // 2
ret[f_idx] = time
ret[s_idx] = time
stack = []
for x, s, idx in odd:
if s == "R":
stack.append((x, idx))
elif not stack:
stack.append((-x, idx))
else:
col, pre_idx = stack.pop()
time = abs(x - col) // 2
ret[pre_idx] = time
ret[idx] = time
while len(stack) >= 2:
first, f_idx = stack.pop()
second, s_idx = stack.pop()
time = ((m - first) * 2 + (first - second)) // 2
ret[f_idx] = time
ret[s_idx] = time
print(*ret) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR |
There are $n$ robots driving along an OX axis. There are also two walls: one is at coordinate $0$ and one is at coordinate $m$.
The $i$-th robot starts at an integer coordinate $x_i~(0 < x_i < m)$ and moves either left (towards the $0$) or right with the speed of $1$ unit per second. No two robots start at the same coordinate.
Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.
Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.
For each robot find out if it ever explodes and print the time of explosion if it happens and $-1$ otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the descriptions of $t$ testcases follow.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 3 \cdot 10^5$; $2 \le m \le 10^8$) — the number of robots and the coordinate of the right wall.
The second line of each testcase contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 < x_i < m$) — the starting coordinates of the robots.
The third line of each testcase contains $n$ space-separated characters 'L' or 'R' — the starting directions of the robots ('L' stands for left and 'R' stands for right).
All coordinates $x_i$ in the testcase are distinct.
The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each testcase print $n$ integers — for the $i$-th robot output the time it explodes at if it does and $-1$ otherwise.
-----Examples-----
Input
5
7 12
1 2 3 4 9 10 11
R R L L R R R
2 10
1 6
R R
2 10
1 3
L L
1 10
5
R
7 8
6 1 7 2 3 5 4
R L R L L L L
Output
1 1 1 1 2 -1 2
-1 -1
2 2
-1
-1 2 7 3 2 7 3
-----Note-----
Here is the picture for the seconds $0, 1, 2$ and $3$ of the first testcase:
Notice that robots $2$ and $3$ don't collide because they meet at the same point $2.5$, which is not integer.
After second $3$ robot $6$ just drive infinitely because there's no robot to collide with. | import sys
def check(arr):
arr.sort(key=lambda x: x[1])
stack = []
for idx, pos, way in arr:
if way == "L":
if not stack:
stack.append((idx, -pos))
else:
temp = stack.pop()
ans[idx] = ans[temp[0]] = (pos - temp[1]) // 2
else:
stack.append((idx, pos))
while len(stack) > 1:
fir = stack.pop()
sec = stack.pop()
ans[fir[0]] = ans[sec[0]] = (m + m - fir[1] - sec[1]) // 2
t = int(sys.stdin.readline())
for i in range(t):
n, m = map(int, sys.stdin.readline().split())
start = list(map(int, sys.stdin.readline().split()))
direction = list(sys.stdin.readline().split())
data = [[], []]
ans = [-1] * len(start)
for j in range(len(start)):
data[start[j] % 2].append((j, start[j], direction[j]))
check(data[0])
check(data[1])
print(*ans) | IMPORT FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR IF VAR STRING IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST LIST ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
There are $n$ robots driving along an OX axis. There are also two walls: one is at coordinate $0$ and one is at coordinate $m$.
The $i$-th robot starts at an integer coordinate $x_i~(0 < x_i < m)$ and moves either left (towards the $0$) or right with the speed of $1$ unit per second. No two robots start at the same coordinate.
Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.
Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.
For each robot find out if it ever explodes and print the time of explosion if it happens and $-1$ otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the descriptions of $t$ testcases follow.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 3 \cdot 10^5$; $2 \le m \le 10^8$) — the number of robots and the coordinate of the right wall.
The second line of each testcase contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 < x_i < m$) — the starting coordinates of the robots.
The third line of each testcase contains $n$ space-separated characters 'L' or 'R' — the starting directions of the robots ('L' stands for left and 'R' stands for right).
All coordinates $x_i$ in the testcase are distinct.
The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each testcase print $n$ integers — for the $i$-th robot output the time it explodes at if it does and $-1$ otherwise.
-----Examples-----
Input
5
7 12
1 2 3 4 9 10 11
R R L L R R R
2 10
1 6
R R
2 10
1 3
L L
1 10
5
R
7 8
6 1 7 2 3 5 4
R L R L L L L
Output
1 1 1 1 2 -1 2
-1 -1
2 2
-1
-1 2 7 3 2 7 3
-----Note-----
Here is the picture for the seconds $0, 1, 2$ and $3$ of the first testcase:
Notice that robots $2$ and $3$ don't collide because they meet at the same point $2.5$, which is not integer.
After second $3$ robot $6$ just drive infinitely because there's no robot to collide with. | for _ in range(int(input())):
n, m = map(int, input().split())
a = [int(x) for x in input().split()]
d = [x for x in input().split()]
a1, a2 = [], []
for i in range(n):
if a[i] % 2 == 0:
a2.append(i)
else:
a1.append(i)
ans = [(-1) for i in range(n)]
a1 = sorted(a1, key=lambda x: a[x])
a2 = sorted(a2, key=lambda x: a[x])
s1, s2 = [], []
for i in a2:
if d[i] == "R":
s1.append(i)
elif not s1:
s2.append(i)
else:
k = s1.pop()
ans[i] = ans[k] = (a[i] - a[k]) // 2
while len(s1) > 1:
x1 = s1.pop()
x2 = s1.pop()
ans[x1] = ans[x2] = (a[x1] - a[x2]) // 2 + (m - a[x1])
z = 0
while z < len(s2) - 1:
x1 = s2[z + 1]
x2 = s2[z]
ans[x1] = ans[x2] = (a[x1] - a[x2]) // 2 + a[x2]
z += 2
if len(s1) % 2 == 1 and len(s2) % 2 == 1:
k1 = s1.pop()
k2 = s2[-1]
if m - a[k1] > a[k2]:
x1 = m - a[k1]
x2 = a[k2]
else:
x1 = a[k2]
x2 = m - a[k1]
ans[k1] = ans[k2] = x1 + (m - x1 + x2) // 2
s1, s2 = [], []
for i in a1:
if d[i] == "R":
s1.append(i)
elif not s1:
s2.append(i)
else:
k = s1.pop()
ans[i] = ans[k] = (a[i] - a[k]) // 2
while len(s1) > 1:
x1 = s1.pop()
x2 = s1.pop()
ans[x1] = ans[x2] = (a[x1] - a[x2]) // 2 + (m - a[x1])
z = 0
while z < len(s2) - 1:
x1 = s2[z + 1]
x2 = s2[z]
ans[x1] = ans[x2] = (a[x1] - a[x2]) // 2 + a[x2]
z += 2
if len(s1) % 2 == 1 and len(s2) % 2 == 1:
k1 = s1.pop()
k2 = s2[-1]
if m - a[k1] > a[k2]:
x1 = m - a[k1]
x2 = a[k2]
else:
x1 = a[k2]
x2 = m - a[k1]
ans[k1] = ans[k2] = x1 + (m - x1 + x2) // 2
for i in ans:
print(i, end=" ")
print() | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR LIST LIST FOR VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
There are $n$ robots driving along an OX axis. There are also two walls: one is at coordinate $0$ and one is at coordinate $m$.
The $i$-th robot starts at an integer coordinate $x_i~(0 < x_i < m)$ and moves either left (towards the $0$) or right with the speed of $1$ unit per second. No two robots start at the same coordinate.
Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.
Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.
For each robot find out if it ever explodes and print the time of explosion if it happens and $-1$ otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the descriptions of $t$ testcases follow.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 3 \cdot 10^5$; $2 \le m \le 10^8$) — the number of robots and the coordinate of the right wall.
The second line of each testcase contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 < x_i < m$) — the starting coordinates of the robots.
The third line of each testcase contains $n$ space-separated characters 'L' or 'R' — the starting directions of the robots ('L' stands for left and 'R' stands for right).
All coordinates $x_i$ in the testcase are distinct.
The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each testcase print $n$ integers — for the $i$-th robot output the time it explodes at if it does and $-1$ otherwise.
-----Examples-----
Input
5
7 12
1 2 3 4 9 10 11
R R L L R R R
2 10
1 6
R R
2 10
1 3
L L
1 10
5
R
7 8
6 1 7 2 3 5 4
R L R L L L L
Output
1 1 1 1 2 -1 2
-1 -1
2 2
-1
-1 2 7 3 2 7 3
-----Note-----
Here is the picture for the seconds $0, 1, 2$ and $3$ of the first testcase:
Notice that robots $2$ and $3$ don't collide because they meet at the same point $2.5$, which is not integer.
After second $3$ robot $6$ just drive infinitely because there's no robot to collide with. | import sys
input = sys.stdin.readline
def solve(N, M, X, D):
for i in range(len(X)):
X[i] = X[i], D[i], i
X.sort()
ans = [(-1) for _ in range(N)]
stk = [[] for _ in range(2)]
for i, (x, d, idx) in enumerate(X):
parity = x % 2
if d == 1:
stk[parity].append((x, idx))
elif stk[parity]:
collider = stk[parity][-1]
stk[parity].pop()
dist = (x - collider[0]) // 2
ans[idx] = ans[collider[1]] = dist
else:
stk[parity].append((-x, idx))
for i in range(2):
while len(stk[i]) > 1:
first = stk[i][-1]
stk[i].pop()
second = stk[i][-1]
stk[i].pop()
dist = (M * 2 - first[0] - second[0]) // 2
ans[first[1]] = ans[second[1]] = dist
print(*ans)
T = int(input())
for _ in range(T):
N, M = map(int, input().split())
X = list(map(int, input().split()))
D = list(map(lambda x: -1 if x == "L" else 1, input().split()))
solve(N, M, X, D) | IMPORT ASSIGN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER WHILE FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING NUMBER NUMBER FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR |
There are $n$ robots driving along an OX axis. There are also two walls: one is at coordinate $0$ and one is at coordinate $m$.
The $i$-th robot starts at an integer coordinate $x_i~(0 < x_i < m)$ and moves either left (towards the $0$) or right with the speed of $1$ unit per second. No two robots start at the same coordinate.
Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.
Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.
For each robot find out if it ever explodes and print the time of explosion if it happens and $-1$ otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the descriptions of $t$ testcases follow.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 3 \cdot 10^5$; $2 \le m \le 10^8$) — the number of robots and the coordinate of the right wall.
The second line of each testcase contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 < x_i < m$) — the starting coordinates of the robots.
The third line of each testcase contains $n$ space-separated characters 'L' or 'R' — the starting directions of the robots ('L' stands for left and 'R' stands for right).
All coordinates $x_i$ in the testcase are distinct.
The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each testcase print $n$ integers — for the $i$-th robot output the time it explodes at if it does and $-1$ otherwise.
-----Examples-----
Input
5
7 12
1 2 3 4 9 10 11
R R L L R R R
2 10
1 6
R R
2 10
1 3
L L
1 10
5
R
7 8
6 1 7 2 3 5 4
R L R L L L L
Output
1 1 1 1 2 -1 2
-1 -1
2 2
-1
-1 2 7 3 2 7 3
-----Note-----
Here is the picture for the seconds $0, 1, 2$ and $3$ of the first testcase:
Notice that robots $2$ and $3$ don't collide because they meet at the same point $2.5$, which is not integer.
After second $3$ robot $6$ just drive infinitely because there's no robot to collide with. | import sys
mod = 1000000007
eps = 10**-9
def main():
import sys
input = sys.stdin.readline
def solve(info, ans, M):
st = []
N = len(info)
for i in range(N):
x, j, d = info[i]
if d == "L":
if st:
x0, j0 = st.pop()
ans[j] = ans[j0] = abs(x - x0) // 2
else:
st.append((-x, j))
else:
st.append((x, j))
while len(st) > 1:
x1, j1 = st.pop()
x1 = M + (M - x1)
x0, j0 = st.pop()
ans[j0] = ans[j1] = abs(x1 - x0) // 2
for _ in range(int(input())):
N, M = map(int, input().split())
X = list(map(int, input().split()))
LR = list(input().split())
even = []
odd = []
for i, x in enumerate(X):
if x & 1:
odd.append((x, i, LR[i]))
else:
even.append((x, i, LR[i]))
odd.sort(key=lambda y: y[0])
even.sort(key=lambda y: y[0])
ans = [-1] * N
solve(odd, ans, M)
solve(even, ans, M)
print(*ans)
main() | IMPORT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR STRING IF VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
There are $n$ robots driving along an OX axis. There are also two walls: one is at coordinate $0$ and one is at coordinate $m$.
The $i$-th robot starts at an integer coordinate $x_i~(0 < x_i < m)$ and moves either left (towards the $0$) or right with the speed of $1$ unit per second. No two robots start at the same coordinate.
Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.
Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.
For each robot find out if it ever explodes and print the time of explosion if it happens and $-1$ otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the descriptions of $t$ testcases follow.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 3 \cdot 10^5$; $2 \le m \le 10^8$) — the number of robots and the coordinate of the right wall.
The second line of each testcase contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 < x_i < m$) — the starting coordinates of the robots.
The third line of each testcase contains $n$ space-separated characters 'L' or 'R' — the starting directions of the robots ('L' stands for left and 'R' stands for right).
All coordinates $x_i$ in the testcase are distinct.
The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each testcase print $n$ integers — for the $i$-th robot output the time it explodes at if it does and $-1$ otherwise.
-----Examples-----
Input
5
7 12
1 2 3 4 9 10 11
R R L L R R R
2 10
1 6
R R
2 10
1 3
L L
1 10
5
R
7 8
6 1 7 2 3 5 4
R L R L L L L
Output
1 1 1 1 2 -1 2
-1 -1
2 2
-1
-1 2 7 3 2 7 3
-----Note-----
Here is the picture for the seconds $0, 1, 2$ and $3$ of the first testcase:
Notice that robots $2$ and $3$ don't collide because they meet at the same point $2.5$, which is not integer.
After second $3$ robot $6$ just drive infinitely because there's no robot to collide with. | for cs in range(int(input())):
n, m = map(int, input().split())
X = list(map(int, input().split()))
D = list(map(str, input().split()))
X_odd = []
X_even = []
ans = [(-1) for _ in range(n)]
for i in range(n):
if X[i] % 2 == 1:
X_odd.append([X[i], D[i], i])
else:
X_even.append([X[i], D[i], i])
X_odd.sort()
X_even.sort()
def F(Y):
stack = []
for r in Y:
if len(stack) == 0:
stack.append(r)
elif r[1] == "L":
if stack[-1][1] == "L":
x = (r[0] + stack[-1][0]) // 2
ans[r[2]] = x
ans[stack[-1][2]] = x
stack.pop()
elif stack[-1][1] == "R":
ans[r[2]] = (r[0] - stack[-1][0]) // 2
ans[stack[-1][2]] = ans[r[2]]
stack.pop()
else:
stack.append(r)
while len(stack) > 1:
f = stack[-1]
stack.pop()
s = stack[-1]
stack.pop()
if f[1] == "R" and s[1] == "R":
ans[f[2]] = ans[s[2]] = (2 * m - f[0] - s[0]) // 2
elif f[1] == "R" and s[1] == "L":
ans[f[2]] = ans[s[2]] = (m - f[0] + s[0] + m) // 2
F(X_odd)
F(X_even)
print(*ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER STRING IF VAR NUMBER NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER STRING ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER STRING VAR NUMBER STRING ASSIGN VAR VAR NUMBER VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER STRING VAR NUMBER STRING ASSIGN VAR VAR NUMBER VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
There are $n$ robots driving along an OX axis. There are also two walls: one is at coordinate $0$ and one is at coordinate $m$.
The $i$-th robot starts at an integer coordinate $x_i~(0 < x_i < m)$ and moves either left (towards the $0$) or right with the speed of $1$ unit per second. No two robots start at the same coordinate.
Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.
Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.
For each robot find out if it ever explodes and print the time of explosion if it happens and $-1$ otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the descriptions of $t$ testcases follow.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 3 \cdot 10^5$; $2 \le m \le 10^8$) — the number of robots and the coordinate of the right wall.
The second line of each testcase contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 < x_i < m$) — the starting coordinates of the robots.
The third line of each testcase contains $n$ space-separated characters 'L' or 'R' — the starting directions of the robots ('L' stands for left and 'R' stands for right).
All coordinates $x_i$ in the testcase are distinct.
The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each testcase print $n$ integers — for the $i$-th robot output the time it explodes at if it does and $-1$ otherwise.
-----Examples-----
Input
5
7 12
1 2 3 4 9 10 11
R R L L R R R
2 10
1 6
R R
2 10
1 3
L L
1 10
5
R
7 8
6 1 7 2 3 5 4
R L R L L L L
Output
1 1 1 1 2 -1 2
-1 -1
2 2
-1
-1 2 7 3 2 7 3
-----Note-----
Here is the picture for the seconds $0, 1, 2$ and $3$ of the first testcase:
Notice that robots $2$ and $3$ don't collide because they meet at the same point $2.5$, which is not integer.
After second $3$ robot $6$ just drive infinitely because there's no robot to collide with. | t = int(input())
def get(n, m, a):
stack = [0] * (n + 1)
top = 0
res = [-1] * n
for i in range(n):
if a[i] > 0:
top += 1
stack[top] = i
elif top == 0 or a[stack[top]] < 0:
top += 1
stack[top] = i
else:
j = stack[top]
res[j] = (-a[i] - a[j]) // 2
res[i] = res[j]
top -= 1
if top > 0:
down = 1
while top > down and a[stack[down]] < 0 and a[stack[down + 1]] < 0:
i = stack[down]
j = stack[down + 1]
res[i] = (-a[j] + a[i]) // 2 - a[i]
res[j] = res[i]
down += 2
up = top
while up > 1 and a[stack[up]] > 0 and a[stack[up - 1]] > 0:
i = stack[up - 1]
j = stack[up]
res[i] = (a[j] - a[i]) // 2 + (m - a[j])
res[j] = res[i]
up -= 2
if down + 1 == up and down <= top and up >= 0:
i = stack[down]
j = stack[up]
if -a[i] > m - a[j]:
res[i] = (-a[i] + a[j]) // 2 + m - a[j]
res[j] = res[i]
else:
res[i] = (a[i] - a[j]) // 2 + m - a[i]
res[j] = res[i]
return res
for ts in range(t):
n, m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
dir = [i for i in input().split()]
aa = [(a[i], i) for i in range(n)]
aa = sorted(aa)
x1 = []
p1 = []
x2 = []
p2 = []
for id in range(n):
i = aa[id][1]
if a[i] % 2 == 0:
if dir[i] == "R":
x1.append(a[i])
else:
x1.append(-a[i])
p1.append(i)
else:
if dir[i] == "R":
x2.append(a[i])
else:
x2.append(-a[i])
p2.append(i)
res1 = get(len(x1), m, x1)
res2 = get(len(x2), m, x2)
finRes = [0] * n
for i in range(len(res1)):
finRes[p1[i]] = res1[i]
for i in range(len(res2)):
finRes[p2[i]] = res2[i]
for i in finRes:
print(i, end=" ")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
There are $n$ robots driving along an OX axis. There are also two walls: one is at coordinate $0$ and one is at coordinate $m$.
The $i$-th robot starts at an integer coordinate $x_i~(0 < x_i < m)$ and moves either left (towards the $0$) or right with the speed of $1$ unit per second. No two robots start at the same coordinate.
Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.
Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.
For each robot find out if it ever explodes and print the time of explosion if it happens and $-1$ otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the descriptions of $t$ testcases follow.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 3 \cdot 10^5$; $2 \le m \le 10^8$) — the number of robots and the coordinate of the right wall.
The second line of each testcase contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 < x_i < m$) — the starting coordinates of the robots.
The third line of each testcase contains $n$ space-separated characters 'L' or 'R' — the starting directions of the robots ('L' stands for left and 'R' stands for right).
All coordinates $x_i$ in the testcase are distinct.
The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each testcase print $n$ integers — for the $i$-th robot output the time it explodes at if it does and $-1$ otherwise.
-----Examples-----
Input
5
7 12
1 2 3 4 9 10 11
R R L L R R R
2 10
1 6
R R
2 10
1 3
L L
1 10
5
R
7 8
6 1 7 2 3 5 4
R L R L L L L
Output
1 1 1 1 2 -1 2
-1 -1
2 2
-1
-1 2 7 3 2 7 3
-----Note-----
Here is the picture for the seconds $0, 1, 2$ and $3$ of the first testcase:
Notice that robots $2$ and $3$ don't collide because they meet at the same point $2.5$, which is not integer.
After second $3$ robot $6$ just drive infinitely because there's no robot to collide with. | for _ in range(int(input())):
n, m = map(int, input().split())
p = list(map(int, input().split()))
d = list(map(str, input().split()))
c = {}
for i in range(len(p)):
ele = p[i]
c[ele] = i
e = []
o = []
ans = [0] * n
for i in range(n):
x = p[i]
if x % 2:
o.append((x, d[i]))
else:
e.append((x, d[i]))
e.sort()
o.sort()
stack = []
for x in e:
di = x[0]
h = x[1]
if h == "R":
stack.append(x)
elif len(stack) == 0:
stack.append(x)
elif stack[-1][1] == "R":
cnt = 0
y = stack.pop()
diff = (di - y[0]) // 2
cnt += diff
i1 = c[di]
i2 = c[y[0]]
ans[i1] = cnt
ans[i2] = cnt
else:
cnt = 0
y = stack.pop()
diff = (di - y[0]) // 2
cnt += diff
cnt += y[0]
i1 = c[di]
i2 = c[y[0]]
ans[i1] = cnt
ans[i2] = cnt
while len(stack) > 1:
e2 = stack.pop()
e1 = stack.pop()
d2 = e2[0]
d1 = e1[0]
if e2[1] == "R" and e1[1] == "R":
cnt = 0
diff = (d2 - d1) // 2
cnt += diff
cnt += m - d2
i1 = c[d2]
i2 = c[d1]
ans[i1] = cnt
ans[i2] = cnt
continue
z1 = d1
z2 = d2
dis = max(abs(m - d2), d1)
cnt = dis
d1 = abs(d1 - dis)
d2 = (d2 + dis) % m
d2 = m - d2
diff = abs(d2 - d1) // 2
cnt += diff
i1 = c[z2]
i2 = c[z1]
ans[i1] = cnt
ans[i2] = cnt
continue
if len(stack) == 1:
ele = stack.pop()
d1 = ele[0]
i1 = c[d1]
ans[i1] = -1
stack = []
for x in o:
h = x[1]
di = x[0]
if h == "R":
stack.append(x)
elif len(stack) == 0:
stack.append(x)
elif stack[-1][1] == "R":
cnt = 0
y = stack.pop()
diff = (di - y[0]) // 2
cnt += diff
i1 = c[di]
i2 = c[y[0]]
ans[i1] = cnt
ans[i2] = cnt
else:
cnt = 0
y = stack.pop()
diff = (di - y[0]) // 2
cnt += diff
cnt += y[0]
i1 = c[di]
i2 = c[y[0]]
ans[i1] = cnt
ans[i2] = cnt
while len(stack) > 1:
e2 = stack.pop()
e1 = stack.pop()
d2 = e2[0]
d1 = e1[0]
if e2[1] == "R" and e1[1] == "R":
cnt = 0
diff = (d2 - d1) // 2
cnt += diff
cnt += m - d2
i1 = c[d2]
i2 = c[d1]
ans[i1] = cnt
ans[i2] = cnt
continue
z1 = d1
z2 = d2
dis = max(abs(m - d2), d1)
cnt = dis
d1 = abs(d1 - dis)
d2 = (d2 + dis) % m
d2 = m - d2
diff = abs(d2 - d1) // 2
cnt += diff
i1 = c[z2]
i2 = c[z1]
ans[i1] = cnt
ans[i2] = cnt
continue
if len(stack) == 1:
ele = stack.pop()
d1 = ele[0]
i1 = c[d1]
ans[i1] = -1
print(*ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER STRING VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER STRING VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
There are $n$ robots driving along an OX axis. There are also two walls: one is at coordinate $0$ and one is at coordinate $m$.
The $i$-th robot starts at an integer coordinate $x_i~(0 < x_i < m)$ and moves either left (towards the $0$) or right with the speed of $1$ unit per second. No two robots start at the same coordinate.
Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.
Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.
For each robot find out if it ever explodes and print the time of explosion if it happens and $-1$ otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the descriptions of $t$ testcases follow.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 3 \cdot 10^5$; $2 \le m \le 10^8$) — the number of robots and the coordinate of the right wall.
The second line of each testcase contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 < x_i < m$) — the starting coordinates of the robots.
The third line of each testcase contains $n$ space-separated characters 'L' or 'R' — the starting directions of the robots ('L' stands for left and 'R' stands for right).
All coordinates $x_i$ in the testcase are distinct.
The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each testcase print $n$ integers — for the $i$-th robot output the time it explodes at if it does and $-1$ otherwise.
-----Examples-----
Input
5
7 12
1 2 3 4 9 10 11
R R L L R R R
2 10
1 6
R R
2 10
1 3
L L
1 10
5
R
7 8
6 1 7 2 3 5 4
R L R L L L L
Output
1 1 1 1 2 -1 2
-1 -1
2 2
-1
-1 2 7 3 2 7 3
-----Note-----
Here is the picture for the seconds $0, 1, 2$ and $3$ of the first testcase:
Notice that robots $2$ and $3$ don't collide because they meet at the same point $2.5$, which is not integer.
After second $3$ robot $6$ just drive infinitely because there's no robot to collide with. | import sys
input = sys.stdin.buffer.readline
for t in range(int(input())):
N, M = map(int, input().split())
A = list(map(int, input().split()))
S = list(input())
X = [(A[i], S[i << 1], i) for i in range(N)]
X.sort()
ANS = [-1] * N
Q = [[], []]
Y = []
for i in range(N):
z = X[i][0] & 1
if X[i][1] == 76:
if len(Q[z]):
x = Q[z][-1]
y = X[i][2]
del Q[z][-1]
ANS[x] = abs(A[y] - A[x]) >> 1
ANS[y] = ANS[x]
else:
Q[z].append(X[i][2])
Y = [[[], []] for i in range(2)]
for i in range(N):
z = A[i] & 1
if ANS[i] == -1:
if S[i << 1] == 76:
Y[z][0].append((A[i], i))
else:
Y[z][1].append((A[i], i))
for i in range(2):
for j in range(2):
Y[i][j].sort()
for i in range(2):
for k in range(len(Y[i][0]) >> 1):
ANS[Y[i][0][k << 1][1]] = Y[i][0][k << 1][0] + (
Y[i][0][(k << 1) + 1][0] - Y[i][0][k << 1][0] >> 1
)
ANS[Y[i][0][(k << 1) + 1][1]] = ANS[Y[i][0][k << 1][1]]
for k in range(len(Y[i][1]) >> 1):
ANS[Y[i][1][-(k << 1) - 1][1]] = (
M
- Y[i][1][-(k << 1) - 1][0]
+ (Y[i][1][-(k << 1) - 1][0] - Y[i][1][-(k << 1) - 2][0] >> 1)
)
ANS[Y[i][1][-(k << 1) - 2][1]] = ANS[Y[i][1][-(k << 1) - 1][1]]
if len(Y[i][0]) & 1 and len(Y[i][1]) & 1:
x, y = Y[i][0][-1], Y[i][1][0]
ANS[x[1]] = x[0] + (M - y[0]) + M >> 1
ANS[y[1]] = ANS[x[1]]
print(*ANS) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST LIST LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST LIST LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
There are $n$ robots driving along an OX axis. There are also two walls: one is at coordinate $0$ and one is at coordinate $m$.
The $i$-th robot starts at an integer coordinate $x_i~(0 < x_i < m)$ and moves either left (towards the $0$) or right with the speed of $1$ unit per second. No two robots start at the same coordinate.
Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.
Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.
For each robot find out if it ever explodes and print the time of explosion if it happens and $-1$ otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the descriptions of $t$ testcases follow.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 3 \cdot 10^5$; $2 \le m \le 10^8$) — the number of robots and the coordinate of the right wall.
The second line of each testcase contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 < x_i < m$) — the starting coordinates of the robots.
The third line of each testcase contains $n$ space-separated characters 'L' or 'R' — the starting directions of the robots ('L' stands for left and 'R' stands for right).
All coordinates $x_i$ in the testcase are distinct.
The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each testcase print $n$ integers — for the $i$-th robot output the time it explodes at if it does and $-1$ otherwise.
-----Examples-----
Input
5
7 12
1 2 3 4 9 10 11
R R L L R R R
2 10
1 6
R R
2 10
1 3
L L
1 10
5
R
7 8
6 1 7 2 3 5 4
R L R L L L L
Output
1 1 1 1 2 -1 2
-1 -1
2 2
-1
-1 2 7 3 2 7 3
-----Note-----
Here is the picture for the seconds $0, 1, 2$ and $3$ of the first testcase:
Notice that robots $2$ and $3$ don't collide because they meet at the same point $2.5$, which is not integer.
After second $3$ robot $6$ just drive infinitely because there's no robot to collide with. | def binarysearch(x, l):
low = 0
high = len(l) - 1
ans = high + 1
while low <= high:
mid = (low + high) // 2
if l[mid][0] < x:
low = mid + 1
else:
ans = mid
high = mid - 1
return ans
t = int(input())
for i in range(t):
n, m = input().split()
n = int(n)
m = int(m)
u = []
v = []
p = {i: (-1) for i in range(n)}
a = list(map(int, input().split(" ")))
b = input().split(" ")
for i in range(n):
if a[i] % 2:
u.append([a[i], b[i], i])
else:
v.append([a[i], b[i], i])
d = [u, v]
u.sort(key=lambda x: x[0])
v.sort(key=lambda x: x[0])
for listi in d:
rl = []
ll = []
for i in range(len(listi)):
if listi[i][1] == "R":
rl.append([listi[i][0], listi[i][2]])
else:
ll.append([listi[i][0], listi[i][2]])
ppp = len(rl)
for j in reversed(range(ppp)):
ans = binarysearch(rl[j][0], ll)
if ans < len(ll):
p[rl[j][1]] = (ll[ans][0] - rl[j][0]) / 2
p[ll[ans][1]] = p[rl[j][1]]
ll.pop(ans)
rl.pop(j)
kk = len(rl)
for pp in range(kk - 1, 0, -2):
p[rl[pp][1]] = m - (rl[pp][0] + rl[pp - 1][0]) / 2
p[rl[pp - 1][1]] = p[rl[pp][1]]
rl.pop()
rl.pop()
lll = len(ll)
for pp in range(0, lll - 1, 2):
p[ll[pp][1]] = (ll[pp + 1][0] + ll[pp][0]) / 2
p[ll[pp + 1][1]] = p[ll[pp][1]]
if ll and p[ll[-1][1]] == -1 and rl:
p[ll[-1][1]] = (ll[-1][0] - rl[0][0]) / 2 + m
p[rl[0][1]] = p[ll[-1][1]]
for i in range(len(p)):
print(int(p[i]), end=" ")
print() | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR |
There are $n$ robots driving along an OX axis. There are also two walls: one is at coordinate $0$ and one is at coordinate $m$.
The $i$-th robot starts at an integer coordinate $x_i~(0 < x_i < m)$ and moves either left (towards the $0$) or right with the speed of $1$ unit per second. No two robots start at the same coordinate.
Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.
Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.
For each robot find out if it ever explodes and print the time of explosion if it happens and $-1$ otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the descriptions of $t$ testcases follow.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 3 \cdot 10^5$; $2 \le m \le 10^8$) — the number of robots and the coordinate of the right wall.
The second line of each testcase contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 < x_i < m$) — the starting coordinates of the robots.
The third line of each testcase contains $n$ space-separated characters 'L' or 'R' — the starting directions of the robots ('L' stands for left and 'R' stands for right).
All coordinates $x_i$ in the testcase are distinct.
The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each testcase print $n$ integers — for the $i$-th robot output the time it explodes at if it does and $-1$ otherwise.
-----Examples-----
Input
5
7 12
1 2 3 4 9 10 11
R R L L R R R
2 10
1 6
R R
2 10
1 3
L L
1 10
5
R
7 8
6 1 7 2 3 5 4
R L R L L L L
Output
1 1 1 1 2 -1 2
-1 -1
2 2
-1
-1 2 7 3 2 7 3
-----Note-----
Here is the picture for the seconds $0, 1, 2$ and $3$ of the first testcase:
Notice that robots $2$ and $3$ don't collide because they meet at the same point $2.5$, which is not integer.
After second $3$ robot $6$ just drive infinitely because there's no robot to collide with. | import sys
def task_c():
input = sys.stdin.readline
for _ in range(int(input())):
n, m = map(int, input().split())
pos = list(map(int, input().split()))
dir = list(map(str, input().split()))
robots = [(pos[i], 1 if dir[i] == "R" else -1, i) for i in range(n)]
ans = [-1] * n
def solve(v):
v.sort(key=lambda x: x[0])
stk = []
for x, dir, id in v:
if dir == -1:
if stk:
x2, dir2, id2 = stk.pop()
if dir2 == 1:
ans[id] = ans[id2] = (x - x2) // 2
else:
ans[id] = ans[id2] = (x + x2) // 2
else:
stk.append((x, dir, id))
else:
stk.append((x, dir, id))
while len(stk) >= 2:
x, dir, id = stk.pop()
x2, dir2, id2 = stk.pop()
if dir2 == 1:
ans[id] = ans[id2] = (2 * m - x - x2) // 2
else:
ans[id] = ans[id2] = (2 * m - x + x2) // 2
solve([r for r in robots if r[0] % 2 == 0])
solve([r for r in robots if r[0] % 2 == 1])
print(*ans)
task_c() | IMPORT FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR STRING NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR IF VAR NUMBER IF VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
There are $n$ robots driving along an OX axis. There are also two walls: one is at coordinate $0$ and one is at coordinate $m$.
The $i$-th robot starts at an integer coordinate $x_i~(0 < x_i < m)$ and moves either left (towards the $0$) or right with the speed of $1$ unit per second. No two robots start at the same coordinate.
Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.
Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.
For each robot find out if it ever explodes and print the time of explosion if it happens and $-1$ otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the descriptions of $t$ testcases follow.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 3 \cdot 10^5$; $2 \le m \le 10^8$) — the number of robots and the coordinate of the right wall.
The second line of each testcase contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 < x_i < m$) — the starting coordinates of the robots.
The third line of each testcase contains $n$ space-separated characters 'L' or 'R' — the starting directions of the robots ('L' stands for left and 'R' stands for right).
All coordinates $x_i$ in the testcase are distinct.
The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each testcase print $n$ integers — for the $i$-th robot output the time it explodes at if it does and $-1$ otherwise.
-----Examples-----
Input
5
7 12
1 2 3 4 9 10 11
R R L L R R R
2 10
1 6
R R
2 10
1 3
L L
1 10
5
R
7 8
6 1 7 2 3 5 4
R L R L L L L
Output
1 1 1 1 2 -1 2
-1 -1
2 2
-1
-1 2 7 3 2 7 3
-----Note-----
Here is the picture for the seconds $0, 1, 2$ and $3$ of the first testcase:
Notice that robots $2$ and $3$ don't collide because they meet at the same point $2.5$, which is not integer.
After second $3$ robot $6$ just drive infinitely because there's no robot to collide with. | def solve(robot, m, res):
robot.sort()
stack = []
for x, dire, i in robot:
if dire == "L":
if not stack:
stack.append((i, -x))
else:
i2, x2 = stack[-1]
res[i] = res[i2] = (x - x2) // 2
stack.pop()
else:
stack.append((i, x))
while len(stack) >= 2:
i1, x1 = stack[-1]
stack.pop()
x1 = m + (m - x1)
i2, x2 = stack[-1]
stack.pop()
res[i1] = res[i2] = (x1 - x2) // 2
for _ in range(int(input())):
n, m = map(int, input().split())
info = list(zip(map(int, input().split()), input().split()))
robot = [[], []]
for i in range(n):
x, dire = info[i]
robot[x & 1].append((x, dire, i))
res = [(-1) for i in range(n)]
solve(robot[0], m, res)
solve(robot[1], m, res)
print(*res) | FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR IF VAR STRING IF VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
There are $n$ robots driving along an OX axis. There are also two walls: one is at coordinate $0$ and one is at coordinate $m$.
The $i$-th robot starts at an integer coordinate $x_i~(0 < x_i < m)$ and moves either left (towards the $0$) or right with the speed of $1$ unit per second. No two robots start at the same coordinate.
Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.
Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.
For each robot find out if it ever explodes and print the time of explosion if it happens and $-1$ otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the descriptions of $t$ testcases follow.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 3 \cdot 10^5$; $2 \le m \le 10^8$) — the number of robots and the coordinate of the right wall.
The second line of each testcase contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 < x_i < m$) — the starting coordinates of the robots.
The third line of each testcase contains $n$ space-separated characters 'L' or 'R' — the starting directions of the robots ('L' stands for left and 'R' stands for right).
All coordinates $x_i$ in the testcase are distinct.
The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each testcase print $n$ integers — for the $i$-th robot output the time it explodes at if it does and $-1$ otherwise.
-----Examples-----
Input
5
7 12
1 2 3 4 9 10 11
R R L L R R R
2 10
1 6
R R
2 10
1 3
L L
1 10
5
R
7 8
6 1 7 2 3 5 4
R L R L L L L
Output
1 1 1 1 2 -1 2
-1 -1
2 2
-1
-1 2 7 3 2 7 3
-----Note-----
Here is the picture for the seconds $0, 1, 2$ and $3$ of the first testcase:
Notice that robots $2$ and $3$ don't collide because they meet at the same point $2.5$, which is not integer.
After second $3$ robot $6$ just drive infinitely because there's no robot to collide with. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n, m = map(int, input().split())
A = list(map(int, input().split()))
B = input().split()
stack0 = []
stack1 = []
ans = [-1] * n
C = sorted([(a, b, i) for i, (a, b) in enumerate(zip(A, B))])
for a, b, i in C:
if not a % 2:
if b == "L":
if stack0:
x, j = stack0.pop()
ans[i] = ans[j] = (a - x) // 2
else:
stack0.append((-a, i))
else:
stack0.append((a, i))
elif b == "L":
if stack1:
x, j = stack1.pop()
ans[i] = ans[j] = (a - x) // 2
else:
stack1.append((-a, i))
else:
stack1.append((a, i))
while len(stack0) >= 2:
x, i = stack0.pop()
y, j = stack0.pop()
ans[i] = ans[j] = (2 * m - x - y) // 2
while len(stack1) >= 2:
x, i = stack1.pop()
y, j = stack1.pop()
ans[i] = ans[j] = (2 * m - x - y) // 2
print(*ans) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR VAR IF BIN_OP VAR NUMBER IF VAR STRING IF VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR STRING IF VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
There are $n$ robots driving along an OX axis. There are also two walls: one is at coordinate $0$ and one is at coordinate $m$.
The $i$-th robot starts at an integer coordinate $x_i~(0 < x_i < m)$ and moves either left (towards the $0$) or right with the speed of $1$ unit per second. No two robots start at the same coordinate.
Whenever a robot reaches a wall, it turns around instantly and continues his ride in the opposite direction with the same speed.
Whenever several robots meet at the same integer coordinate, they collide and explode into dust. Once a robot has exploded, it doesn't collide with any other robot. Note that if several robots meet at a non-integer coordinate, nothing happens.
For each robot find out if it ever explodes and print the time of explosion if it happens and $-1$ otherwise.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of testcases.
Then the descriptions of $t$ testcases follow.
The first line of each testcase contains two integers $n$ and $m$ ($1 \le n \le 3 \cdot 10^5$; $2 \le m \le 10^8$) — the number of robots and the coordinate of the right wall.
The second line of each testcase contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 < x_i < m$) — the starting coordinates of the robots.
The third line of each testcase contains $n$ space-separated characters 'L' or 'R' — the starting directions of the robots ('L' stands for left and 'R' stands for right).
All coordinates $x_i$ in the testcase are distinct.
The sum of $n$ over all testcases doesn't exceed $3 \cdot 10^5$.
-----Output-----
For each testcase print $n$ integers — for the $i$-th robot output the time it explodes at if it does and $-1$ otherwise.
-----Examples-----
Input
5
7 12
1 2 3 4 9 10 11
R R L L R R R
2 10
1 6
R R
2 10
1 3
L L
1 10
5
R
7 8
6 1 7 2 3 5 4
R L R L L L L
Output
1 1 1 1 2 -1 2
-1 -1
2 2
-1
-1 2 7 3 2 7 3
-----Note-----
Here is the picture for the seconds $0, 1, 2$ and $3$ of the first testcase:
Notice that robots $2$ and $3$ don't collide because they meet at the same point $2.5$, which is not integer.
After second $3$ robot $6$ just drive infinitely because there's no robot to collide with. | def collide_after(indices):
ans = [-1] * N
indices.sort(key=lambda i: A[i])
stack = []
for i in indices:
if D[i] == "R":
stack.append(i)
elif len(stack) > 0:
j = stack.pop()
ans[i] = A[i] - A[j] >> 1
ans[j] = ans[i]
else:
stack.append(i)
A[i] = -A[i]
while len(stack) >= 2:
i = stack.pop()
j = stack.pop()
ans[i] = M - A[i] + (A[i] - A[j] >> 1)
ans[j] = ans[i]
return ans
def solve():
even = [i for i in range(N) if A[i] % 2 == 0]
odd = [i for i in range(N) if A[i] % 2 == 1]
even_soln = collide_after(even)
odd_soln = collide_after(odd)
ans = [0] * N
for i in range(N):
if A[i] % 2 == 0:
ans[i] = even_soln[i]
else:
ans[i] = odd_soln[i]
return ans
test_cases = int(input())
for test_case in range(test_cases):
N, M = map(int, input().split())
A = list(map(int, input().split()))
D = input().split()
print(*solve()) | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
Alice has a hand of cards, given as an array of integers.
Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards.
Return true if and only if she can.
Example 1:
Input: hand = [1,2,3,6,2,3,4,7,8], W = 3
Output: true
Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8].
Example 2:
Input: hand = [1,2,3,4,5], W = 4
Output: false
Explanation: Alice's hand can't be rearranged into groups of 4.
Constraints:
1 <= hand.length <= 10000
0 <= hand[i] <= 10^9
1 <= W <= hand.length
Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/ | class Solution:
def isNStraightHand(self, hand: List[int], W: int) -> bool:
if len(hand) % W != 0:
return False
freq_dict = collections.Counter(hand)
while freq_dict:
min_num = min(freq_dict)
for i in range(min_num, min_num + W):
freq = freq_dict[i]
if freq == 0:
return False
elif freq == 1:
del freq_dict[i]
else:
freq_dict[i] -= 1
return True | CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER RETURN NUMBER VAR |
Alice has a hand of cards, given as an array of integers.
Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards.
Return true if and only if she can.
Example 1:
Input: hand = [1,2,3,6,2,3,4,7,8], W = 3
Output: true
Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8].
Example 2:
Input: hand = [1,2,3,4,5], W = 4
Output: false
Explanation: Alice's hand can't be rearranged into groups of 4.
Constraints:
1 <= hand.length <= 10000
0 <= hand[i] <= 10^9
1 <= W <= hand.length
Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/ | class Solution:
def isNStraightHand(self, hand: List[int], W: int) -> bool:
hand.sort()
count = {}
for h in hand:
if h not in count:
count[h] = 1
else:
count[h] += 1
key = list(count.keys())
print(count)
head = 0
while head < len(key):
this_key = key[head]
if count[this_key] == 0:
head += 1
continue
tail = head + 1
while tail < len(key) and tail < head + W:
if (
count[key[tail]] >= count[this_key]
and key[tail] == key[tail - 1] + 1
):
count[key[tail]] -= count[this_key]
else:
return False
tail += 1
if tail < head + W:
return False
count[this_key] = 0
head += 1
return True | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR RETURN NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER RETURN NUMBER VAR |
Alice has a hand of cards, given as an array of integers.
Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards.
Return true if and only if she can.
Example 1:
Input: hand = [1,2,3,6,2,3,4,7,8], W = 3
Output: true
Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8].
Example 2:
Input: hand = [1,2,3,4,5], W = 4
Output: false
Explanation: Alice's hand can't be rearranged into groups of 4.
Constraints:
1 <= hand.length <= 10000
0 <= hand[i] <= 10^9
1 <= W <= hand.length
Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/ | class Solution:
def isNStraightHand(self, hand: List[int], W: int) -> bool:
count_map = Counter()
for num in hand:
count_map[num] += 1
test = sorted(count_map)
sorted_hand = sorted(dict.fromkeys(hand))
while len(sorted_hand) > 0:
while len(sorted_hand) > 0 and count_map[sorted_hand[0]] == 0:
sorted_hand.pop(0)
if len(sorted_hand) > 0:
for key in range(sorted_hand[0], sorted_hand[0] + W):
if key in count_map:
count_map[key] -= 1
if count_map[key] < 0:
return False
else:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER RETURN NUMBER VAR |
Alice has a hand of cards, given as an array of integers.
Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards.
Return true if and only if she can.
Example 1:
Input: hand = [1,2,3,6,2,3,4,7,8], W = 3
Output: true
Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8].
Example 2:
Input: hand = [1,2,3,4,5], W = 4
Output: false
Explanation: Alice's hand can't be rearranged into groups of 4.
Constraints:
1 <= hand.length <= 10000
0 <= hand[i] <= 10^9
1 <= W <= hand.length
Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/ | class Solution:
def isNStraightHand(self, hand: List[int], W: int) -> bool:
if len(hand) % W != 0:
return False
heap = [(key, val) for key, val in list(Counter(hand).items())]
heapq.heapify(heap)
while heap:
pre, prec = None, None
temp = deque()
for _ in range(W):
if not heap:
return False
num, count = heapq.heappop(heap)
if not pre:
pre, prec = num, count
continue
if num != pre + 1 or count < prec:
return False
pre = num
if count > prec:
temp.append((num, count - prec))
while temp:
heapq.heappush(heap, temp.pop())
return True | CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR VAR NONE NONE ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR RETURN NUMBER VAR |
Alice has a hand of cards, given as an array of integers.
Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards.
Return true if and only if she can.
Example 1:
Input: hand = [1,2,3,6,2,3,4,7,8], W = 3
Output: true
Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8].
Example 2:
Input: hand = [1,2,3,4,5], W = 4
Output: false
Explanation: Alice's hand can't be rearranged into groups of 4.
Constraints:
1 <= hand.length <= 10000
0 <= hand[i] <= 10^9
1 <= W <= hand.length
Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/ | class Solution:
def isNStraightHand(self, hand: List[int], W: int) -> bool:
cnt = collections.Counter(hand)
while cnt:
m = min(cnt)
for i in range(m, m + W):
if i not in cnt:
return False
elif cnt[i] == 1:
del cnt[i]
else:
cnt[i] -= 1
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN NUMBER VAR |
Alice has a hand of cards, given as an array of integers.
Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards.
Return true if and only if she can.
Example 1:
Input: hand = [1,2,3,6,2,3,4,7,8], W = 3
Output: true
Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8].
Example 2:
Input: hand = [1,2,3,4,5], W = 4
Output: false
Explanation: Alice's hand can't be rearranged into groups of 4.
Constraints:
1 <= hand.length <= 10000
0 <= hand[i] <= 10^9
1 <= W <= hand.length
Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/ | class Solution:
def isNStraightHand(self, hand: List[int], W: int) -> bool:
hand.sort()
def helper(hand):
if not hand:
return
if len(hand) % W != 0:
self.ans = False
return
n = len(hand)
i, k = 0, 0
prev = hand[0] - 1
new_hand = []
while i < n and k < W:
if hand[i] == prev:
new_hand.append(hand[i])
elif hand[i] != prev + 1:
self.ans = False
return
else:
prev += 1
k += 1
i += 1
new_hand += hand[i:]
if not new_hand:
self.ans = True
return
return helper(new_hand)
helper(hand)
return self.ans | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF IF VAR RETURN IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER RETURN VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR ASSIGN VAR NUMBER RETURN RETURN FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR |
Alice has a hand of cards, given as an array of integers.
Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards.
Return true if and only if she can.
Example 1:
Input: hand = [1,2,3,6,2,3,4,7,8], W = 3
Output: true
Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8].
Example 2:
Input: hand = [1,2,3,4,5], W = 4
Output: false
Explanation: Alice's hand can't be rearranged into groups of 4.
Constraints:
1 <= hand.length <= 10000
0 <= hand[i] <= 10^9
1 <= W <= hand.length
Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/ | class Solution:
def isNStraightHand(self, hand: List[int], W: int) -> bool:
hmap = {}
for h in hand:
if h not in hmap:
hmap[h] = 0
hmap[h] += 1
cards = sorted(list(hmap.keys()))
for card in cards:
while card in hmap:
for n in range(card, card + W):
if n not in hmap:
return False
else:
hmap[n] -= 1
if hmap[n] == 0:
del hmap[n]
if hmap:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR WHILE VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR IF VAR RETURN NUMBER RETURN NUMBER VAR |
Alice has a hand of cards, given as an array of integers.
Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards.
Return true if and only if she can.
Example 1:
Input: hand = [1,2,3,6,2,3,4,7,8], W = 3
Output: true
Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8].
Example 2:
Input: hand = [1,2,3,4,5], W = 4
Output: false
Explanation: Alice's hand can't be rearranged into groups of 4.
Constraints:
1 <= hand.length <= 10000
0 <= hand[i] <= 10^9
1 <= W <= hand.length
Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/ | class Solution:
def isNStraightHand(self, hand: List[int], W: int) -> bool:
hand.sort()
if len(hand) % W != 0:
return False
while hand:
z = min(hand)
for t in range(z, z + W):
try:
hand.remove(t)
except:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.