description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | def compare(p1, p2):
return p1[0] ** 2 + p1[1] ** 2 - (p2[0] ** 2 + p2[1] ** 2)
def swap(arr, i, j):
arr[i], arr[j] = arr[j], arr[i]
def partition(A, l, r):
pivot = A[l]
while l < r:
while l < r and compare(A[r], pivot) >= 0:
r -= 1
A[l] = A[r]
while l < r and compare(A[l], pivot) <= 0:
l += 1
A[r] = A[l]
A[l] = pivot
return l
def quick_select(points, k, low, high):
while low < high:
pivot = partition(points, low, high)
if pivot == k:
break
elif pivot < k:
low = pivot + 1
else:
high = pivot - 1
class Solution:
def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:
quick_select(points, k, 0, len(points) - 1)
return points[:k] | FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR WHILE VAR VAR WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER CLASS_DEF FUNC_DEF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
self.quickselect(points, K, 0, len(points) - 1)
return points[:K]
def quickselect(self, points, k, lo, high):
if lo >= high:
return
pivot = self.partition(points, lo, high)
if pivot + 1 == k:
return
elif pivot < k:
self.quickselect(points, k, pivot + 1, high)
else:
self.quickselect(points, k, lo, pivot - 1)
def partition(self, points, lo, high):
pivot = self.dist(points[high])
w = lo
for r in range(lo, high):
if self.dist(points[r]) < pivot:
points[w], points[r] = points[r], points[w]
w += 1
points[w], points[high] = points[high], points[w]
return w
def dist(self, pt):
return pt[0] ** 2 + pt[1] ** 2 | CLASS_DEF FUNC_DEF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR VAR VAR VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
dist = lambda x: x[0] ** 2 + x[1] ** 2
def partition(start, end):
ran = random.randint(start, end)
pivot = end
points[pivot], points[ran] = points[ran], points[pivot]
border = start
for cur in range(start, end):
if dist(points[cur]) <= dist(points[pivot]):
points[cur], points[border] = points[border], points[cur]
border += 1
points[border], points[pivot] = points[pivot], points[border]
return border
def quick_sort(left, right, k):
if left >= right:
return
p = partition(left, right)
if p == k - 1:
return
if p < k - 1:
quick_sort(p + 1, right, k)
else:
quick_sort(left, p - 1, k)
quick_sort(0, len(points) - 1, K)
return points[:K] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution(object):
def kClosest(self, points, K):
def distance(p):
return (p[0] ** 2 + p[1] ** 2) ** 0.5
def partition(l, r):
pivot = points[r]
mover = l
for i in range(l, r):
if distance(points[i]) <= distance(pivot):
points[mover], points[i] = points[i], points[mover]
mover += 1
points[mover], points[r] = points[r], points[mover]
return mover
def sort(l, r):
if l < r:
p = partition(l, r)
if p == K:
return
elif p < K:
sort(p + 1, r)
else:
sort(l, p - 1)
sort(0, len(points) - 1)
return points[:K] | CLASS_DEF VAR FUNC_DEF FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class myObj:
def __init__(self, val, p):
self.val = val
self.p = p
def __lt__(self, other):
return self.val > other.val
class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
h = []
heapq.heapify(h)
for p in points:
dis = p[0] * p[0] + p[1] * p[1]
heapq.heappush(h, myObj(dis, p))
if len(h) > K:
heapq.heappop(h)
ans = []
while h:
obj = heapq.heappop(h)
ans.append(obj.p)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF RETURN VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
max_heap = []
for point in points:
x, y = point[0], point[1]
dist = x**2 + y**2
if len(max_heap) < K:
heapq.heappush(max_heap, (-dist, x, y))
elif dist < -max_heap[0][0]:
heapq.heappop(max_heap)
heapq.heappush(max_heap, (-dist, x, y))
return [[x, y] for dist, x, y in max_heap] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN LIST VAR VAR VAR VAR VAR VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
closest = lambda x: points[x][0] ** 2 + points[x][1] ** 2
def partition(i, j):
oi = i
pivot = closest(i)
i += 1
while True:
while i < j and closest(i) < pivot:
i += 1
while i <= j and closest(j) >= pivot:
j -= 1
if i >= j:
break
points[i], points[j] = points[j], points[i]
points[oi], points[j] = points[j], points[oi]
return j
def kclosest(i, j, k):
if i >= j:
return
mid = partition(i, j)
if k > mid - i + 1:
return kclosest(mid + 1, j, k - (mid - i + 1))
elif k < mid - i + 1:
return kclosest(i, mid - 1, k)
kclosest(0, len(points) - 1, K)
return points[:K] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER WHILE NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
def dist(point):
return sum([(e**2) for e in point])
def partition(nums, left, right):
x = dist(nums[right])
i = left - 1
for j in range(left, right):
if dist(nums[j]) < x:
i += 1
nums[i], nums[j] = nums[j], nums[i]
i += 1
nums[right], nums[i] = nums[i], nums[right]
return i
p = -1
left = 0
right = len(points) - 1
while p != K:
p = partition(points, left, right)
if p + 1 < K:
left = p + 1
elif p + 1 > K:
right = p - 1
else:
return points[:K]
return points[:K] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR RETURN VAR VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
def dist(x) -> int:
return x[0] ** 2 + x[1] ** 2
def partition(i, j) -> int:
pivot = points[j]
l = i - 1
for r in range(i, j):
rv = points[r]
if dist(rv) < dist(pivot):
l += 1
points[l], points[r] = points[r], points[l]
points[j], points[l + 1] = points[l + 1], points[j]
return l + 1
def sort(i, j, K):
if i >= j:
return
mid = partition(i, j)
if mid - i + 1 == K:
return
elif mid - i + 1 < K:
sort(mid + 1, j, K - (mid - i + 1))
else:
sort(i, mid - 1, K)
sort(0, len(points) - 1, K)
return points[:K] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR RETURN BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN IF BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR VAR VAR VAR VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
self.sort(0, len(points) - 1, points, K)
return points[:K]
def getDistance(self, a):
return a[0] ** 2 + a[1] ** 2
def sort(self, l, r, points, K):
if l >= r:
return
mid = self.partition(l, r, points)
if mid - l + 1 < K:
self.sort(mid + 1, r, points, K - (mid - l + 1))
else:
self.sort(l, mid - 1, points, K)
def partition(self, i, j, points):
pivot = self.getDistance(points[i])
l = i
r = j
while True:
while l <= r and self.getDistance(points[l]) <= pivot:
l += 1
while l <= r and self.getDistance(points[r]) > pivot:
r -= 1
if l >= r:
break
points[l], points[r] = points[r], points[l]
points[i], points[r] = points[r], points[i]
return r | CLASS_DEF FUNC_DEF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR RETURN VAR VAR VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
arr = self.sortPoints(points)
return arr[:K]
def sortPoints(self, points):
if len(points) <= 1:
return points
mid = len(points) // 2
left = self.sortPoints(points[:mid])
right = self.sortPoints(points[mid:])
return self.merge(left, right)
def merge(self, left, right):
arr, i, j = [], 0, 0
while i < len(left) and j < len(right):
xLeft, yLeft = left[i][0], left[i][1]
xRight, yRight = right[j][0], right[j][1]
leftDis = xLeft * xLeft + yLeft * yLeft
rightDis = xRight * xRight + yRight * yRight
if leftDis < rightDis:
arr.append(left[i])
i += 1
elif rightDis < leftDis:
arr.append(right[j])
j += 1
else:
arr.append(left[i])
arr.append(right[j])
i, j = i + 1, j + 1
while i < len(left):
arr.append(left[i])
i += 1
while j < len(right):
arr.append(right[j])
j += 1
return arr | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR LIST NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR |
We have a list of points on the plane. Find the K closest points to the origin (0, 0).
(Here, the distance between two points on a plane is the Euclidean distance.)
You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)
Example 1:
Input: points = [[1,3],[-2,2]], K = 1
Output: [[-2,2]]
Explanation:
The distance between (1, 3) and the origin is sqrt(10).
The distance between (-2, 2) and the origin is sqrt(8).
Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
Example 2:
Input: points = [[3,3],[5,-1],[-2,4]], K = 2
Output: [[3,3],[-2,4]]
(The answer [[-2,4],[3,3]] would also be accepted.)
Note:
1 <= K <= points.length <= 10000
-10000 < points[i][0] < 10000
-10000 < points[i][1] < 10000 | class Solution:
def kClosest(self, points: List[List[int]], K: int) -> List[List[int]]:
heap = []
for a, b in points:
if len(heap) < K:
heapq.heappush(heap, (-1 * (a**2 + b**2), [a, b]))
else:
heapq.heappushpop(heap, (-1 * (a**2 + b**2), [a, b]))
op = []
print(heap)
for i in range(K):
print(i)
op.append(heap[i][1])
return op | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR VAR VAR |
Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.
There is a sequence a that consists of n integers a_1, a_2, ..., a_{n}. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r and a_{k} = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, a_{i}) > f(j, n, a_{j}).
Help Pashmak with the test.
-----Input-----
The first line of the input contains an integer n (1 ≤ n ≤ 10^6). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
7
1 2 1 1 2 2 1
Output
8
Input
3
1 1 1
Output
1
Input
5
1 2 3 4 5
Output
0 | n = int(input())
a = list(map(int, input().split()))
fen = [0] * (n + 1)
def update(index, value):
while index <= n:
fen[index] += value
index += index & -index
def get(p):
ans = 0
while p > 0:
ans += fen[p]
p -= p & -p
return ans
l = [0] * n
r = [0] * n
cnt = [0] * (n + 1)
appearances = {}
for i in range(n - 1, -1, -1):
if a[i] in appearances.keys():
appearances[a[i]] += 1
else:
appearances[a[i]] = 1
r[i] = appearances[a[i]]
update(r[i], 1)
appearances = {}
res = 0
for i in range(n):
if a[i] in appearances.keys():
appearances[a[i]] += 1
else:
appearances[a[i]] = 1
l[i] = appearances[a[i]]
update(r[i], -1)
res += get(l[i] - 1)
print(res) | 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 FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.
There is a sequence a that consists of n integers a_1, a_2, ..., a_{n}. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r and a_{k} = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, a_{i}) > f(j, n, a_{j}).
Help Pashmak with the test.
-----Input-----
The first line of the input contains an integer n (1 ≤ n ≤ 10^6). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
7
1 2 1 1 2 2 1
Output
8
Input
3
1 1 1
Output
1
Input
5
1 2 3 4 5
Output
0 | import sys
input = sys.stdin.readline
for _ in range(1):
n = int(input())
arr = [int(x) for x in input().split()]
b = [(0) for i in range(n + 1)]
def update(i, val, n):
i += 1
while i < n:
b[i] += val
i += i & -i
return
def get(i):
i += 1
ans = 0
while i > 0:
ans += b[i]
i -= i & -i
return ans
d = {}
for i in arr:
if i in d:
d[i] += 1
else:
d[i] = 1
update(d[i], 1, n + 1)
ans = 0
now = {}
for i in arr:
update(d[i], -1, n + 1)
d[i] -= 1
if i in now:
now[i] += 1
else:
now[i] = 1
ans += get(now[i] - 1)
print(ans) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN FUNC_DEF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.
There is a sequence a that consists of n integers a_1, a_2, ..., a_{n}. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r and a_{k} = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, a_{i}) > f(j, n, a_{j}).
Help Pashmak with the test.
-----Input-----
The first line of the input contains an integer n (1 ≤ n ≤ 10^6). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
7
1 2 1 1 2 2 1
Output
8
Input
3
1 1 1
Output
1
Input
5
1 2 3 4 5
Output
0 | n = int(input())
l = [int(j) for j in input().split()]
d = dict()
pre = []
for i in range(n):
if l[i] in d:
d[l[i]] += 1
else:
d[l[i]] = 1
pre.append(d[l[i]])
suf = [(0) for i in range(n)]
d = dict()
for i in range(n - 1, -1, -1):
if l[i] in d:
d[l[i]] += 1
else:
d[l[i]] = 1
suf[i] = d[l[i]]
def update(bit, index, val):
n = len(bit)
while index < n:
bit[index] += val
index += index & -1 * index
def getsum(bit, index):
n = len(bit)
ans = 0
while index > 0:
ans += bit[index]
index -= index & -1 * index
return ans
n = len(pre)
bit = [0] * (max(suf) + 1)
inv_ct = 0
for i in range(n - 1, -1, -1):
inv_ct += getsum(bit, pre[i] - 1)
update(bit, suf[i], 1)
print(inv_ct) | 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 LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.
There is a sequence a that consists of n integers a_1, a_2, ..., a_{n}. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r and a_{k} = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, a_{i}) > f(j, n, a_{j}).
Help Pashmak with the test.
-----Input-----
The first line of the input contains an integer n (1 ≤ n ≤ 10^6). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
7
1 2 1 1 2 2 1
Output
8
Input
3
1 1 1
Output
1
Input
5
1 2 3 4 5
Output
0 | INF = 10**10
def merge(l, r):
res = l + r
i = j = k = 0
while i < len(l) and j < len(r):
if l[i] < r[j]:
res[k] = l[i]
k += 1
i += 1
else:
res[k] = r[j]
k += 1
j += 1
while i < len(l):
res[k] = l[i]
k += 1
i += 1
while j < len(r):
res[k] = r[j]
k += 1
j += 1
return res
def solve(fl, fr, l, r):
if l == r:
return 0
mid = (l + r) // 2
res = solve(fl, fr, l, mid) + solve(fl, fr, mid + 1, r)
i, j = l, mid + 1
while i <= mid:
while j <= r and fr[j] < fl[i]:
j += 1
res += j - mid - 1
i += 1
fl[l : r + 1] = merge(fl[l : mid + 1], fl[mid + 1 : r + 1])
fr[l : r + 1] = merge(fr[l : mid + 1], fr[mid + 1 : r + 1])
return res
n = int(input())
a = list(map(int, input().split()))
fl, cnt = [], {}
for x in a:
cnt[x] = cnt.get(x, 0) + 1
fl.append(cnt[x])
fr, cnt = [], {}
for x in a[::-1]:
cnt[x] = cnt.get(x, 0) + 1
fr.append(cnt[x])
fr = fr[::-1]
print(solve(fl, fr, 0, n - 1)) | ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST DICT FOR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER |
Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.
There is a sequence a that consists of n integers a_1, a_2, ..., a_{n}. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r and a_{k} = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, a_{i}) > f(j, n, a_{j}).
Help Pashmak with the test.
-----Input-----
The first line of the input contains an integer n (1 ≤ n ≤ 10^6). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
7
1 2 1 1 2 2 1
Output
8
Input
3
1 1 1
Output
1
Input
5
1 2 3 4 5
Output
0 | import sys
input = sys.stdin.readline
n = int(input())
t = list(map(int, input().split()))
d = dict()
j = 0
prefix = []
while j <= n - 1:
if t[j] not in d:
d[t[j]] = 1
else:
d[t[j]] += 1
prefix.append(d[t[j]])
j += 1
suff = []
d = dict()
j = n - 1
while j >= 0:
if t[j] not in d:
d[t[j]] = 1
else:
d[t[j]] += 1
suff.append(d[t[j]])
j -= 1
suff = suff[::-1]
def update(bit, index, val):
p = len(bit)
while index < p:
bit[index] += val
index += index & -index
def getsum(bit, index):
ans = 0
while index > 0:
ans += bit[index]
index -= index & -index
return ans
l = len(prefix)
bit = [0] * (len(suff) + 1)
res = 0
j = l - 1
while j >= 0:
res += getsum(bit, prefix[j] - 1)
update(bit, suff[j], 1)
j += -1
print(res) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.
There is a sequence a that consists of n integers a_1, a_2, ..., a_{n}. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r and a_{k} = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, a_{i}) > f(j, n, a_{j}).
Help Pashmak with the test.
-----Input-----
The first line of the input contains an integer n (1 ≤ n ≤ 10^6). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
7
1 2 1 1 2 2 1
Output
8
Input
3
1 1 1
Output
1
Input
5
1 2 3 4 5
Output
0 | def merge(a, x, y, u, v):
i = 0
b = a[x : v + 1]
j = u
for k in range(x, y + 1):
while j <= v and a[j] < a[k]:
b[i] = a[j]
j += 1
i += 1
b[i] = a[k]
i += 1
while j <= u:
b[i] = a[j]
i += 1
j += 1
return b
def cal(l, r, x, y):
if x == y:
return 0
mid = (x + y) // 2
res = cal(l, r, x, mid) + cal(l, r, mid + 1, y)
j = mid + 1
for i in range(x, mid + 1):
while j <= y and r[j] < l[i]:
j += 1
res += j - mid - 1
l[x : y + 1] = merge(l, x, mid, mid + 1, y)
r[x : y + 1] = merge(r, x, mid, mid + 1, y)
return res
def countOccurrencePairs(a):
n = len(a)
l = [(0) for _ in range(n)]
d = {}
for i in range(n):
if a[i] not in d:
d[a[i]] = 0
d[a[i]] += 1
l[i] = d[a[i]]
r = [(0) for _ in range(n)]
d = {}
for i in range(n - 1, -1, -1):
if a[i] not in d:
d[a[i]] = 0
d[a[i]] += 1
r[i] = d[a[i]]
return cal(l, r, 0, n - 1)
n = int(input())
a = list(map(int, input().split()))
print(countOccurrencePairs(a)) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.
There is a sequence a that consists of n integers a_1, a_2, ..., a_{n}. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r and a_{k} = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, a_{i}) > f(j, n, a_{j}).
Help Pashmak with the test.
-----Input-----
The first line of the input contains an integer n (1 ≤ n ≤ 10^6). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
7
1 2 1 1 2 2 1
Output
8
Input
3
1 1 1
Output
1
Input
5
1 2 3 4 5
Output
0 | from sys import stdin, stdout
class bit:
def __init__(self, n):
self.n = n
self.f = [0] * (n + 1)
def update(self, pos, value):
while pos <= self.n:
self.f[pos] += value
pos += pos & -pos
def sum(self, pos):
res = 0
while pos:
res += self.f[pos]
pos -= pos & -pos
return res
def unique(A):
C = sorted(A)
B = []
for element in C:
if not B or B[-1] != element:
B.append(element)
return B
n = int(stdin.readline())
A = [int(x) for x in stdin.readline().split()]
Atmp = [0] * n
Work = bit(n + 10)
Map = {}
for index in range(n - 1, -1, -1):
if A[index] in Map:
Map[A[index]] += 1
else:
Map[A[index]] = 1
Atmp[index] = Map[A[index]]
Work.update(Map[A[index]], 1)
Map.clear()
ans = 0
for i in range(n):
if A[i] in Map:
Map[A[i]] += 1
else:
Map[A[i]] = 1
Work.update(Atmp[i], -1)
ans += Work.sum(Map[A[i]] - 1)
stdout.write("%d" % ans) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR |
Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.
There is a sequence a that consists of n integers a_1, a_2, ..., a_{n}. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r and a_{k} = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, a_{i}) > f(j, n, a_{j}).
Help Pashmak with the test.
-----Input-----
The first line of the input contains an integer n (1 ≤ n ≤ 10^6). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
7
1 2 1 1 2 2 1
Output
8
Input
3
1 1 1
Output
1
Input
5
1 2 3 4 5
Output
0 | BIT = [0] * 10**7
def query_BIT(i):
res = 0
while i > 0:
res += BIT[i]
i -= i & -i
return res
def add_to_BIT(n, p):
while p <= n:
BIT[p] += 1
p += p & -p
def main():
n = int(input())
arr = [int(i) for i in input().split(" ")]
pre = [0] * n
suf = [0] * n
d_map = {}
for i in range(n):
val = arr[i]
if val not in d_map:
d_map[val] = 1
else:
d_map[val] += 1
pre[i] = d_map[val]
d_map.clear()
for i in range(n - 1, -1, -1):
val = arr[i]
if val not in d_map:
d_map[val] = 1
else:
d_map[val] += 1
suf[i] = d_map[val]
ans = 0
for i in range(n - 1, -1, -1):
ans += query_BIT(pre[i] - 1)
add_to_BIT(n, suf[i])
print(ans)
main() | ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF WHILE VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.
There is a sequence a that consists of n integers a_1, a_2, ..., a_{n}. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r and a_{k} = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, a_{i}) > f(j, n, a_{j}).
Help Pashmak with the test.
-----Input-----
The first line of the input contains an integer n (1 ≤ n ≤ 10^6). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
7
1 2 1 1 2 2 1
Output
8
Input
3
1 1 1
Output
1
Input
5
1 2 3 4 5
Output
0 | import sys
input = sys.stdin.readline
BIT = [0] * 10**6
def update(idx):
while idx < len(BIT):
BIT[idx] += 1
idx += idx & -idx
def query(idx):
s = 0
while idx > 0:
s += BIT[idx]
idx -= idx & -idx
return s
n = int(input())
a = list(map(int, input().split()))
ans = 0
cnt = {}
l = [0] * n
r = [0] * n
for i in range(n):
cnt[a[i]] = cnt.get(a[i], 0) + 1
l[i] = cnt[a[i]]
cnt.clear()
for i in range(n)[::-1]:
cnt[a[i]] = cnt.get(a[i], 0) + 1
r[i] = cnt[a[i]]
if i < n - 1:
ans += query(l[i] - 1)
update(r[i])
print(ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER FUNC_DEF WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.
There is a sequence a that consists of n integers a_1, a_2, ..., a_{n}. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r and a_{k} = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, a_{i}) > f(j, n, a_{j}).
Help Pashmak with the test.
-----Input-----
The first line of the input contains an integer n (1 ≤ n ≤ 10^6). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
7
1 2 1 1 2 2 1
Output
8
Input
3
1 1 1
Output
1
Input
5
1 2 3 4 5
Output
0 | class SegTree:
def __init__(self, arr=None, length=None):
if arr is not None:
self.n = len(arr)
self.t = [(0) for _ in range(2 * self.n)]
self.construct(arr)
else:
self.n = length
self.t = [(0) for _ in range(2 * self.n)]
def construct(self, a):
self.t[self.n :] = arr
for i in reversed(range(0, self.n - 1)):
self.t[i] = self.t[i * 2] + self.t[i * 2 + 1]
def modify(self, p, val):
p += self.n
self.t[p] = val
while p > 1:
self.t[p // 2] = self.t[p] + self.t[p ^ 1]
p //= 2
def query(self, l, r):
res = 0
l += self.n
r += self.n
while l < r:
if l & 1:
res += self.t[l]
l += 1
if r & 1:
r -= 1
res += self.t[r]
l //= 2
r //= 2
return res
n = int(input())
l = [int(x) for x in input().split()]
front_fs = []
ct = {}
for v in l:
if v not in ct:
ct[v] = 0
ct[v] += 1
front_fs.append(ct[v])
back_fs = []
ct = {}
for v in reversed(l):
if v not in ct:
ct[v] = 0
ct[v] += 1
back_fs.append(ct[v])
back_fs = list(reversed(back_fs))
tot = 0
done = SegTree(length=n)
for fval_f, fval_b in zip(front_fs, back_fs):
tot += done.query(fval_b + 1, n)
if fval_f == n:
break
done.modify(fval_f, done.query(fval_f, fval_f + 1) + 1)
print(tot) | CLASS_DEF FUNC_DEF NONE NONE IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR FUNC_DEF ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR VAR VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.
There is a sequence a that consists of n integers a_1, a_2, ..., a_{n}. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r and a_{k} = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, a_{i}) > f(j, n, a_{j}).
Help Pashmak with the test.
-----Input-----
The first line of the input contains an integer n (1 ≤ n ≤ 10^6). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
7
1 2 1 1 2 2 1
Output
8
Input
3
1 1 1
Output
1
Input
5
1 2 3 4 5
Output
0 | n = int(input())
def getsum(BITTree, i):
s = 0
i = i + 1
while i > 0:
s += BITTree[i]
i -= i & -i
return s
def updatebit(BITTree, n, i, v):
i += 1
while i <= n:
BITTree[i] += v
i += i & -i
a = list(map(int, input().split()))
pre = dict()
pos = dict()
for i in range(n):
if pre.get(a[i], None) == None:
pre[a[i]] = 0
pre[a[i]] += 1
ans = 0
BIT = [0] * (n + 1)
for i in range(n - 1, 0, -1):
pre[a[i]] -= 1
if pos.get(a[i], None) == None:
pos[a[i]] = 0
pos[a[i]] += 1
updatebit(BIT, n, pos[a[i]], 1)
temp = getsum(BIT, pre[a[i - 1]] - 1)
ans += temp
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NONE NONE ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NONE NONE ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.
There is a sequence a that consists of n integers a_1, a_2, ..., a_{n}. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r and a_{k} = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, a_{i}) > f(j, n, a_{j}).
Help Pashmak with the test.
-----Input-----
The first line of the input contains an integer n (1 ≤ n ≤ 10^6). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
7
1 2 1 1 2 2 1
Output
8
Input
3
1 1 1
Output
1
Input
5
1 2 3 4 5
Output
0 | bit = [(0) for i in range(int(2000000.0 + 1))]
n = int(input())
def add(ind, x):
ind += 1
while ind <= n:
bit[ind] += x
ind += ind & -ind
def getsum(ind):
ans = 0
ind += 1
while ind > 0:
ans += bit[ind]
ind -= ind & -ind
return ans
a = list(map(int, input().split()))
b = [i for i in a]
b.sort()
d = {}
for i in range(n):
d[b[i]] = i
b = []
for i in a:
b.append(d[i])
a1 = [(0) for i in range(n)]
f1 = []
for i in b:
a1[i] += 1
f1.append(a1[i])
f2 = []
a1 = [(0) for i in range(n)]
b.reverse()
for i in b:
a1[i] += 1
f2.append(a1[i])
b.reverse()
f2.reverse()
ans = 0
for i in range(n - 1, -1, -1):
ans += getsum(f1[i] - 1)
add(f2[i], 1)
print(ans) | ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.
There is a sequence a that consists of n integers a_1, a_2, ..., a_{n}. Let's denote f(l, r, x) the number of indices k such that: l ≤ k ≤ r and a_{k} = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(1, i, a_{i}) > f(j, n, a_{j}).
Help Pashmak with the test.
-----Input-----
The first line of the input contains an integer n (1 ≤ n ≤ 10^6). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9).
-----Output-----
Print a single integer — the answer to the problem.
-----Examples-----
Input
7
1 2 1 1 2 2 1
Output
8
Input
3
1 1 1
Output
1
Input
5
1 2 3 4 5
Output
0 | import sys
input = sys.stdin.readline
n = int(input())
ar = list(map(int, input().split()))
li = []
dic = {}
for i in range(1, n + 1):
if ar[-i] in dic:
dic[ar[-i]] += 1
else:
dic[ar[-i]] = 1
li.append(dic[ar[-i]])
br = [0] * (n + 1)
for i in range(n):
br[li[i]] += 1
for idx in range(1, n + 1):
idx2 = idx + (idx & -idx)
if idx2 < n:
br[idx2] += br[idx]
main = 0
front = []
dic = {}
for i in range(n):
if ar[i] in dic:
dic[ar[i]] += 1
else:
dic[ar[i]] = 1
front.append(dic[ar[i]])
for i in range(n - 1):
inp = li[n - i - 1]
add = -1
while inp < n + 1:
br[inp] += add
inp += inp & -inp
if inp != 1:
inp -= 1
add = 1
while inp < n + 1:
br[inp] += add
inp += inp & -inp
ans = 0
inp = front[i]
if inp != 1:
inp -= 1
while inp:
ans += br[inp]
inp -= inp & -inp
main += ans
print(main) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this integer is between 1 and 100 000, inclusive. It is possible that some cards have the same integers on them.
Vasily decided to sort the cards. To do this, he repeatedly takes the top card from the deck, and if the number on it equals the minimum number written on the cards in the deck, then he places the card away. Otherwise, he puts it under the deck and takes the next card from the top, and so on. The process ends as soon as there are no cards in the deck. You can assume that Vasily always knows the minimum number written on some card in the remaining deck, but doesn't know where this card (or these cards) is.
You are to determine the total number of times Vasily takes the top card from the deck.
-----Input-----
The first line contains single integer n (1 ≤ n ≤ 100 000) — the number of cards in the deck.
The second line contains a sequence of n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 100 000), where a_{i} is the number written on the i-th from top card in the deck.
-----Output-----
Print the total number of times Vasily takes the top card from the deck.
-----Examples-----
Input
4
6 3 1 2
Output
7
Input
1
1000
Output
1
Input
7
3 3 3 3 3 3 3
Output
7
-----Note-----
In the first example Vasily at first looks at the card with number 6 on it, puts it under the deck, then on the card with number 3, puts it under the deck, and then on the card with number 1. He places away the card with 1, because the number written on it is the minimum among the remaining cards. After that the cards from top to bottom are [2, 6, 3]. Then Vasily looks at the top card with number 2 and puts it away. After that the cards from top to bottom are [6, 3]. Then Vasily looks at card 6, puts it under the deck, then at card 3 and puts it away. Then there is only one card with number 6 on it, and Vasily looks at it and puts it away. Thus, in total Vasily looks at 7 cards. | def main():
input()
numbers = tuple(map(int, input().split()))
d = []
for i in range(len(numbers)):
while len(d) <= numbers[i]:
d.append([])
d[numbers[i]].append(i)
dd = [[]]
for line in d:
if line:
dd.append(line)
d = dd
answer = [None] * len(numbers)
for item in d[1]:
answer[item] = 1
for i in range(1, len(d) - 1):
left_maxes = [0]
right_maxes = [0]
for j in range(len(d[i])):
left_maxes.append(max(left_maxes[-1], answer[d[i][j]]))
right_maxes.append(max(right_maxes[-1], answer[d[i][len(d[i]) - j - 1]]))
left_amount = 0
for j in range(len(d[i + 1])):
while left_amount < len(d[i]) and d[i][left_amount] < d[i + 1][j]:
left_amount += 1
answer[d[i + 1][j]] = max(
left_maxes[left_amount], right_maxes[len(d[i]) - left_amount] + 1
)
res = 0
for ans in answer:
res += ans
print(res)
def __starting_point():
main()
__starting_point() | FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST LIST FOR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR FOR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this integer is between 1 and 100 000, inclusive. It is possible that some cards have the same integers on them.
Vasily decided to sort the cards. To do this, he repeatedly takes the top card from the deck, and if the number on it equals the minimum number written on the cards in the deck, then he places the card away. Otherwise, he puts it under the deck and takes the next card from the top, and so on. The process ends as soon as there are no cards in the deck. You can assume that Vasily always knows the minimum number written on some card in the remaining deck, but doesn't know where this card (or these cards) is.
You are to determine the total number of times Vasily takes the top card from the deck.
-----Input-----
The first line contains single integer n (1 ≤ n ≤ 100 000) — the number of cards in the deck.
The second line contains a sequence of n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 100 000), where a_{i} is the number written on the i-th from top card in the deck.
-----Output-----
Print the total number of times Vasily takes the top card from the deck.
-----Examples-----
Input
4
6 3 1 2
Output
7
Input
1
1000
Output
1
Input
7
3 3 3 3 3 3 3
Output
7
-----Note-----
In the first example Vasily at first looks at the card with number 6 on it, puts it under the deck, then on the card with number 3, puts it under the deck, and then on the card with number 1. He places away the card with 1, because the number written on it is the minimum among the remaining cards. After that the cards from top to bottom are [2, 6, 3]. Then Vasily looks at the top card with number 2 and puts it away. After that the cards from top to bottom are [6, 3]. Then Vasily looks at card 6, puts it under the deck, then at card 3 and puts it away. Then there is only one card with number 6 on it, and Vasily looks at it and puts it away. Thus, in total Vasily looks at 7 cards. | import sys
fin = sys.stdin
fout = sys.stdout
n = int(fin.readline())
a = list(map(int, fin.readline().split()))
def solution(n, a):
sorted_arr = [(i, elem) for i, elem in enumerate(a)]
sorted_arr.sort(key=lambda x: (x[1], x[0]))
sorted_indexes = [x[0] for x in sorted_arr]
cnt = 0
current_n = n
prev_index = sorted_indexes[0]
prev_elem = sorted_arr[0][1]
cur_len = 1
i = 1
while i < n:
cur_index = sorted_indexes[i]
cur_elem = sorted_arr[i][1]
if prev_index < cur_index:
cur_len += 1
prev_index = sorted_indexes[i]
prev_elem = sorted_arr[i][1]
elif i + 1 < n and cur_elem == sorted_arr[i + 1][1]:
penalty = 1
last_penalty_ind = sorted_indexes[i]
while i + 1 < n and sorted_arr[i + 1][1] == cur_elem:
if sorted_arr[i + 1][0] >= prev_index:
cur_len += 1
else:
penalty += 1
last_penalty_ind = sorted_indexes[i + 1]
i += 1
cnt += current_n
current_n -= cur_len
cur_len = penalty
prev_elem = cur_elem
prev_index = last_penalty_ind
else:
cnt += current_n
current_n -= cur_len
cur_len = 1
prev_index = sorted_indexes[i]
prev_elem = sorted_arr[i][1]
i += 1
cnt += current_n
return cnt
cnt = solution(n, a)
fout.write(str(cnt))
fout.close() | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this integer is between 1 and 100 000, inclusive. It is possible that some cards have the same integers on them.
Vasily decided to sort the cards. To do this, he repeatedly takes the top card from the deck, and if the number on it equals the minimum number written on the cards in the deck, then he places the card away. Otherwise, he puts it under the deck and takes the next card from the top, and so on. The process ends as soon as there are no cards in the deck. You can assume that Vasily always knows the minimum number written on some card in the remaining deck, but doesn't know where this card (or these cards) is.
You are to determine the total number of times Vasily takes the top card from the deck.
-----Input-----
The first line contains single integer n (1 ≤ n ≤ 100 000) — the number of cards in the deck.
The second line contains a sequence of n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 100 000), where a_{i} is the number written on the i-th from top card in the deck.
-----Output-----
Print the total number of times Vasily takes the top card from the deck.
-----Examples-----
Input
4
6 3 1 2
Output
7
Input
1
1000
Output
1
Input
7
3 3 3 3 3 3 3
Output
7
-----Note-----
In the first example Vasily at first looks at the card with number 6 on it, puts it under the deck, then on the card with number 3, puts it under the deck, and then on the card with number 1. He places away the card with 1, because the number written on it is the minimum among the remaining cards. After that the cards from top to bottom are [2, 6, 3]. Then Vasily looks at the top card with number 2 and puts it away. After that the cards from top to bottom are [6, 3]. Then Vasily looks at card 6, puts it under the deck, then at card 3 and puts it away. Then there is only one card with number 6 on it, and Vasily looks at it and puts it away. Thus, in total Vasily looks at 7 cards. | n = int(input())
s = list(map(int, input().split(" ")))
a = []
for i in range(max(s)):
a.append([])
for i in range(len(s)):
a[s[i] - 1].append(i)
a = list(filter(lambda x: x != [], a))
if len(a) > 1:
for i in range(1, len(a)):
if len(a[i]) > 1:
s = a[i - 1][-1]
if s > a[i][0] and s < a[i][-1]:
for j in range(1, len(a[i])):
if s < a[i][j]:
a[i] = a[i][j:] + a[i][:j]
break
t = []
for i in a:
t += i
c = 0
x = t[0] + 1
i = n - 1
while i > 0:
if t[i] < t[i - 1]:
k = t[i] - t[i - 1] + n
else:
k = t[i] - t[i - 1]
c += k
x -= c // n
i -= 1
print(c + x) | 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 FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR LIST VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this integer is between 1 and 100 000, inclusive. It is possible that some cards have the same integers on them.
Vasily decided to sort the cards. To do this, he repeatedly takes the top card from the deck, and if the number on it equals the minimum number written on the cards in the deck, then he places the card away. Otherwise, he puts it under the deck and takes the next card from the top, and so on. The process ends as soon as there are no cards in the deck. You can assume that Vasily always knows the minimum number written on some card in the remaining deck, but doesn't know where this card (or these cards) is.
You are to determine the total number of times Vasily takes the top card from the deck.
-----Input-----
The first line contains single integer n (1 ≤ n ≤ 100 000) — the number of cards in the deck.
The second line contains a sequence of n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 100 000), where a_{i} is the number written on the i-th from top card in the deck.
-----Output-----
Print the total number of times Vasily takes the top card from the deck.
-----Examples-----
Input
4
6 3 1 2
Output
7
Input
1
1000
Output
1
Input
7
3 3 3 3 3 3 3
Output
7
-----Note-----
In the first example Vasily at first looks at the card with number 6 on it, puts it under the deck, then on the card with number 3, puts it under the deck, and then on the card with number 1. He places away the card with 1, because the number written on it is the minimum among the remaining cards. After that the cards from top to bottom are [2, 6, 3]. Then Vasily looks at the top card with number 2 and puts it away. After that the cards from top to bottom are [6, 3]. Then Vasily looks at card 6, puts it under the deck, then at card 3 and puts it away. Then there is only one card with number 6 on it, and Vasily looks at it and puts it away. Thus, in total Vasily looks at 7 cards. | def solve(arr):
b = list(enumerate(arr))
b.sort(key=lambda x: x[1])
res = 0
prev_right = -1
prev_level = -1
cur_level = 1
cur_value = -1
cur_right = -1
for i, v in b:
if v > cur_value:
prev_level = cur_level
prev_right = cur_right
cur_value = v
cur_right = i
if i < prev_right:
cur_level = prev_level + 1
res += cur_level
elif i < prev_right:
res += prev_level + 1
if cur_level != prev_level + 1:
cur_level = prev_level + 1
cur_right = i
cur_right = max(cur_right, i)
else:
res += prev_level
if cur_level == prev_level:
cur_right = max(cur_right, i)
return res
n = int(input())
a = [int(x) for x in input().strip().split()]
print(solve(a)) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You are given an array a and you need to generate an array b. You are allowed to apply only one type of operation on the array a, any number of times.
In one operation you can swap a_{i} with a_{i+1} only if a_{i}+a_{i+1} is even.
Array b thus generated by applying above operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Example 1:
Input:
N=3
a[]={1,3,5}
Output:
5,3,1
Explanation: [1,3,5],[1,5,3],[3,1,5],[3,5,1],
[5,1,3] and [5,3,1] are all possible
values of array b while the last one is
lexicographically largest.
Example 2:
Input:
N=4
a[]={1,3,4,2}
Output:
b[]={3,1,4,2}
Explanation: [1,3,4,2],[1,3,2,4],[3,1,2,4] and
[3,1,4,2] are all possible values of b among
which the last one is lexicographically largest one.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lexicographically Largest() which takes the array arr[], and its size N as input parameters and returns the array b.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= arr[i] <= 10^{5}
Array may contain duplicate elements. | class Solution:
def lexicographicallyLargest(self, a, n):
i = 0
temp, res = [], []
while i < n:
while i < n and a[i] % 2 == 0:
res.append(a[i])
i += 1
if len(res) > 0:
res.sort(reverse=True)
temp += res
res = []
while i < n and a[i] % 2 != 0:
res.append(a[i])
i += 1
if len(res) > 0:
res.sort(reverse=True)
temp += res
res = []
return temp | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR LIST LIST WHILE VAR VAR WHILE VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR LIST WHILE VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR LIST RETURN VAR |
You are given an array a and you need to generate an array b. You are allowed to apply only one type of operation on the array a, any number of times.
In one operation you can swap a_{i} with a_{i+1} only if a_{i}+a_{i+1} is even.
Array b thus generated by applying above operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Example 1:
Input:
N=3
a[]={1,3,5}
Output:
5,3,1
Explanation: [1,3,5],[1,5,3],[3,1,5],[3,5,1],
[5,1,3] and [5,3,1] are all possible
values of array b while the last one is
lexicographically largest.
Example 2:
Input:
N=4
a[]={1,3,4,2}
Output:
b[]={3,1,4,2}
Explanation: [1,3,4,2],[1,3,2,4],[3,1,2,4] and
[3,1,4,2] are all possible values of b among
which the last one is lexicographically largest one.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lexicographically Largest() which takes the array arr[], and its size N as input parameters and returns the array b.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= arr[i] <= 10^{5}
Array may contain duplicate elements. | class Solution:
def lexicographicallyLargest(self, a, n):
ans = []
last = 0
for i in range(n - 1):
if (a[i] + a[i + 1]) % 2 == 1:
ans.extend(sorted(a[last : i + 1], reverse=True))
last = i + 1
ans.extend(sorted(a[last:], reverse=True))
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR |
You are given an array a and you need to generate an array b. You are allowed to apply only one type of operation on the array a, any number of times.
In one operation you can swap a_{i} with a_{i+1} only if a_{i}+a_{i+1} is even.
Array b thus generated by applying above operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Example 1:
Input:
N=3
a[]={1,3,5}
Output:
5,3,1
Explanation: [1,3,5],[1,5,3],[3,1,5],[3,5,1],
[5,1,3] and [5,3,1] are all possible
values of array b while the last one is
lexicographically largest.
Example 2:
Input:
N=4
a[]={1,3,4,2}
Output:
b[]={3,1,4,2}
Explanation: [1,3,4,2],[1,3,2,4],[3,1,2,4] and
[3,1,4,2] are all possible values of b among
which the last one is lexicographically largest one.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lexicographically Largest() which takes the array arr[], and its size N as input parameters and returns the array b.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= arr[i] <= 10^{5}
Array may contain duplicate elements. | class Solution:
def lexicographicallyLargest(self, a, n):
j = 0
for i in range(1, n):
if a[i - 1] % 2 == a[i] % 2:
continue
else:
b = a[j:i]
b.sort(reverse=True)
a[j:i] = b
j = i
if j < n:
b = a[j:n]
b.sort(reverse=True)
a[j:n] = b
return a | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR |
You are given an array a and you need to generate an array b. You are allowed to apply only one type of operation on the array a, any number of times.
In one operation you can swap a_{i} with a_{i+1} only if a_{i}+a_{i+1} is even.
Array b thus generated by applying above operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Example 1:
Input:
N=3
a[]={1,3,5}
Output:
5,3,1
Explanation: [1,3,5],[1,5,3],[3,1,5],[3,5,1],
[5,1,3] and [5,3,1] are all possible
values of array b while the last one is
lexicographically largest.
Example 2:
Input:
N=4
a[]={1,3,4,2}
Output:
b[]={3,1,4,2}
Explanation: [1,3,4,2],[1,3,2,4],[3,1,2,4] and
[3,1,4,2] are all possible values of b among
which the last one is lexicographically largest one.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lexicographically Largest() which takes the array arr[], and its size N as input parameters and returns the array b.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= arr[i] <= 10^{5}
Array may contain duplicate elements. | class Solution:
def lexicographicallyLargest(self, a, n):
prev, curr = a[0] % 2, 0
for index, i in enumerate(a):
if i % 2 == prev:
curr += 1
else:
a[index - curr : index] = sorted(a[index - curr : index], reverse=True)
curr, prev = 1, i % 2
a[n - curr :] = sorted(a[n - curr :], reverse=True)
return a | CLASS_DEF FUNC_DEF ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR |
You are given an array a and you need to generate an array b. You are allowed to apply only one type of operation on the array a, any number of times.
In one operation you can swap a_{i} with a_{i+1} only if a_{i}+a_{i+1} is even.
Array b thus generated by applying above operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Example 1:
Input:
N=3
a[]={1,3,5}
Output:
5,3,1
Explanation: [1,3,5],[1,5,3],[3,1,5],[3,5,1],
[5,1,3] and [5,3,1] are all possible
values of array b while the last one is
lexicographically largest.
Example 2:
Input:
N=4
a[]={1,3,4,2}
Output:
b[]={3,1,4,2}
Explanation: [1,3,4,2],[1,3,2,4],[3,1,2,4] and
[3,1,4,2] are all possible values of b among
which the last one is lexicographically largest one.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lexicographically Largest() which takes the array arr[], and its size N as input parameters and returns the array b.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= arr[i] <= 10^{5}
Array may contain duplicate elements. | class Solution:
def lexicographicallyLargest(self, a, n):
ans = []
temp = []
temp.append(a[0])
for i in range(1, len(a)):
if (a[i] + a[i - 1]) % 2 == 0:
temp.append(a[i])
continue
temp.sort(reverse=True)
ans += temp
temp = []
temp.append(a[i])
temp.sort(reverse=True)
ans = ans + temp
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR |
You are given an array a and you need to generate an array b. You are allowed to apply only one type of operation on the array a, any number of times.
In one operation you can swap a_{i} with a_{i+1} only if a_{i}+a_{i+1} is even.
Array b thus generated by applying above operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Example 1:
Input:
N=3
a[]={1,3,5}
Output:
5,3,1
Explanation: [1,3,5],[1,5,3],[3,1,5],[3,5,1],
[5,1,3] and [5,3,1] are all possible
values of array b while the last one is
lexicographically largest.
Example 2:
Input:
N=4
a[]={1,3,4,2}
Output:
b[]={3,1,4,2}
Explanation: [1,3,4,2],[1,3,2,4],[3,1,2,4] and
[3,1,4,2] are all possible values of b among
which the last one is lexicographically largest one.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lexicographically Largest() which takes the array arr[], and its size N as input parameters and returns the array b.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= arr[i] <= 10^{5}
Array may contain duplicate elements. | class Solution:
def lexicographicallyLargest(self, a, n):
b = []
accept = "none"
t = []
for i in a:
if i % 2 == 0:
if accept == "none":
t.append(i)
accept = "even"
elif accept == "even":
t.append(i)
else:
b += sorted(t, reverse=True)
t = []
t.append(i)
accept = "even"
elif accept == "none":
t.append(i)
accept = "odd"
elif accept == "odd":
t.append(i)
else:
b += sorted(t, reverse=True)
t = []
t.append(i)
accept = "odd"
b += sorted(t, reverse=True)
return b | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING IF VAR STRING EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING IF VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING IF VAR STRING EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR NUMBER RETURN VAR |
You are given an array a and you need to generate an array b. You are allowed to apply only one type of operation on the array a, any number of times.
In one operation you can swap a_{i} with a_{i+1} only if a_{i}+a_{i+1} is even.
Array b thus generated by applying above operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Example 1:
Input:
N=3
a[]={1,3,5}
Output:
5,3,1
Explanation: [1,3,5],[1,5,3],[3,1,5],[3,5,1],
[5,1,3] and [5,3,1] are all possible
values of array b while the last one is
lexicographically largest.
Example 2:
Input:
N=4
a[]={1,3,4,2}
Output:
b[]={3,1,4,2}
Explanation: [1,3,4,2],[1,3,2,4],[3,1,2,4] and
[3,1,4,2] are all possible values of b among
which the last one is lexicographically largest one.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lexicographically Largest() which takes the array arr[], and its size N as input parameters and returns the array b.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= arr[i] <= 10^{5}
Array may contain duplicate elements. | class Solution:
def lexicographicallyLargest(self, a, n):
ans = []
cur = a[0]
v = [cur]
for i in range(1, n):
if cur & 1 == a[i] & 1:
v.append(a[i])
else:
for x in sorted(v, reverse=True):
ans.append(x)
cur = a[i]
v = [cur]
for x in sorted(v, reverse=True):
ans.append(x)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR |
You are given an array a and you need to generate an array b. You are allowed to apply only one type of operation on the array a, any number of times.
In one operation you can swap a_{i} with a_{i+1} only if a_{i}+a_{i+1} is even.
Array b thus generated by applying above operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Example 1:
Input:
N=3
a[]={1,3,5}
Output:
5,3,1
Explanation: [1,3,5],[1,5,3],[3,1,5],[3,5,1],
[5,1,3] and [5,3,1] are all possible
values of array b while the last one is
lexicographically largest.
Example 2:
Input:
N=4
a[]={1,3,4,2}
Output:
b[]={3,1,4,2}
Explanation: [1,3,4,2],[1,3,2,4],[3,1,2,4] and
[3,1,4,2] are all possible values of b among
which the last one is lexicographically largest one.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lexicographically Largest() which takes the array arr[], and its size N as input parameters and returns the array b.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= arr[i] <= 10^{5}
Array may contain duplicate elements. | class Solution:
def lexicographicallyLargest(self, a, n):
b = []
small = []
for i in range(n):
if not small:
small.append(a[i])
elif not (small[-1] + a[i]) % 2:
small.append(a[i])
else:
small.sort()
while small:
b.append(small.pop())
small.append(a[i])
if small:
small.sort()
while small:
b.append(small.pop())
return b | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR WHILE VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR WHILE VAR EXPR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR |
You are given an array a and you need to generate an array b. You are allowed to apply only one type of operation on the array a, any number of times.
In one operation you can swap a_{i} with a_{i+1} only if a_{i}+a_{i+1} is even.
Array b thus generated by applying above operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Example 1:
Input:
N=3
a[]={1,3,5}
Output:
5,3,1
Explanation: [1,3,5],[1,5,3],[3,1,5],[3,5,1],
[5,1,3] and [5,3,1] are all possible
values of array b while the last one is
lexicographically largest.
Example 2:
Input:
N=4
a[]={1,3,4,2}
Output:
b[]={3,1,4,2}
Explanation: [1,3,4,2],[1,3,2,4],[3,1,2,4] and
[3,1,4,2] are all possible values of b among
which the last one is lexicographically largest one.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lexicographically Largest() which takes the array arr[], and its size N as input parameters and returns the array b.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= arr[i] <= 10^{5}
Array may contain duplicate elements. | class Solution:
def lexicographicallyLargest(self, l, n):
st = 0
end = 0
for i in range(n - 1):
if (l[i] + l[i + 1]) % 2 == 0:
end += 1
else:
l[st : end + 1] = sorted(l[st : end + 1], reverse=True)
st = end + 1
end = end + 1
l[st : end + 1] = sorted(l[st : end + 1], reverse=True)
return l | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR |
You are given an array a and you need to generate an array b. You are allowed to apply only one type of operation on the array a, any number of times.
In one operation you can swap a_{i} with a_{i+1} only if a_{i}+a_{i+1} is even.
Array b thus generated by applying above operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Example 1:
Input:
N=3
a[]={1,3,5}
Output:
5,3,1
Explanation: [1,3,5],[1,5,3],[3,1,5],[3,5,1],
[5,1,3] and [5,3,1] are all possible
values of array b while the last one is
lexicographically largest.
Example 2:
Input:
N=4
a[]={1,3,4,2}
Output:
b[]={3,1,4,2}
Explanation: [1,3,4,2],[1,3,2,4],[3,1,2,4] and
[3,1,4,2] are all possible values of b among
which the last one is lexicographically largest one.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lexicographically Largest() which takes the array arr[], and its size N as input parameters and returns the array b.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= arr[i] <= 10^{5}
Array may contain duplicate elements. | class Solution:
def lexicographicallyLargest(self, a, n):
i, j = 0, 1
while i < n - 1 and j < n:
if (a[i] + a[j]) % 2 == 0:
j += 1
else:
a[i:j] = sorted(a[i:j], reverse=True)
i = j
j += 1
if j == n:
a[i:j] = sorted(a[i:j], reverse=True)
return a | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR |
You are given an array a and you need to generate an array b. You are allowed to apply only one type of operation on the array a, any number of times.
In one operation you can swap a_{i} with a_{i+1} only if a_{i}+a_{i+1} is even.
Array b thus generated by applying above operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Example 1:
Input:
N=3
a[]={1,3,5}
Output:
5,3,1
Explanation: [1,3,5],[1,5,3],[3,1,5],[3,5,1],
[5,1,3] and [5,3,1] are all possible
values of array b while the last one is
lexicographically largest.
Example 2:
Input:
N=4
a[]={1,3,4,2}
Output:
b[]={3,1,4,2}
Explanation: [1,3,4,2],[1,3,2,4],[3,1,2,4] and
[3,1,4,2] are all possible values of b among
which the last one is lexicographically largest one.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lexicographically Largest() which takes the array arr[], and its size N as input parameters and returns the array b.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= arr[i] <= 10^{5}
Array may contain duplicate elements. | class Solution:
def lexicographicallyLargest(self, a, n):
s = 0
for i in range(n - 1):
if (a[i] + a[i + 1]) % 2 == 0:
a[i + 1], a[i] = a[i], a[i + 1]
else:
if i - s >= 1:
a[s : i + 1] = sorted(a[s : i + 1], reverse=True)
s = i + 1
if s == 0:
a.sort(reverse=True)
elif n - s >= 1:
a[s:] = sorted(a[s:], reverse=True)
return a | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR |
You are given an array a and you need to generate an array b. You are allowed to apply only one type of operation on the array a, any number of times.
In one operation you can swap a_{i} with a_{i+1} only if a_{i}+a_{i+1} is even.
Array b thus generated by applying above operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Example 1:
Input:
N=3
a[]={1,3,5}
Output:
5,3,1
Explanation: [1,3,5],[1,5,3],[3,1,5],[3,5,1],
[5,1,3] and [5,3,1] are all possible
values of array b while the last one is
lexicographically largest.
Example 2:
Input:
N=4
a[]={1,3,4,2}
Output:
b[]={3,1,4,2}
Explanation: [1,3,4,2],[1,3,2,4],[3,1,2,4] and
[3,1,4,2] are all possible values of b among
which the last one is lexicographically largest one.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lexicographically Largest() which takes the array arr[], and its size N as input parameters and returns the array b.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= arr[i] <= 10^{5}
Array may contain duplicate elements. | class Solution:
def lexicographicallyLargest(self, a, n):
e = a[0] % 2
s = 0
for i, j in enumerate(a):
if not j % 2 == e:
a[s:i] = sorted(a[s:i], reverse=True)
e = j % 2
s = i
a[s:n] = sorted(a[s:n], reverse=True)
return a | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR |
You are given an array a and you need to generate an array b. You are allowed to apply only one type of operation on the array a, any number of times.
In one operation you can swap a_{i} with a_{i+1} only if a_{i}+a_{i+1} is even.
Array b thus generated by applying above operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Example 1:
Input:
N=3
a[]={1,3,5}
Output:
5,3,1
Explanation: [1,3,5],[1,5,3],[3,1,5],[3,5,1],
[5,1,3] and [5,3,1] are all possible
values of array b while the last one is
lexicographically largest.
Example 2:
Input:
N=4
a[]={1,3,4,2}
Output:
b[]={3,1,4,2}
Explanation: [1,3,4,2],[1,3,2,4],[3,1,2,4] and
[3,1,4,2] are all possible values of b among
which the last one is lexicographically largest one.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lexicographically Largest() which takes the array arr[], and its size N as input parameters and returns the array b.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= arr[i] <= 10^{5}
Array may contain duplicate elements. | class Solution:
def lexicographicallyLargest(self, a, n):
i = 0
ans = []
while i < n:
j = i + 1
while j < n and a[j] % 2 == a[j - 1] % 2:
j += 1
tmp = a[i:j]
tmp.sort(reverse=True)
ans.extend(tmp)
i = j
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR |
You are given an array a and you need to generate an array b. You are allowed to apply only one type of operation on the array a, any number of times.
In one operation you can swap a_{i} with a_{i+1} only if a_{i}+a_{i+1} is even.
Array b thus generated by applying above operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Example 1:
Input:
N=3
a[]={1,3,5}
Output:
5,3,1
Explanation: [1,3,5],[1,5,3],[3,1,5],[3,5,1],
[5,1,3] and [5,3,1] are all possible
values of array b while the last one is
lexicographically largest.
Example 2:
Input:
N=4
a[]={1,3,4,2}
Output:
b[]={3,1,4,2}
Explanation: [1,3,4,2],[1,3,2,4],[3,1,2,4] and
[3,1,4,2] are all possible values of b among
which the last one is lexicographically largest one.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lexicographically Largest() which takes the array arr[], and its size N as input parameters and returns the array b.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= arr[i] <= 10^{5}
Array may contain duplicate elements. | class Solution:
def lexicographicallyLargest(self, a, n):
is_even, end = True, len(a) - 1
for i in range(len(a) - 1, -1, -1):
if a[i] % 2 == 0:
if is_even:
if i != 0:
continue
else:
self.quicksort(a, i, end)
self.reverse_list(a, i, end)
else:
self.quicksort(a, i + 1, end)
self.reverse_list(a, i + 1, end)
is_even, end = True, i
elif not is_even:
if i != 0:
continue
else:
self.quicksort(a, i, end)
self.reverse_list(a, i, end)
else:
self.quicksort(a, i + 1, end)
self.reverse_list(a, i + 1, end)
is_even, end = False, i
return a
def reverse_list(self, array, beg, fin):
if beg == fin:
return
else:
while beg < fin:
array[beg], array[fin] = array[fin], array[beg]
beg += 1
fin -= 1
return
def quicksort(self, array, low, high):
if low < high:
ind = self.partition(array, low, high)
self.quicksort(array, low, ind - 1)
self.quicksort(array, ind + 1, high)
return array
def partition(self, arr, lo, hi):
piv = arr[hi]
i, j = lo, hi - 1
while i < j:
while arr[i] <= piv and i < hi:
i += 1
while arr[j] >= piv and j > lo:
j -= 1
if i < j:
arr[i], arr[j] = arr[j], arr[i]
if arr[i] > piv:
arr[i], arr[hi] = arr[hi], arr[i]
return i | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER NUMBER IF VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR IF VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR |
You are given an array a and you need to generate an array b. You are allowed to apply only one type of operation on the array a, any number of times.
In one operation you can swap a_{i} with a_{i+1} only if a_{i}+a_{i+1} is even.
Array b thus generated by applying above operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Example 1:
Input:
N=3
a[]={1,3,5}
Output:
5,3,1
Explanation: [1,3,5],[1,5,3],[3,1,5],[3,5,1],
[5,1,3] and [5,3,1] are all possible
values of array b while the last one is
lexicographically largest.
Example 2:
Input:
N=4
a[]={1,3,4,2}
Output:
b[]={3,1,4,2}
Explanation: [1,3,4,2],[1,3,2,4],[3,1,2,4] and
[3,1,4,2] are all possible values of b among
which the last one is lexicographically largest one.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lexicographically Largest() which takes the array arr[], and its size N as input parameters and returns the array b.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= arr[i] <= 10^{5}
Array may contain duplicate elements. | class Solution:
def lexicographicallyLargest(self, a, n):
ans = []
i = 0
while i < n:
temp = []
if a[i] % 2 == 0:
while i < n and a[i] % 2 == 0:
temp.append(a[i])
i += 1
else:
while i < n and a[i] % 2 == 1:
temp.append(a[i])
i += 1
temp.sort()
ans.append(temp[::-1])
res = []
for i in ans:
for j in i:
res.append(j)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR LIST IF BIN_OP VAR VAR NUMBER NUMBER WHILE VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
You are given an array a and you need to generate an array b. You are allowed to apply only one type of operation on the array a, any number of times.
In one operation you can swap a_{i} with a_{i+1} only if a_{i}+a_{i+1} is even.
Array b thus generated by applying above operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Example 1:
Input:
N=3
a[]={1,3,5}
Output:
5,3,1
Explanation: [1,3,5],[1,5,3],[3,1,5],[3,5,1],
[5,1,3] and [5,3,1] are all possible
values of array b while the last one is
lexicographically largest.
Example 2:
Input:
N=4
a[]={1,3,4,2}
Output:
b[]={3,1,4,2}
Explanation: [1,3,4,2],[1,3,2,4],[3,1,2,4] and
[3,1,4,2] are all possible values of b among
which the last one is lexicographically largest one.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lexicographically Largest() which takes the array arr[], and its size N as input parameters and returns the array b.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= arr[i] <= 10^{5}
Array may contain duplicate elements. | class Solution:
def lexicographicallyLargest(self, a, n):
l = [a[0]]
for i in range(1, n):
if (a[i - 1] + a[i]) % 2 == 0:
l.append(a[i])
else:
l.sort(reverse=True)
a[i - len(l) : i] = l
l = [a[i]]
if len(l) != 1:
l.sort(reverse=True)
a[n - len(l) : n] = l
return a | CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
You are given an array a and you need to generate an array b. You are allowed to apply only one type of operation on the array a, any number of times.
In one operation you can swap a_{i} with a_{i+1} only if a_{i}+a_{i+1} is even.
Array b thus generated by applying above operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Example 1:
Input:
N=3
a[]={1,3,5}
Output:
5,3,1
Explanation: [1,3,5],[1,5,3],[3,1,5],[3,5,1],
[5,1,3] and [5,3,1] are all possible
values of array b while the last one is
lexicographically largest.
Example 2:
Input:
N=4
a[]={1,3,4,2}
Output:
b[]={3,1,4,2}
Explanation: [1,3,4,2],[1,3,2,4],[3,1,2,4] and
[3,1,4,2] are all possible values of b among
which the last one is lexicographically largest one.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lexicographically Largest() which takes the array arr[], and its size N as input parameters and returns the array b.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= arr[i] <= 10^{5}
Array may contain duplicate elements. | class Solution:
def lexicographicallyLargest(self, a, n):
temp = []
flag = False
st, en = 0, 0
for i in range(n):
if a[i] % 2 == 0:
if not flag and temp:
a[st:i] = sorted(temp, reverse=True)
temp = []
if not flag:
st = i
flag = True
temp.append(a[i])
else:
if flag and temp:
a[st:i] = sorted(temp, reverse=True)
temp = []
if flag:
st = i
flag = False
temp.append(a[i])
if temp:
a[st:] = sorted(temp, reverse=True)
return a | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST IF VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST IF VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER RETURN VAR |
You are given an array a and you need to generate an array b. You are allowed to apply only one type of operation on the array a, any number of times.
In one operation you can swap a_{i} with a_{i+1} only if a_{i}+a_{i+1} is even.
Array b thus generated by applying above operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Example 1:
Input:
N=3
a[]={1,3,5}
Output:
5,3,1
Explanation: [1,3,5],[1,5,3],[3,1,5],[3,5,1],
[5,1,3] and [5,3,1] are all possible
values of array b while the last one is
lexicographically largest.
Example 2:
Input:
N=4
a[]={1,3,4,2}
Output:
b[]={3,1,4,2}
Explanation: [1,3,4,2],[1,3,2,4],[3,1,2,4] and
[3,1,4,2] are all possible values of b among
which the last one is lexicographically largest one.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lexicographically Largest() which takes the array arr[], and its size N as input parameters and returns the array b.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= arr[i] <= 10^{5}
Array may contain duplicate elements. | class Solution:
def lexicographicallyLargest(self, a, n):
b = a[:]
x = list(map(lambda y: y % 2, a))
flag = x[0]
p1 = 0
p2 = 1
for i in range(1, n):
if x[i] == flag:
p2 += 1
else:
b[p1:p2] = sorted(b[p1:p2])[::-1]
flag = x[i]
p1 = i
p2 = i + 1
b[p1:p2] = sorted(b[p1:p2])[::-1]
return b | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR |
You are given an array a and you need to generate an array b. You are allowed to apply only one type of operation on the array a, any number of times.
In one operation you can swap a_{i} with a_{i+1} only if a_{i}+a_{i+1} is even.
Array b thus generated by applying above operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Example 1:
Input:
N=3
a[]={1,3,5}
Output:
5,3,1
Explanation: [1,3,5],[1,5,3],[3,1,5],[3,5,1],
[5,1,3] and [5,3,1] are all possible
values of array b while the last one is
lexicographically largest.
Example 2:
Input:
N=4
a[]={1,3,4,2}
Output:
b[]={3,1,4,2}
Explanation: [1,3,4,2],[1,3,2,4],[3,1,2,4] and
[3,1,4,2] are all possible values of b among
which the last one is lexicographically largest one.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lexicographically Largest() which takes the array arr[], and its size N as input parameters and returns the array b.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= arr[i] <= 10^{5}
Array may contain duplicate elements. | class Solution:
def lexicographicallyLargest(self, a, n):
if a[0] % 2 == 0:
prev = 1
else:
prev = 0
k = []
k.append(a[0])
res = []
for i in a[1:]:
if i % 2 == 0 and prev == 1:
k.append(i)
elif i % 2 == 0 and prev == 0:
res.extend(sorted(k)[::-1])
k = []
k.append(i)
prev = 1
elif i % 2 != 0 and prev == 0:
k.append(i)
else:
res.extend(sorted(k)[::-1])
k = []
k.append(i)
prev = 0
res.extend(sorted(k)[::-1])
return res | CLASS_DEF FUNC_DEF IF BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN VAR |
You are given an array a and you need to generate an array b. You are allowed to apply only one type of operation on the array a, any number of times.
In one operation you can swap a_{i} with a_{i+1} only if a_{i}+a_{i+1} is even.
Array b thus generated by applying above operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Example 1:
Input:
N=3
a[]={1,3,5}
Output:
5,3,1
Explanation: [1,3,5],[1,5,3],[3,1,5],[3,5,1],
[5,1,3] and [5,3,1] are all possible
values of array b while the last one is
lexicographically largest.
Example 2:
Input:
N=4
a[]={1,3,4,2}
Output:
b[]={3,1,4,2}
Explanation: [1,3,4,2],[1,3,2,4],[3,1,2,4] and
[3,1,4,2] are all possible values of b among
which the last one is lexicographically largest one.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lexicographically Largest() which takes the array arr[], and its size N as input parameters and returns the array b.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= arr[i] <= 10^{5}
Array may contain duplicate elements. | class Solution:
def lexicographicallyLargest(self, a, n):
oddeven = self.oddeve(a, n)
result = a[:]
for i in oddeven:
result[i[0] : i[1]] = sorted(a[i[0] : i[1]], reverse=True)
return result
def oddeve(self, a, n):
system = []
i = 0
while i < n - 1:
oddeve = a[i] % 2
j = i + 1
while j < n and oddeve == a[j] % 2:
j += 1
if i != j - 1:
system.append([i, j])
i = j
return system | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR RETURN VAR |
You are given an array a and you need to generate an array b. You are allowed to apply only one type of operation on the array a, any number of times.
In one operation you can swap a_{i} with a_{i+1} only if a_{i}+a_{i+1} is even.
Array b thus generated by applying above operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Example 1:
Input:
N=3
a[]={1,3,5}
Output:
5,3,1
Explanation: [1,3,5],[1,5,3],[3,1,5],[3,5,1],
[5,1,3] and [5,3,1] are all possible
values of array b while the last one is
lexicographically largest.
Example 2:
Input:
N=4
a[]={1,3,4,2}
Output:
b[]={3,1,4,2}
Explanation: [1,3,4,2],[1,3,2,4],[3,1,2,4] and
[3,1,4,2] are all possible values of b among
which the last one is lexicographically largest one.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lexicographically Largest() which takes the array arr[], and its size N as input parameters and returns the array b.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= arr[i] <= 10^{5}
Array may contain duplicate elements. | class Solution:
def lexicographicallyLargest(self, a, n):
cur_elems = []
result = []
for elem in a:
if not cur_elems or elem % 2 == cur_elems[-1] % 2:
cur_elems.append(elem)
else:
cur_elems.sort(reverse=True)
result.extend(cur_elems)
cur_elems = [elem]
cur_elems.sort(reverse=True)
result.extend(cur_elems)
return result | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR |
You are given an array a and you need to generate an array b. You are allowed to apply only one type of operation on the array a, any number of times.
In one operation you can swap a_{i} with a_{i+1} only if a_{i}+a_{i+1} is even.
Array b thus generated by applying above operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Example 1:
Input:
N=3
a[]={1,3,5}
Output:
5,3,1
Explanation: [1,3,5],[1,5,3],[3,1,5],[3,5,1],
[5,1,3] and [5,3,1] are all possible
values of array b while the last one is
lexicographically largest.
Example 2:
Input:
N=4
a[]={1,3,4,2}
Output:
b[]={3,1,4,2}
Explanation: [1,3,4,2],[1,3,2,4],[3,1,2,4] and
[3,1,4,2] are all possible values of b among
which the last one is lexicographically largest one.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lexicographically Largest() which takes the array arr[], and its size N as input parameters and returns the array b.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= arr[i] <= 10^{5}
Array may contain duplicate elements. | class Solution:
def lexicographicallyLargest(self, a, n):
out = [a[0]]
v = []
for i in range(1, n):
if out[-1] % 2 != a[i] % 2:
out.sort(reverse=True)
v.extend(out)
out.clear()
out.append(a[i])
else:
out.append(a[i])
out.sort(reverse=True)
v.extend(out)
return v | CLASS_DEF FUNC_DEF ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR |
You are given an array a and you need to generate an array b. You are allowed to apply only one type of operation on the array a, any number of times.
In one operation you can swap a_{i} with a_{i+1} only if a_{i}+a_{i+1} is even.
Array b thus generated by applying above operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Example 1:
Input:
N=3
a[]={1,3,5}
Output:
5,3,1
Explanation: [1,3,5],[1,5,3],[3,1,5],[3,5,1],
[5,1,3] and [5,3,1] are all possible
values of array b while the last one is
lexicographically largest.
Example 2:
Input:
N=4
a[]={1,3,4,2}
Output:
b[]={3,1,4,2}
Explanation: [1,3,4,2],[1,3,2,4],[3,1,2,4] and
[3,1,4,2] are all possible values of b among
which the last one is lexicographically largest one.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lexicographically Largest() which takes the array arr[], and its size N as input parameters and returns the array b.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= arr[i] <= 10^{5}
Array may contain duplicate elements. | class Solution:
def lexicographicallyLargest(self, a, n):
i = 0
ans = []
while i < n:
t = [a[i]]
i += 1
while i < n:
if t[-1] % 2 == 0 and a[i] % 2 == 0:
t.append(a[i])
elif t[-1] % 2 and a[i] % 2:
t.append(a[i])
else:
break
i += 1
if len(t) > 1:
ans += sorted(t, reverse=True)
else:
ans.append(t[0])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR ASSIGN VAR LIST VAR VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
You are given an array a and you need to generate an array b. You are allowed to apply only one type of operation on the array a, any number of times.
In one operation you can swap a_{i} with a_{i+1} only if a_{i}+a_{i+1} is even.
Array b thus generated by applying above operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Example 1:
Input:
N=3
a[]={1,3,5}
Output:
5,3,1
Explanation: [1,3,5],[1,5,3],[3,1,5],[3,5,1],
[5,1,3] and [5,3,1] are all possible
values of array b while the last one is
lexicographically largest.
Example 2:
Input:
N=4
a[]={1,3,4,2}
Output:
b[]={3,1,4,2}
Explanation: [1,3,4,2],[1,3,2,4],[3,1,2,4] and
[3,1,4,2] are all possible values of b among
which the last one is lexicographically largest one.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lexicographically Largest() which takes the array arr[], and its size N as input parameters and returns the array b.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= arr[i] <= 10^{5}
Array may contain duplicate elements. | class Solution:
def lexicographicallyLargest(self, a, n):
ans = []
t = []
for i in a:
if not t:
t.append(i)
elif t[-1] % 2 == i % 2:
t.append(i)
else:
ans.append(t)
t = [i]
if t:
ans.append(t)
n = len(ans)
for i in range(n):
ans[i].sort(reverse=True)
res = []
for i in ans:
res.extend(i)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR |
You are given an array a and you need to generate an array b. You are allowed to apply only one type of operation on the array a, any number of times.
In one operation you can swap a_{i} with a_{i+1} only if a_{i}+a_{i+1} is even.
Array b thus generated by applying above operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Example 1:
Input:
N=3
a[]={1,3,5}
Output:
5,3,1
Explanation: [1,3,5],[1,5,3],[3,1,5],[3,5,1],
[5,1,3] and [5,3,1] are all possible
values of array b while the last one is
lexicographically largest.
Example 2:
Input:
N=4
a[]={1,3,4,2}
Output:
b[]={3,1,4,2}
Explanation: [1,3,4,2],[1,3,2,4],[3,1,2,4] and
[3,1,4,2] are all possible values of b among
which the last one is lexicographically largest one.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lexicographically Largest() which takes the array arr[], and its size N as input parameters and returns the array b.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= arr[i] <= 10^{5}
Array may contain duplicate elements. | class Solution:
def lexicographicallyLargest(self, a, n):
swapBoundaries = {}
prev = None
bound = []
i = 0
for num in a:
if prev is None:
bound.append(num)
prev = num % 2
elif prev == num % 2:
bound.append(num)
else:
swapBoundaries[i] = bound
i += 1
bound = [num]
prev = num % 2
if bound:
swapBoundaries[i] = bound
i += 1
array = []
for x in range(i):
array += sorted(swapBoundaries[x], reverse=True)
return array | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NONE ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR |
You are given an array a and you need to generate an array b. You are allowed to apply only one type of operation on the array a, any number of times.
In one operation you can swap a_{i} with a_{i+1} only if a_{i}+a_{i+1} is even.
Array b thus generated by applying above operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Example 1:
Input:
N=3
a[]={1,3,5}
Output:
5,3,1
Explanation: [1,3,5],[1,5,3],[3,1,5],[3,5,1],
[5,1,3] and [5,3,1] are all possible
values of array b while the last one is
lexicographically largest.
Example 2:
Input:
N=4
a[]={1,3,4,2}
Output:
b[]={3,1,4,2}
Explanation: [1,3,4,2],[1,3,2,4],[3,1,2,4] and
[3,1,4,2] are all possible values of b among
which the last one is lexicographically largest one.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lexicographically Largest() which takes the array arr[], and its size N as input parameters and returns the array b.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= arr[i] <= 10^{5}
Array may contain duplicate elements. | class Solution:
def lexicographicallyLargest(self, a, n):
chunk = 0
oe = a[0] % 2
tmp = [(chunk, a[0])]
for i in a[1:]:
if i % 2 != oe:
chunk += 1
oe = i % 2
tmp.append((chunk, i))
tmp.sort(key=lambda x: (x[0], -x[1]))
return list(map(lambda x: x[1], tmp)) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST VAR VAR NUMBER FOR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR |
You are given an array a and you need to generate an array b. You are allowed to apply only one type of operation on the array a, any number of times.
In one operation you can swap a_{i} with a_{i+1} only if a_{i}+a_{i+1} is even.
Array b thus generated by applying above operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Example 1:
Input:
N=3
a[]={1,3,5}
Output:
5,3,1
Explanation: [1,3,5],[1,5,3],[3,1,5],[3,5,1],
[5,1,3] and [5,3,1] are all possible
values of array b while the last one is
lexicographically largest.
Example 2:
Input:
N=4
a[]={1,3,4,2}
Output:
b[]={3,1,4,2}
Explanation: [1,3,4,2],[1,3,2,4],[3,1,2,4] and
[3,1,4,2] are all possible values of b among
which the last one is lexicographically largest one.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lexicographically Largest() which takes the array arr[], and its size N as input parameters and returns the array b.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= arr[i] <= 10^{5}
Array may contain duplicate elements. | class Solution:
def lexicographicallyLargest(self, a, n):
pr = 0
i = 1
while i < n:
if a[i] % 2 != a[i - 1] % 2:
a[pr:i] = sorted(a[pr:i], reverse=True)
pr = i
i += 1
a[pr:i] = sorted(a[pr:i], reverse=True)
return a | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR |
You are given an array a and you need to generate an array b. You are allowed to apply only one type of operation on the array a, any number of times.
In one operation you can swap a_{i} with a_{i+1} only if a_{i}+a_{i+1} is even.
Array b thus generated by applying above operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Example 1:
Input:
N=3
a[]={1,3,5}
Output:
5,3,1
Explanation: [1,3,5],[1,5,3],[3,1,5],[3,5,1],
[5,1,3] and [5,3,1] are all possible
values of array b while the last one is
lexicographically largest.
Example 2:
Input:
N=4
a[]={1,3,4,2}
Output:
b[]={3,1,4,2}
Explanation: [1,3,4,2],[1,3,2,4],[3,1,2,4] and
[3,1,4,2] are all possible values of b among
which the last one is lexicographically largest one.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lexicographically Largest() which takes the array arr[], and its size N as input parameters and returns the array b.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= arr[i] <= 10^{5}
Array may contain duplicate elements. | class Solution:
def lexicographicallyLargest(self, a, n):
k = 0
i = 0
while i < n - 1:
if (a[i] + a[i + 1]) % 2 != 0:
b = a[k : i + 1]
b.sort(reverse=True)
a[k : i + 1] = b
k = i + 1
elif (a[i] + a[i + 1]) % 2 == 0 and i == n - 2:
b = a[k : i + 2]
b.sort(reverse=True)
a[k : i + 2] = b
break
i += 1
return a | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR |
You are given an array a and you need to generate an array b. You are allowed to apply only one type of operation on the array a, any number of times.
In one operation you can swap a_{i} with a_{i+1} only if a_{i}+a_{i+1} is even.
Array b thus generated by applying above operation any number of times, should be lexicographically the largest among all arrays that can be generated from array a.
Example 1:
Input:
N=3
a[]={1,3,5}
Output:
5,3,1
Explanation: [1,3,5],[1,5,3],[3,1,5],[3,5,1],
[5,1,3] and [5,3,1] are all possible
values of array b while the last one is
lexicographically largest.
Example 2:
Input:
N=4
a[]={1,3,4,2}
Output:
b[]={3,1,4,2}
Explanation: [1,3,4,2],[1,3,2,4],[3,1,2,4] and
[3,1,4,2] are all possible values of b among
which the last one is lexicographically largest one.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lexicographically Largest() which takes the array arr[], and its size N as input parameters and returns the array b.
Expected Time Complexity: O(NlogN)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
1<= arr[i] <= 10^{5}
Array may contain duplicate elements. | class Solution:
def lexicographicallyLargest(self, a, n):
b = []
i = 0
while i < n:
arr = []
j = i
while j < n and a[j] % 2 == a[i] % 2:
arr.append(a[j])
j += 1
arr.sort(reverse=True)
b.extend(arr)
i = j
return b | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR |
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
Integers in each row are sorted in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.
Example:
Consider the following matrix:
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Given target = 5, return true.
Given target = 20, return false. | class Solution:
def searchMatrix(self, matrix, target):
if matrix is None or len(matrix) == 0 or len(matrix[0]) == 0:
return False
row, col = 0, len(matrix[0]) - 1
while row < len(matrix) and col >= 0:
if matrix[row][col] == target:
return True
elif matrix[row][col] < target:
row += 1
else:
col -= 1
return False | CLASS_DEF FUNC_DEF IF VAR NONE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER |
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
Integers in each row are sorted in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.
Example:
Consider the following matrix:
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Given target = 5, return true.
Given target = 20, return false. | class Solution(object):
def searchMatrix(self, matrix, target):
if not matrix or not matrix[0]:
return False
m = len(matrix)
n = len(matrix[0])
cols = [([0] * m) for _ in range(n)]
for i in range(n):
for j in range(m):
cols[i][j] = matrix[j][i]
for i in range(m):
low, high = matrix[i][0], matrix[i][-1]
if low <= target <= high:
x = self.search(matrix[i], target)
if (
target == matrix[i][x]
or 0 < x <= len(matrix[i])
and target == self.search(cols[x], target)
):
return True
return False
def search(self, arr, target):
low = 0
high = len(arr)
while low < high:
mid = (low + high) // 2
if arr[mid] < target:
low = mid + 1
elif arr[mid] == target:
return mid
else:
high = mid
return low | CLASS_DEF VAR FUNC_DEF IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR RETURN VAR ASSIGN VAR VAR RETURN VAR |
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
Integers in each row are sorted in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.
Example:
Consider the following matrix:
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Given target = 5, return true.
Given target = 20, return false. | class Solution:
def searchMatrix(self, matrix, k):
for i in range(len(matrix)):
for j in range(len(matrix[i])):
if k < matrix[i][j] or k > matrix[i][len(matrix[i]) - 1]:
break
elif matrix[i][j] == k:
return True
return False | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
Integers in each row are sorted in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.
Example:
Consider the following matrix:
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Given target = 5, return true.
Given target = 20, return false. | class Solution:
def searchMatrix(self, matrix, target):
ans = False
if matrix == [[]] or matrix == []:
return ans
ans = self.check(matrix, target, ans)
return ans
def check(self, matrix, target, ans):
if ans == True:
return ans
for i in range(min(len(matrix[0]), len(matrix))):
if target == matrix[i][i]:
ans = True
return ans
elif (
i + 1 != min(len(matrix[0]), len(matrix))
and target > matrix[i][i]
and target < matrix[i + 1][i + 1]
):
temp = []
for x in range(i + 1):
temp.append(matrix[x][i:])
ans = self.check(temp, target, ans)
temp = []
for x in range(len(matrix) - i):
temp.append(matrix[i + x][: i + 1])
ans = self.check(temp, target, ans)
else:
continue
if (
target
> matrix[min(len(matrix[0]), len(matrix)) - 1][
min(len(matrix[0]), len(matrix)) - 1
]
):
if len(matrix[0]) == len(matrix):
return ans
elif len(matrix[0]) > len(matrix):
temp = []
for x in range(len(matrix)):
temp.append(matrix[x][len(matrix) :])
print(temp)
ans = self.check(temp, target, ans)
else:
temp = []
for x in range(len(matrix) - len(matrix[0])):
temp.append(matrix[len(matrix[0]) + x])
ans = self.check(temp, target, ans)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR LIST LIST VAR LIST RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR RETURN VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
Integers in each row are sorted in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.
Example:
Consider the following matrix:
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Given target = 5, return true.
Given target = 20, return false. | class Solution:
def searchMatrix(self, matrix, target):
m = len(matrix)
if m == 0:
return False
n = len(matrix[0])
if n == 0:
return False
row, col = 0, n - 1
while row < m and col >= 0:
if matrix[row][col] == target:
return True
elif matrix[row][col] > target:
col -= 1
else:
row += 1
return False | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER |
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
Integers in each row are sorted in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.
Example:
Consider the following matrix:
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Given target = 5, return true.
Given target = 20, return false. | class Solution:
def searchMatrix(self, matrix, target):
return self.search(matrix, target)
def use_binary_search(self, matrix, target):
if not matrix or not matrix[0]:
return False
m, n = len(matrix), len(matrix[0])
for i in range(m):
if self.binary_search(matrix, target, i, False):
return True
for i in range(n):
if self.binary_search(matrix, target, i, True):
return True
return False
def binary_search(self, matrix, target, start, vertical):
lo = 0
hi = len(matrix) if vertical else len(matrix[0])
while lo < hi:
mid = lo + (hi - lo) // 2
val = matrix[mid][start] if vertical else matrix[start][mid]
if val == target:
return True
elif val < target:
lo = mid + 1
else:
hi = mid
return False
def search(self, matrix, target):
if not matrix or not matrix[0]:
return False
m, n = len(matrix), len(matrix[0])
row, col = m - 1, 0
while row >= 0 and col < n:
val = matrix[row][col]
if val == target:
return True
elif val > target:
row -= 1
else:
col += 1
return False | CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN NUMBER FUNC_DEF IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER |
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
Integers in each row are sorted in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.
Example:
Consider the following matrix:
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Given target = 5, return true.
Given target = 20, return false. | class Solution:
def searchMatrix(self, matrix, target):
height = len(matrix)
if height == 0:
return False
width = len(matrix[0])
def search(x, y):
if x < 0 or y < 0 or x >= height or y >= width:
return False
current = matrix[x][y]
if current == target:
return True
if current == None:
return False
matrix[x][y] = None
if current > target:
return search(x, y - 1) or search(x - 1, y)
if current < target:
return search(x, y + 1) or search(x + 1, y)
return search(0, 0) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR NONE RETURN NUMBER ASSIGN VAR VAR VAR NONE IF VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR NUMBER NUMBER |
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
Integers in each row are sorted in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.
Example:
Consider the following matrix:
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Given target = 5, return true.
Given target = 20, return false. | class Solution:
def searchMatrix(self, matrix, target):
def helper(matrix, target, i_base=0, j_base=0):
if len(matrix) == 0:
return False
if len(matrix) < 4 and len(matrix[0]) < 4:
for i in range(0, len(matrix)):
for j in range(0, len(matrix[0])):
if matrix[i][j] == target:
print("(" + str(i_base + i) + "," + str(j_base + j) + ")")
return True
return False
else:
cmp = matrix[len(matrix) // 2][len(matrix[0]) // 2]
if cmp == target:
print(
"("
+ str(i_base + len(matrix) // 2)
+ ","
+ str(j_base + len(matrix[0]) // 2)
+ ")"
)
return True
quadrant2 = []
quadrant3 = []
for i in range(0, len(matrix) // 2):
quadrant2.append(matrix[i][len(matrix[0]) // 2 + 1 :])
for i in range(len(matrix) // 2 + 1, len(matrix)):
quadrant3.append(matrix[i][0 : len(matrix[0]) // 2])
if target <= cmp:
quadrant1 = []
for i in range(0, len(matrix) // 2 + 1):
quadrant1.append(matrix[i][0 : len(matrix[0]) // 2 + 1])
return (
helper(quadrant1, target, i_base, j_base)
or helper(
quadrant2, target, i_base, j_base + len(matrix[0]) // 2 + 1
)
or helper(
quadrant3, target, i_base + len(matrix) // 2 + 1, j_base
)
)
else:
quadrant4 = []
for i in range(len(matrix) // 2, len(matrix)):
quadrant4.append(matrix[i][len(matrix[0]) // 2 :])
return (
helper(
quadrant4,
target,
i_base + len(matrix) // 2,
j_base + len(matrix[0]) // 2,
)
or helper(
quadrant2, target, i_base, j_base + len(matrix[0]) // 2 + 1
)
or helper(
quadrant3, target, i_base + len(matrix) // 2 + 1, j_base
)
)
out = helper(matrix, target)
if out == False:
print("(-1, -1)")
return out | CLASS_DEF FUNC_DEF FUNC_DEF NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP STRING FUNC_CALL VAR BIN_OP VAR VAR STRING FUNC_CALL VAR BIN_OP VAR VAR STRING RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP STRING FUNC_CALL VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER STRING RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN VAR |
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
Integers in each row are sorted in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.
Example:
Consider the following matrix:
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Given target = 5, return true.
Given target = 20, return false. | class Solution:
def searchMatrix(self, matrix, target):
if not matrix or not matrix[0]:
return False
row, col = 0, len(matrix[0]) - 1
while row < len(matrix) and -1 < col:
if matrix[row][col] == target:
return True
if matrix[row][col] < target:
row += 1
else:
col -= 1
return False | CLASS_DEF FUNC_DEF IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER |
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
Integers in each row are sorted in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.
Example:
Consider the following matrix:
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Given target = 5, return true.
Given target = 20, return false. | class Solution:
def searchMatrix(self, matrix, target):
index_x = 0
index_y = 0
m = len(matrix)
if m == 0:
return False
n = len(matrix[0])
if n == 0:
return False
for num in range(n):
if target == matrix[0][num]:
return True
if target < matrix[0][num]:
index_x = num
break
index_x = n
for num in range(m):
if target == matrix[num][0]:
return True
if target < matrix[num][0]:
index_y = num
break
index_y = m
for x in range(index_x):
for y in range(index_y):
if matrix[y][x] == target:
return True
return False | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR RETURN NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
Integers in each row are sorted in ascending from left to right.
Integers in each column are sorted in ascending from top to bottom.
Example:
Consider the following matrix:
[
[1, 4, 7, 11, 15],
[2, 5, 8, 12, 19],
[3, 6, 9, 16, 22],
[10, 13, 14, 17, 24],
[18, 21, 23, 26, 30]
]
Given target = 5, return true.
Given target = 20, return false. | class Solution:
def searchMatrix(self, matrix, target):
if not matrix:
return False
row = len(matrix)
col = len(matrix[0])
for i in range(row):
for j in range(col):
if matrix[i][j] == target:
return True
return False | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given an unsorted array arr[] of n positive integers. Find the number of triangles that can be formed with three different array elements as lengths of three sides of triangles.
Example 1:
Input:
n = 3
arr[] = {3, 5, 4}
Output:
1
Explanation:
A triangle is possible
with all the elements 5, 3 and 4.
Example 2:
Input:
n = 5
arr[] = {6, 4, 9, 7, 8}
Output:
10
Explanation:
There are 10 triangles
possible with the given elements like
(6,4,9), (6,7,8),...
Your Task:
This is a function problem. You only need to complete the function findNumberOfTriangles() that takes arr[] and n as input parameters and returns the count of total possible triangles.
Expected Time Complexity: O(n^{2}).
Expected Space Complexity: O(1).
Constraints:
3 <= n <= 10^{3}
1 <= arr[i] <= 10^{3} | class Solution:
def findNumberOfTriangles(self, arr, n):
arr.sort()
count = 0
for max in range(n - 1, 1, -1):
a = 0
b = max - 1
while b > a:
if arr[a] + arr[b] > arr[max]:
count += b - a
b -= 1
else:
a += 1
return count | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
Given an unsorted array arr[] of n positive integers. Find the number of triangles that can be formed with three different array elements as lengths of three sides of triangles.
Example 1:
Input:
n = 3
arr[] = {3, 5, 4}
Output:
1
Explanation:
A triangle is possible
with all the elements 5, 3 and 4.
Example 2:
Input:
n = 5
arr[] = {6, 4, 9, 7, 8}
Output:
10
Explanation:
There are 10 triangles
possible with the given elements like
(6,4,9), (6,7,8),...
Your Task:
This is a function problem. You only need to complete the function findNumberOfTriangles() that takes arr[] and n as input parameters and returns the count of total possible triangles.
Expected Time Complexity: O(n^{2}).
Expected Space Complexity: O(1).
Constraints:
3 <= n <= 10^{3}
1 <= arr[i] <= 10^{3} | class Solution:
def findNumberOfTriangles(self, arr, n):
arr.sort()
count = 0
for i in range(n - 2):
j, p = i + 1, i + 2
while j < n - 1:
if arr[i] + arr[j] <= arr[p]:
count += p - j - 1
j += 1
else:
p += 1
if p >= n:
count += sum([(p - k - 1) for k in range(j, n - 1)])
break
return count | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR |
Given an unsorted array arr[] of n positive integers. Find the number of triangles that can be formed with three different array elements as lengths of three sides of triangles.
Example 1:
Input:
n = 3
arr[] = {3, 5, 4}
Output:
1
Explanation:
A triangle is possible
with all the elements 5, 3 and 4.
Example 2:
Input:
n = 5
arr[] = {6, 4, 9, 7, 8}
Output:
10
Explanation:
There are 10 triangles
possible with the given elements like
(6,4,9), (6,7,8),...
Your Task:
This is a function problem. You only need to complete the function findNumberOfTriangles() that takes arr[] and n as input parameters and returns the count of total possible triangles.
Expected Time Complexity: O(n^{2}).
Expected Space Complexity: O(1).
Constraints:
3 <= n <= 10^{3}
1 <= arr[i] <= 10^{3} | class Solution:
def findNumberOfTriangles(self, arr, n):
arr.sort()
count = 0
for i in range(n - 1, 0, -1):
l, r = 0, i - 1
while l < r:
if arr[l] + arr[r] > arr[i]:
count = count + (r - l)
r = r - 1
else:
l = l + 1
return count | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given an unsorted array arr[] of n positive integers. Find the number of triangles that can be formed with three different array elements as lengths of three sides of triangles.
Example 1:
Input:
n = 3
arr[] = {3, 5, 4}
Output:
1
Explanation:
A triangle is possible
with all the elements 5, 3 and 4.
Example 2:
Input:
n = 5
arr[] = {6, 4, 9, 7, 8}
Output:
10
Explanation:
There are 10 triangles
possible with the given elements like
(6,4,9), (6,7,8),...
Your Task:
This is a function problem. You only need to complete the function findNumberOfTriangles() that takes arr[] and n as input parameters and returns the count of total possible triangles.
Expected Time Complexity: O(n^{2}).
Expected Space Complexity: O(1).
Constraints:
3 <= n <= 10^{3}
1 <= arr[i] <= 10^{3} | class Solution:
def findNumberOfTriangles(self, arr, n):
def find2Numbers(a, y):
k = 0
l = len(a) - 1
c = 0
while k < l:
if y < a[k] + a[l]:
c += l - k
k += 1
elif y >= a[k] + a[l]:
l -= 1
return c
arr.sort(reverse=True)
total = 0
for i in range(len(arr)):
x = arr[i]
total += find2Numbers(arr[i + 1 : len(arr)], x)
return total | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR RETURN VAR |
Given an unsorted array arr[] of n positive integers. Find the number of triangles that can be formed with three different array elements as lengths of three sides of triangles.
Example 1:
Input:
n = 3
arr[] = {3, 5, 4}
Output:
1
Explanation:
A triangle is possible
with all the elements 5, 3 and 4.
Example 2:
Input:
n = 5
arr[] = {6, 4, 9, 7, 8}
Output:
10
Explanation:
There are 10 triangles
possible with the given elements like
(6,4,9), (6,7,8),...
Your Task:
This is a function problem. You only need to complete the function findNumberOfTriangles() that takes arr[] and n as input parameters and returns the count of total possible triangles.
Expected Time Complexity: O(n^{2}).
Expected Space Complexity: O(1).
Constraints:
3 <= n <= 10^{3}
1 <= arr[i] <= 10^{3} | class Solution:
def findNumberOfTriangles(self, arr, n):
arr.sort()
ans = 0
i = 2
while i < len(arr):
left = 0
right = i - 1
while left < right:
if arr[left] + arr[right] > arr[i]:
ans += right - left
right -= 1
else:
left += 1
i += 1
return ans | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
Given an unsorted array arr[] of n positive integers. Find the number of triangles that can be formed with three different array elements as lengths of three sides of triangles.
Example 1:
Input:
n = 3
arr[] = {3, 5, 4}
Output:
1
Explanation:
A triangle is possible
with all the elements 5, 3 and 4.
Example 2:
Input:
n = 5
arr[] = {6, 4, 9, 7, 8}
Output:
10
Explanation:
There are 10 triangles
possible with the given elements like
(6,4,9), (6,7,8),...
Your Task:
This is a function problem. You only need to complete the function findNumberOfTriangles() that takes arr[] and n as input parameters and returns the count of total possible triangles.
Expected Time Complexity: O(n^{2}).
Expected Space Complexity: O(1).
Constraints:
3 <= n <= 10^{3}
1 <= arr[i] <= 10^{3} | class Solution:
def nC2(self, n):
return n * (n - 1) / 2
def findNumberOfTriangles(self, arr, n):
arr.sort()
j = n - 1
count = 0
while j >= 2:
i = 0
k = j - 1
c = arr[j]
while i < k:
a = arr[i]
b = arr[k]
if a + b > c:
count = count + (k - i)
k = k - 1
else:
i = i + 1
j = j - 1
return count | CLASS_DEF FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given an unsorted array arr[] of n positive integers. Find the number of triangles that can be formed with three different array elements as lengths of three sides of triangles.
Example 1:
Input:
n = 3
arr[] = {3, 5, 4}
Output:
1
Explanation:
A triangle is possible
with all the elements 5, 3 and 4.
Example 2:
Input:
n = 5
arr[] = {6, 4, 9, 7, 8}
Output:
10
Explanation:
There are 10 triangles
possible with the given elements like
(6,4,9), (6,7,8),...
Your Task:
This is a function problem. You only need to complete the function findNumberOfTriangles() that takes arr[] and n as input parameters and returns the count of total possible triangles.
Expected Time Complexity: O(n^{2}).
Expected Space Complexity: O(1).
Constraints:
3 <= n <= 10^{3}
1 <= arr[i] <= 10^{3} | class Solution:
def findNumberOfTriangles(self, arr, n):
arr.sort()
i = len(arr) - 1
answer = 0
while i >= 0:
l = 0
r = i - 1
while l < r:
if arr[l] + arr[r] > arr[i]:
answer = answer + r - l
r -= 1
else:
l += 1
i -= 1
return answer | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR |
Given an unsorted array arr[] of n positive integers. Find the number of triangles that can be formed with three different array elements as lengths of three sides of triangles.
Example 1:
Input:
n = 3
arr[] = {3, 5, 4}
Output:
1
Explanation:
A triangle is possible
with all the elements 5, 3 and 4.
Example 2:
Input:
n = 5
arr[] = {6, 4, 9, 7, 8}
Output:
10
Explanation:
There are 10 triangles
possible with the given elements like
(6,4,9), (6,7,8),...
Your Task:
This is a function problem. You only need to complete the function findNumberOfTriangles() that takes arr[] and n as input parameters and returns the count of total possible triangles.
Expected Time Complexity: O(n^{2}).
Expected Space Complexity: O(1).
Constraints:
3 <= n <= 10^{3}
1 <= arr[i] <= 10^{3} | class Solution:
def findNumberOfTriangles(self, arr, n):
ans = 0
arr.sort()
for i in range(0, n - 2):
third = i + 2
for j in range(i + 1, n):
while third < n and arr[i] + arr[j] > arr[third]:
third += 1
ans += third - j - 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR |
Given an unsorted array arr[] of n positive integers. Find the number of triangles that can be formed with three different array elements as lengths of three sides of triangles.
Example 1:
Input:
n = 3
arr[] = {3, 5, 4}
Output:
1
Explanation:
A triangle is possible
with all the elements 5, 3 and 4.
Example 2:
Input:
n = 5
arr[] = {6, 4, 9, 7, 8}
Output:
10
Explanation:
There are 10 triangles
possible with the given elements like
(6,4,9), (6,7,8),...
Your Task:
This is a function problem. You only need to complete the function findNumberOfTriangles() that takes arr[] and n as input parameters and returns the count of total possible triangles.
Expected Time Complexity: O(n^{2}).
Expected Space Complexity: O(1).
Constraints:
3 <= n <= 10^{3}
1 <= arr[i] <= 10^{3} | class Solution:
def findNumberOfTriangles(self, arr, n):
ct = 0
arr.sort()
for i in range(n):
j, k = 0, i - 1
while j < k:
if arr[j] + arr[k] > arr[i]:
ct += k - j
k -= 1
else:
j += 1
return ct | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
Given an unsorted array arr[] of n positive integers. Find the number of triangles that can be formed with three different array elements as lengths of three sides of triangles.
Example 1:
Input:
n = 3
arr[] = {3, 5, 4}
Output:
1
Explanation:
A triangle is possible
with all the elements 5, 3 and 4.
Example 2:
Input:
n = 5
arr[] = {6, 4, 9, 7, 8}
Output:
10
Explanation:
There are 10 triangles
possible with the given elements like
(6,4,9), (6,7,8),...
Your Task:
This is a function problem. You only need to complete the function findNumberOfTriangles() that takes arr[] and n as input parameters and returns the count of total possible triangles.
Expected Time Complexity: O(n^{2}).
Expected Space Complexity: O(1).
Constraints:
3 <= n <= 10^{3}
1 <= arr[i] <= 10^{3} | class Solution:
def findNumberOfTriangles(self, arr, n):
arr = sorted(arr, reverse=True)
count = 0
for i in range(n - 2):
l, r = i + 1, n - 1
while l < r:
if arr[l] + arr[r] <= arr[i]:
r -= 1
else:
count += r - l
l += 1
return count | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR |
Given an unsorted array arr[] of n positive integers. Find the number of triangles that can be formed with three different array elements as lengths of three sides of triangles.
Example 1:
Input:
n = 3
arr[] = {3, 5, 4}
Output:
1
Explanation:
A triangle is possible
with all the elements 5, 3 and 4.
Example 2:
Input:
n = 5
arr[] = {6, 4, 9, 7, 8}
Output:
10
Explanation:
There are 10 triangles
possible with the given elements like
(6,4,9), (6,7,8),...
Your Task:
This is a function problem. You only need to complete the function findNumberOfTriangles() that takes arr[] and n as input parameters and returns the count of total possible triangles.
Expected Time Complexity: O(n^{2}).
Expected Space Complexity: O(1).
Constraints:
3 <= n <= 10^{3}
1 <= arr[i] <= 10^{3} | class Solution:
def findNumberOfTriangles(self, a, n):
ans = 0
a.sort()
for i in range(n - 2, 0, -1):
j = i + 1
k = 0
while k < i:
if j == n:
ans += j - i - 1
k += 1
elif a[k] + a[i] > a[j]:
j += 1
else:
ans += j - i - 1
k += 1
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR |
Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, the i-th manager either sorts first r_{i} numbers in non-descending or non-ascending order and then passes the report to the manager i + 1, or directly to Blake (if this manager has number i = m).
Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence a_{i} of length n and the description of each manager, that is value r_{i} and his favourite order. You are asked to speed up the process and determine how the final report will look like.
-----Input-----
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of commodities in the report and the number of managers, respectively.
The second line contains n integers a_{i} (|a_{i}| ≤ 10^9) — the initial report before it gets to the first manager.
Then follow m lines with the descriptions of the operations managers are going to perform. The i-th of these lines contains two integers t_{i} and r_{i} ($t_{i} \in \{1,2 \}$, 1 ≤ r_{i} ≤ n), meaning that the i-th manager sorts the first r_{i} numbers either in the non-descending (if t_{i} = 1) or non-ascending (if t_{i} = 2) order.
-----Output-----
Print n integers — the final report, which will be passed to Blake by manager number m.
-----Examples-----
Input
3 1
1 2 3
2 2
Output
2 1 3
Input
4 2
1 2 4 3
2 3
1 2
Output
2 4 1 3
-----Note-----
In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form.
In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake. | n, m = map(int, input().split())
lis = list(map(int, input().split()))
q = []
for i in range(m):
a, b = map(int, input().split())
while len(q) > 0 and b >= q[-1][1]:
q.pop()
q.append([a, b])
a, b = 0, q[0][1] - 1
q.append([0, 0])
aa = sorted(lis[: b + 1])
for i in range(1, len(q)):
for j in range(q[i - 1][1], q[i][1], -1):
if q[i - 1][0] == 1:
lis[j - 1] = aa[b]
b -= 1
else:
lis[j - 1] = aa[a]
a += 1
print(*lis) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, the i-th manager either sorts first r_{i} numbers in non-descending or non-ascending order and then passes the report to the manager i + 1, or directly to Blake (if this manager has number i = m).
Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence a_{i} of length n and the description of each manager, that is value r_{i} and his favourite order. You are asked to speed up the process and determine how the final report will look like.
-----Input-----
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of commodities in the report and the number of managers, respectively.
The second line contains n integers a_{i} (|a_{i}| ≤ 10^9) — the initial report before it gets to the first manager.
Then follow m lines with the descriptions of the operations managers are going to perform. The i-th of these lines contains two integers t_{i} and r_{i} ($t_{i} \in \{1,2 \}$, 1 ≤ r_{i} ≤ n), meaning that the i-th manager sorts the first r_{i} numbers either in the non-descending (if t_{i} = 1) or non-ascending (if t_{i} = 2) order.
-----Output-----
Print n integers — the final report, which will be passed to Blake by manager number m.
-----Examples-----
Input
3 1
1 2 3
2 2
Output
2 1 3
Input
4 2
1 2 4 3
2 3
1 2
Output
2 4 1 3
-----Note-----
In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form.
In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake. | n, m = [int(i) for i in input().split()]
adata = [int(i) for i in input().split()]
t = [None] * m
r = [None] * m
for i in range(m):
t[i], r[i] = [int(j) for j in input().split()]
Sortlist = []
Sortcount = 0
M = 0
for i in range(m - 1, -1, -1):
if r[i] > M:
Sortlist.append([r[i], t[i]])
Sortcount += 1
M = r[i]
S = sorted(adata[0 : Sortlist[-1][0]])
L = 0
R = M - 1
for i in range(M - 1, -1, -1):
if Sortcount > 0:
if i < Sortlist[Sortcount - 1][0]:
Reverse = Sortlist[Sortcount - 1][1]
Sortcount -= 1
if Reverse == 1:
adata[i] = S[R]
R -= 1
else:
adata[i] = S[L]
L += 1
for i in adata[0:-1]:
print(str(i) + " ", end="")
print(adata[-1]) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING STRING EXPR FUNC_CALL VAR VAR NUMBER |
Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, the i-th manager either sorts first r_{i} numbers in non-descending or non-ascending order and then passes the report to the manager i + 1, or directly to Blake (if this manager has number i = m).
Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence a_{i} of length n and the description of each manager, that is value r_{i} and his favourite order. You are asked to speed up the process and determine how the final report will look like.
-----Input-----
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of commodities in the report and the number of managers, respectively.
The second line contains n integers a_{i} (|a_{i}| ≤ 10^9) — the initial report before it gets to the first manager.
Then follow m lines with the descriptions of the operations managers are going to perform. The i-th of these lines contains two integers t_{i} and r_{i} ($t_{i} \in \{1,2 \}$, 1 ≤ r_{i} ≤ n), meaning that the i-th manager sorts the first r_{i} numbers either in the non-descending (if t_{i} = 1) or non-ascending (if t_{i} = 2) order.
-----Output-----
Print n integers — the final report, which will be passed to Blake by manager number m.
-----Examples-----
Input
3 1
1 2 3
2 2
Output
2 1 3
Input
4 2
1 2 4 3
2 3
1 2
Output
2 4 1 3
-----Note-----
In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form.
In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake. | [n, m] = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
t = m * [0]
r = m * [0]
for k in range(0, m):
[t[k], r[k]] = [int(i) for i in input().split()]
tstack = []
rstack = []
mm = -1
lastt = -1
tstack.append(t[0])
rstack.append(r[0])
top = 0
for i in range(1, m):
while top > -1:
if r[i] >= rstack[top]:
rstack.pop()
tstack.pop()
top = top - 1
else:
break
if top == -1:
rstack.append(r[i])
tstack.append(t[i])
top = top + 1
elif tstack[top] != t[i]:
rstack.append(r[i])
tstack.append(t[i])
top = top + 1
r0 = rstack[0]
sorteda = sorted(a[:r0])
begin = 0
end = len(sorteda) - 1
for mindex in range(0, len(tstack) - 1):
rthis = rstack[mindex]
rnext = rstack[mindex + 1]
if tstack[mindex] == 1:
for i in range(rthis - 1, rnext - 1, -1):
a[i] = sorteda[end]
end = end - 1
else:
for i in range(rthis - 1, rnext - 1, -1):
a[i] = sorteda[begin]
begin = begin + 1
if tstack[-1] == 1:
for i in range(0, rstack[-1]):
a[i] = sorteda[i + begin]
else:
for i in range(0, rstack[-1]):
a[i] = sorteda[end - i]
print(*a, sep=" ") | ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR LIST NUMBER ASSIGN VAR BIN_OP VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN LIST VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR STRING |
Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, the i-th manager either sorts first r_{i} numbers in non-descending or non-ascending order and then passes the report to the manager i + 1, or directly to Blake (if this manager has number i = m).
Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence a_{i} of length n and the description of each manager, that is value r_{i} and his favourite order. You are asked to speed up the process and determine how the final report will look like.
-----Input-----
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of commodities in the report and the number of managers, respectively.
The second line contains n integers a_{i} (|a_{i}| ≤ 10^9) — the initial report before it gets to the first manager.
Then follow m lines with the descriptions of the operations managers are going to perform. The i-th of these lines contains two integers t_{i} and r_{i} ($t_{i} \in \{1,2 \}$, 1 ≤ r_{i} ≤ n), meaning that the i-th manager sorts the first r_{i} numbers either in the non-descending (if t_{i} = 1) or non-ascending (if t_{i} = 2) order.
-----Output-----
Print n integers — the final report, which will be passed to Blake by manager number m.
-----Examples-----
Input
3 1
1 2 3
2 2
Output
2 1 3
Input
4 2
1 2 4 3
2 3
1 2
Output
2 4 1 3
-----Note-----
In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form.
In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake. | def main():
n, m = input().strip().split()
n, m = int(n), int(m)
initial = list(map(int, input().strip().split()))
max_r = 0
reports = [None] * n
for i in range(m):
order, last_rep = input().strip().split()
last_rep = int(last_rep)
max_r = max(max_r, last_rep)
reports[last_rep - 1] = i, order
prev = None
for i in reversed(range(n)):
if reports[i] is None and prev is None:
continue
elif reports[i] is not None and prev is None:
prev = reports[i]
continue
if reports[i] is None:
reports[i] = prev
continue
if reports[i][0] < prev[0]:
reports[i] = prev
else:
prev = reports[i]
reports = list(map(lambda pair: pair[1] if pair is not None else None, reports))
result = []
for i in reversed(range(n)):
if i == max_r - 1:
break
result.append(initial[i])
begin = 0
end = max_r - 1
arr = initial[begin:max_r]
arr.sort()
for i in reversed(range(max_r)):
if reports[i] == "2":
target = begin
begin += 1
elif reports[i] == "1":
target = end
end -= 1
result.append(arr[target])
result.reverse()
print(*result)
main() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NONE VAR NONE IF VAR VAR NONE VAR NONE ASSIGN VAR VAR VAR IF VAR VAR NONE ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NONE VAR NUMBER NONE VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, the i-th manager either sorts first r_{i} numbers in non-descending or non-ascending order and then passes the report to the manager i + 1, or directly to Blake (if this manager has number i = m).
Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence a_{i} of length n and the description of each manager, that is value r_{i} and his favourite order. You are asked to speed up the process and determine how the final report will look like.
-----Input-----
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of commodities in the report and the number of managers, respectively.
The second line contains n integers a_{i} (|a_{i}| ≤ 10^9) — the initial report before it gets to the first manager.
Then follow m lines with the descriptions of the operations managers are going to perform. The i-th of these lines contains two integers t_{i} and r_{i} ($t_{i} \in \{1,2 \}$, 1 ≤ r_{i} ≤ n), meaning that the i-th manager sorts the first r_{i} numbers either in the non-descending (if t_{i} = 1) or non-ascending (if t_{i} = 2) order.
-----Output-----
Print n integers — the final report, which will be passed to Blake by manager number m.
-----Examples-----
Input
3 1
1 2 3
2 2
Output
2 1 3
Input
4 2
1 2 4 3
2 3
1 2
Output
2 4 1 3
-----Note-----
In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form.
In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake. | n, m = map(int, input().split())
a = list(map(int, input().split()))
t = []
for i in range(m):
asc, r = map(int, input().split())
while t:
if r >= t[-1][1]:
t.pop()
else:
break
t.append([asc, r])
t.append([0, 0])
x = 0
y = t[0][1] - 1
sor = sorted(a[: t[0][1]])
for i in range(1, len(t)):
for j in range(t[i - 1][1], t[i][1], -1):
if t[i - 1][0] == 1:
a[j - 1] = sor[y]
y -= 1
else:
a[j - 1] = sor[x]
x += 1
print(" ".join(list(str(i) for i in a))) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR WHILE VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, the i-th manager either sorts first r_{i} numbers in non-descending or non-ascending order and then passes the report to the manager i + 1, or directly to Blake (if this manager has number i = m).
Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence a_{i} of length n and the description of each manager, that is value r_{i} and his favourite order. You are asked to speed up the process and determine how the final report will look like.
-----Input-----
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of commodities in the report and the number of managers, respectively.
The second line contains n integers a_{i} (|a_{i}| ≤ 10^9) — the initial report before it gets to the first manager.
Then follow m lines with the descriptions of the operations managers are going to perform. The i-th of these lines contains two integers t_{i} and r_{i} ($t_{i} \in \{1,2 \}$, 1 ≤ r_{i} ≤ n), meaning that the i-th manager sorts the first r_{i} numbers either in the non-descending (if t_{i} = 1) or non-ascending (if t_{i} = 2) order.
-----Output-----
Print n integers — the final report, which will be passed to Blake by manager number m.
-----Examples-----
Input
3 1
1 2 3
2 2
Output
2 1 3
Input
4 2
1 2 4 3
2 3
1 2
Output
2 4 1 3
-----Note-----
In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form.
In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake. | from sys import setrecursionlimit
setrecursionlimit(200000)
class data:
def __init__(self, a):
self.data = a
def qsort(self, l, r, reverse):
if l >= r:
return None
ind = l
i = l
j = r
key = self.data[l]
while i < j:
if reverse == 1:
while (self.data[j] >= key) & (j >= l):
j -= 1
while (self.data[i] < key) & (i <= r):
i += 1
ind = i - 1
if i < j:
ind += 1
t = self.data[i]
self.data[i] = self.data[j]
self.data[j] = t
else:
while (self.data[j] <= key) & (j >= l):
j -= 1
ind = j + 1
while (self.data[i] > key) & (i <= r):
i += 1
if i < j:
ind -= 1
t = self.data[i]
self.data[i] = self.data[j]
self.data[j] = t
self.qsort(l, ind, reverse)
self.qsort(ind + 1, r, reverse)
n, m = [int(i) for i in input().split()]
a = data([int(i) for i in input().split()])
t = [None] * m
r = [None] * m
for i in range(m):
t[i], r[i] = [int(j) for j in input().split()]
Sortlist = []
Sortcount = 0
M = 0
for i in range(m - 1, -1, -1):
if r[i] > M:
Sortlist.append([r[i], t[i]])
Sortcount += 1
M = r[i]
S = sorted(a.data[0 : Sortlist[-1][0]])
L = 0
R = M - 1
for i in range(M - 1, -1, -1):
if Sortcount > 0:
if i < Sortlist[Sortcount - 1][0]:
Reverse = Sortlist[Sortcount - 1][1]
Sortcount -= 1
if Reverse == 1:
a.data[i] = S[R]
R -= 1
else:
a.data[i] = S[L]
L += 1
for i in a.data[0:-1]:
print(str(i) + " ", end="")
print(a.data[-1]) | EXPR FUNC_CALL VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR IF VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING STRING EXPR FUNC_CALL VAR VAR NUMBER |
Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, the i-th manager either sorts first r_{i} numbers in non-descending or non-ascending order and then passes the report to the manager i + 1, or directly to Blake (if this manager has number i = m).
Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence a_{i} of length n and the description of each manager, that is value r_{i} and his favourite order. You are asked to speed up the process and determine how the final report will look like.
-----Input-----
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of commodities in the report and the number of managers, respectively.
The second line contains n integers a_{i} (|a_{i}| ≤ 10^9) — the initial report before it gets to the first manager.
Then follow m lines with the descriptions of the operations managers are going to perform. The i-th of these lines contains two integers t_{i} and r_{i} ($t_{i} \in \{1,2 \}$, 1 ≤ r_{i} ≤ n), meaning that the i-th manager sorts the first r_{i} numbers either in the non-descending (if t_{i} = 1) or non-ascending (if t_{i} = 2) order.
-----Output-----
Print n integers — the final report, which will be passed to Blake by manager number m.
-----Examples-----
Input
3 1
1 2 3
2 2
Output
2 1 3
Input
4 2
1 2 4 3
2 3
1 2
Output
2 4 1 3
-----Note-----
In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form.
In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake. | a, b = [int(i) for i in input().split()]
q = [int(i) for i in input().split()]
s = []
for p in range(b):
s += [[int(i) for i in input().split()]]
S = []
k = 0
for p in range(b - 1, -1, -1):
if s[p][1] > k:
S += [s[p]]
k = s[p][1]
Q1 = sorted(q[: S[-1][1]])
x, y = 0, S[-1][1] - 1
S = [[0, 0]] + S
l = 1
for j in range(S[-1][1] - 1, -1, -1):
if x > y:
break
if j < S[-l][1]:
k = S[-l][0]
l += 1
if k == 1:
q[j] = Q1[y]
y -= 1
if k == 2:
q[j] = Q1[x]
x += 1
print(" ".join(list(map(str, q)))) | 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 LIST FOR VAR FUNC_CALL VAR VAR VAR LIST FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR LIST VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST LIST NUMBER NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, the i-th manager either sorts first r_{i} numbers in non-descending or non-ascending order and then passes the report to the manager i + 1, or directly to Blake (if this manager has number i = m).
Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence a_{i} of length n and the description of each manager, that is value r_{i} and his favourite order. You are asked to speed up the process and determine how the final report will look like.
-----Input-----
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of commodities in the report and the number of managers, respectively.
The second line contains n integers a_{i} (|a_{i}| ≤ 10^9) — the initial report before it gets to the first manager.
Then follow m lines with the descriptions of the operations managers are going to perform. The i-th of these lines contains two integers t_{i} and r_{i} ($t_{i} \in \{1,2 \}$, 1 ≤ r_{i} ≤ n), meaning that the i-th manager sorts the first r_{i} numbers either in the non-descending (if t_{i} = 1) or non-ascending (if t_{i} = 2) order.
-----Output-----
Print n integers — the final report, which will be passed to Blake by manager number m.
-----Examples-----
Input
3 1
1 2 3
2 2
Output
2 1 3
Input
4 2
1 2 4 3
2 3
1 2
Output
2 4 1 3
-----Note-----
In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form.
In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake. | n, m = [int(i) for i in input().split()]
_report = [int(i) for i in input().split()]
working = []
for i in range(m):
x, y = map(int, input().split())
while len(working) != 0 and y >= working[-1][1]:
working.pop()
working.append([x, y])
P = working[0][1]
sample = _report[:P]
sample.sort()
x = 0
y = P - 1
working.append([0, 0])
for i in range(len(working) - 1):
for j in range(working[i][1], working[i + 1][1], -1):
if working[i][0] == 1:
_report[j - 1] = sample[y]
y -= 1
if working[i][0] == 2:
_report[j - 1] = sample[x]
x += 1
print(" ".join(map(str, _report))) | 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 LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, the i-th manager either sorts first r_{i} numbers in non-descending or non-ascending order and then passes the report to the manager i + 1, or directly to Blake (if this manager has number i = m).
Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence a_{i} of length n and the description of each manager, that is value r_{i} and his favourite order. You are asked to speed up the process and determine how the final report will look like.
-----Input-----
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of commodities in the report and the number of managers, respectively.
The second line contains n integers a_{i} (|a_{i}| ≤ 10^9) — the initial report before it gets to the first manager.
Then follow m lines with the descriptions of the operations managers are going to perform. The i-th of these lines contains two integers t_{i} and r_{i} ($t_{i} \in \{1,2 \}$, 1 ≤ r_{i} ≤ n), meaning that the i-th manager sorts the first r_{i} numbers either in the non-descending (if t_{i} = 1) or non-ascending (if t_{i} = 2) order.
-----Output-----
Print n integers — the final report, which will be passed to Blake by manager number m.
-----Examples-----
Input
3 1
1 2 3
2 2
Output
2 1 3
Input
4 2
1 2 4 3
2 3
1 2
Output
2 4 1 3
-----Note-----
In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form.
In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake. | def compress(ops):
cops = []
for r, dir in ops:
while cops and cops[-1][0] <= r:
cops.pop()
if not cops or cops[-1][1] != dir:
cops.append((r, dir))
return cops
def transform(lst, ops):
mr, mdir = ops[0]
sections = [list(range(mr, len(lst)))]
ost = 0
oen = mr
pr, pdir = ops[0]
for r, dir in ops[1:]:
k = pr - r
if pdir:
sections.append(reversed(list(range(ost, ost + k))))
ost += k
else:
sections.append(list(range(oen - k, oen)))
oen -= k
pr, pdir = r, dir
if pdir:
sections.append(reversed(list(range(ost, oen))))
else:
sections.append(list(range(ost, oen)))
olst = lst[:mr]
olst.sort()
olst.extend(lst[mr:])
return [olst[i] for sec in reversed(sections) for i in sec]
def parse_op():
d, r = input().split()
return int(r), d == "2"
def parse_input():
n, m = list(map(int, input().split()))
lst = list(map(int, input().split()))
assert len(lst) == n
ops = [parse_op() for _ in range(m)]
return lst, ops
def __starting_point():
lst, ops = parse_input()
cops = compress(ops)
tlst = transform(lst, cops)
print(" ".join(map(str, tlst)))
__starting_point() | FUNC_DEF ASSIGN VAR LIST FOR VAR VAR VAR WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR IF VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR STRING FUNC_DEF 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 FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR |
Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, the i-th manager either sorts first r_{i} numbers in non-descending or non-ascending order and then passes the report to the manager i + 1, or directly to Blake (if this manager has number i = m).
Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence a_{i} of length n and the description of each manager, that is value r_{i} and his favourite order. You are asked to speed up the process and determine how the final report will look like.
-----Input-----
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of commodities in the report and the number of managers, respectively.
The second line contains n integers a_{i} (|a_{i}| ≤ 10^9) — the initial report before it gets to the first manager.
Then follow m lines with the descriptions of the operations managers are going to perform. The i-th of these lines contains two integers t_{i} and r_{i} ($t_{i} \in \{1,2 \}$, 1 ≤ r_{i} ≤ n), meaning that the i-th manager sorts the first r_{i} numbers either in the non-descending (if t_{i} = 1) or non-ascending (if t_{i} = 2) order.
-----Output-----
Print n integers — the final report, which will be passed to Blake by manager number m.
-----Examples-----
Input
3 1
1 2 3
2 2
Output
2 1 3
Input
4 2
1 2 4 3
2 3
1 2
Output
2 4 1 3
-----Note-----
In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form.
In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake. | n, m = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
manager = []
for i in range(m):
t = [int(j) for j in input().split()]
manager.append(t)
j = len(manager) - 1
while j > 0 and len(manager) > 1:
if manager[j][1] > manager[j - 1][1]:
del manager[j - 1]
j -= 1
else:
break
temp = a[manager[0][1] :]
temp.reverse()
result = temp
del a[manager[0][1] :]
a.sort()
lm = len(manager)
l, r = 0, 0
for i in range(lm):
if i < lm - 1:
if manager[i][0] == 1:
for j in range(manager[i][1] - manager[i + 1][1]):
result.append(a[-(1 + r)])
r += 1
elif manager[i][0] == 2:
for j in range(manager[i][1] - manager[i + 1][1]):
result.append(a[l])
l += 1
else:
if r != 0:
tem = a[l:-r]
else:
tem = a[l:]
if manager[i][0] == 1:
tem.reverse()
result.extend(tem)
elif manager[i][0] == 2:
result.extend(tem)
result.reverse()
print(*result) | 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 LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, the i-th manager either sorts first r_{i} numbers in non-descending or non-ascending order and then passes the report to the manager i + 1, or directly to Blake (if this manager has number i = m).
Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence a_{i} of length n and the description of each manager, that is value r_{i} and his favourite order. You are asked to speed up the process and determine how the final report will look like.
-----Input-----
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of commodities in the report and the number of managers, respectively.
The second line contains n integers a_{i} (|a_{i}| ≤ 10^9) — the initial report before it gets to the first manager.
Then follow m lines with the descriptions of the operations managers are going to perform. The i-th of these lines contains two integers t_{i} and r_{i} ($t_{i} \in \{1,2 \}$, 1 ≤ r_{i} ≤ n), meaning that the i-th manager sorts the first r_{i} numbers either in the non-descending (if t_{i} = 1) or non-ascending (if t_{i} = 2) order.
-----Output-----
Print n integers — the final report, which will be passed to Blake by manager number m.
-----Examples-----
Input
3 1
1 2 3
2 2
Output
2 1 3
Input
4 2
1 2 4 3
2 3
1 2
Output
2 4 1 3
-----Note-----
In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form.
In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake. | str1 = input().split()
n = int(str1[0])
m = int(str1[1])
a = list(map(int, input().split()))
oper = []
for i in range(m):
tmp = list(map(int, input().split()))
while len(oper) > 0 and tmp[1] > oper[-1][1]:
oper.pop()
oper.append([tmp[0], tmp[1]])
b = []
for i in range(oper[0][1], n):
b.append(a.pop())
a = sorted(a)
order = oper[0][0]
front = 0
back = len(a) - 1
for i in range(1, len(oper)):
for j in range(oper[i][1], oper[i - 1][1]):
if order == 1:
b.append(a[back])
back -= 1
else:
b.append(a[front])
front += 1
order = oper[i][0]
for i in range(back - front + 1):
if order == 1:
b.append(a[back])
back -= 1
else:
b.append(a[front])
front += 1
for i in range(n - 1, -1, -1):
print(b[i], end=" ") | ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR STRING |
Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, the i-th manager either sorts first r_{i} numbers in non-descending or non-ascending order and then passes the report to the manager i + 1, or directly to Blake (if this manager has number i = m).
Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence a_{i} of length n and the description of each manager, that is value r_{i} and his favourite order. You are asked to speed up the process and determine how the final report will look like.
-----Input-----
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of commodities in the report and the number of managers, respectively.
The second line contains n integers a_{i} (|a_{i}| ≤ 10^9) — the initial report before it gets to the first manager.
Then follow m lines with the descriptions of the operations managers are going to perform. The i-th of these lines contains two integers t_{i} and r_{i} ($t_{i} \in \{1,2 \}$, 1 ≤ r_{i} ≤ n), meaning that the i-th manager sorts the first r_{i} numbers either in the non-descending (if t_{i} = 1) or non-ascending (if t_{i} = 2) order.
-----Output-----
Print n integers — the final report, which will be passed to Blake by manager number m.
-----Examples-----
Input
3 1
1 2 3
2 2
Output
2 1 3
Input
4 2
1 2 4 3
2 3
1 2
Output
2 4 1 3
-----Note-----
In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form.
In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake. | from sys import stdin
n, m = map(int, stdin.readline().split())
a = list(map(int, stdin.readline().split()))
lst = list()
for i in range(m):
t, r = map(int, stdin.readline().split())
while lst and lst[-1][1] < r:
x = lst.pop()
if t == 2:
t = -1
lst.append((t, r))
lst.append((0, 0))
b = sorted(a[: lst[0][1]])
l = 0
r = len(b) - 1
for i in range(len(lst) - 1):
for j in range(lst[i][1] - 1, lst[i + 1][1] - 1, -1):
if lst[i][0] == -1:
a[j] = b[l]
l += 1
else:
a[j] = b[r]
r -= 1
print(" ".join([str(x) for x in a])) | 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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR WHILE VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, the i-th manager either sorts first r_{i} numbers in non-descending or non-ascending order and then passes the report to the manager i + 1, or directly to Blake (if this manager has number i = m).
Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence a_{i} of length n and the description of each manager, that is value r_{i} and his favourite order. You are asked to speed up the process and determine how the final report will look like.
-----Input-----
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of commodities in the report and the number of managers, respectively.
The second line contains n integers a_{i} (|a_{i}| ≤ 10^9) — the initial report before it gets to the first manager.
Then follow m lines with the descriptions of the operations managers are going to perform. The i-th of these lines contains two integers t_{i} and r_{i} ($t_{i} \in \{1,2 \}$, 1 ≤ r_{i} ≤ n), meaning that the i-th manager sorts the first r_{i} numbers either in the non-descending (if t_{i} = 1) or non-ascending (if t_{i} = 2) order.
-----Output-----
Print n integers — the final report, which will be passed to Blake by manager number m.
-----Examples-----
Input
3 1
1 2 3
2 2
Output
2 1 3
Input
4 2
1 2 4 3
2 3
1 2
Output
2 4 1 3
-----Note-----
In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form.
In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake. | 3
class StdReader:
def read_int(self):
return int(self.read_string())
def read_ints(self, sep=None):
return [int(i) for i in self.read_strings(sep)]
def read_float(self):
return float(self.read_string())
def read_floats(self, sep=None):
return [float(i) for i in self.read_strings(sep)]
def read_string(self):
return input()
def read_strings(self, sep=None):
return self.read_string().split(sep)
reader = StdReader()
def part_sort(a, i, j, reverse=False):
a[i:j] = sorted(a[i:j], reverse=reverse)
def part_sorted(b, i, j, reverse=False):
a = list(b)
part_sort(a, i, j, reverse)
return a
def main():
n, m = reader.read_ints()
a = reader.read_ints()
ops = []
for i in range(m):
t, r = reader.read_ints()
op = t, r
while ops and op[1] >= ops[-1][1]:
ops.pop()
ops.append(op)
max_r = ops[0][1]
b = sorted(a[:max_r])
bl = 0
br = max_r - 1
for i in range(len(ops)):
t, r = ops[i]
r1 = 0
if i < len(ops) - 1:
t1, r1 = ops[i + 1]
k = r - r1
if t == 1:
for p in range(k):
a[r - 1 - p] = b[br]
br -= 1
else:
for p in range(k):
a[r - 1 - p] = b[bl]
bl += 1
for ai in a:
print(ai, end=" ")
def __starting_point():
main()
__starting_point() | EXPR NUMBER CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF NONE RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF NONE RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF NONE RETURN FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR WHILE VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, the i-th manager either sorts first r_{i} numbers in non-descending or non-ascending order and then passes the report to the manager i + 1, or directly to Blake (if this manager has number i = m).
Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence a_{i} of length n and the description of each manager, that is value r_{i} and his favourite order. You are asked to speed up the process and determine how the final report will look like.
-----Input-----
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of commodities in the report and the number of managers, respectively.
The second line contains n integers a_{i} (|a_{i}| ≤ 10^9) — the initial report before it gets to the first manager.
Then follow m lines with the descriptions of the operations managers are going to perform. The i-th of these lines contains two integers t_{i} and r_{i} ($t_{i} \in \{1,2 \}$, 1 ≤ r_{i} ≤ n), meaning that the i-th manager sorts the first r_{i} numbers either in the non-descending (if t_{i} = 1) or non-ascending (if t_{i} = 2) order.
-----Output-----
Print n integers — the final report, which will be passed to Blake by manager number m.
-----Examples-----
Input
3 1
1 2 3
2 2
Output
2 1 3
Input
4 2
1 2 4 3
2 3
1 2
Output
2 4 1 3
-----Note-----
In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form.
In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake. | import sys
n, m = [int(x) for x in input().split()]
notes = [int(x) for x in input().split()]
managers = []
for line in sys.stdin:
managers.append(tuple(int(x) for x in line.split()))
bearing_managers = []
M, T = 0, 0
for t, r in reversed(managers):
if r > M:
if t == T:
bearing_managers[-1] = t, r
else:
T = t
bearing_managers.append((t, r))
M = r
bearing_managers.reverse()
t, r = bearing_managers[0]
bearing_managers.append((2 if bearing_managers[-1][0] == 1 else 1, 0))
L, R = 0, r - 1
sorted_part = list(sorted(notes[:r]))
for t1, r1 in bearing_managers:
if t1 == 2:
for i in range(r - 1, r1 - 1, -1):
notes[i] = sorted_part[R]
R -= 1
else:
for i in range(r - 1, r1 - 1, -1):
notes[i] = sorted_part[L]
L += 1
r = r1
for x in notes:
sys.stdout.write(str(x) + " ") | IMPORT 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 LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING |
Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, the i-th manager either sorts first r_{i} numbers in non-descending or non-ascending order and then passes the report to the manager i + 1, or directly to Blake (if this manager has number i = m).
Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence a_{i} of length n and the description of each manager, that is value r_{i} and his favourite order. You are asked to speed up the process and determine how the final report will look like.
-----Input-----
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of commodities in the report and the number of managers, respectively.
The second line contains n integers a_{i} (|a_{i}| ≤ 10^9) — the initial report before it gets to the first manager.
Then follow m lines with the descriptions of the operations managers are going to perform. The i-th of these lines contains two integers t_{i} and r_{i} ($t_{i} \in \{1,2 \}$, 1 ≤ r_{i} ≤ n), meaning that the i-th manager sorts the first r_{i} numbers either in the non-descending (if t_{i} = 1) or non-ascending (if t_{i} = 2) order.
-----Output-----
Print n integers — the final report, which will be passed to Blake by manager number m.
-----Examples-----
Input
3 1
1 2 3
2 2
Output
2 1 3
Input
4 2
1 2 4 3
2 3
1 2
Output
2 4 1 3
-----Note-----
In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form.
In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake. | from sys import stdin, stdout
n, m = map(int, stdin.readline().split())
b = list(map(int, stdin.readline().split()))
a = []
for i in range(m):
a.append(tuple(map(int, stdin.readline().split())))
a = a[::-1]
r = [a[0]]
now = a[0]
f = 0
for i in range(m):
if a[i][1] > now[1]:
if now[0] == a[i][0] and f:
r.remove(now)
r.append(a[i])
now = a[i]
f = 1
maxi = r[-1]
vos = sorted(b[0 : maxi[1]])
ub = vos[::-1]
u = maxi[1] - 1
v = maxi[1] - 1
l = len(r)
r = [(0, 0)] + r
res = [0] * n
pr = [0] * n
for i in range(len(r) - 1):
for j in range(r[i][1], r[i + 1][1]):
pr[j] = r[i + 1][0]
for i in range(n - 1, -1, -1):
if pr[i] == 1:
res[i] = vos[v]
v -= 1
elif pr[i] == 2:
res[i] = ub[u]
u -= 1
else:
res[i] = b[i]
res = list(map(str, res))
stdout.write(" ".join(res)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, the i-th manager either sorts first r_{i} numbers in non-descending or non-ascending order and then passes the report to the manager i + 1, or directly to Blake (if this manager has number i = m).
Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence a_{i} of length n and the description of each manager, that is value r_{i} and his favourite order. You are asked to speed up the process and determine how the final report will look like.
-----Input-----
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of commodities in the report and the number of managers, respectively.
The second line contains n integers a_{i} (|a_{i}| ≤ 10^9) — the initial report before it gets to the first manager.
Then follow m lines with the descriptions of the operations managers are going to perform. The i-th of these lines contains two integers t_{i} and r_{i} ($t_{i} \in \{1,2 \}$, 1 ≤ r_{i} ≤ n), meaning that the i-th manager sorts the first r_{i} numbers either in the non-descending (if t_{i} = 1) or non-ascending (if t_{i} = 2) order.
-----Output-----
Print n integers — the final report, which will be passed to Blake by manager number m.
-----Examples-----
Input
3 1
1 2 3
2 2
Output
2 1 3
Input
4 2
1 2 4 3
2 3
1 2
Output
2 4 1 3
-----Note-----
In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form.
In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake. | n, m = map(int, input().split())
a = list(map(int, input().split()))
deq = []
for i in range(0, m):
t, r = map(int, input().split())
while deq and deq[-1][1] < r:
deq.pop(-1)
deq.append((t, r))
b = sorted(a[0 : deq[0][1]])
seq = []
for t, r in deq:
if seq:
it = (t,) + (
(seq[-1][2] - r, seq[-1][2])
if seq[-1][0] == 2
else (seq[-1][1], seq[-1][1] + r)
)
seq[-1] = (seq[-1][0],) + (
(seq[-1][1], seq[-1][2] - r)
if seq[-1][0] == 2
else (seq[-1][1] + r, seq[-1][2])
)
seq.append(it)
else:
seq.append((t, 0, r))
c = []
for t, l, r in reversed(seq):
c.extend((lambda x: x if t == 1 else reversed(x))(b[l:r]))
c.extend(a[deq[0][1] :])
print(" ".join(map(str, c))) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR VAR IF VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST FOR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, the i-th manager either sorts first r_{i} numbers in non-descending or non-ascending order and then passes the report to the manager i + 1, or directly to Blake (if this manager has number i = m).
Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence a_{i} of length n and the description of each manager, that is value r_{i} and his favourite order. You are asked to speed up the process and determine how the final report will look like.
-----Input-----
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of commodities in the report and the number of managers, respectively.
The second line contains n integers a_{i} (|a_{i}| ≤ 10^9) — the initial report before it gets to the first manager.
Then follow m lines with the descriptions of the operations managers are going to perform. The i-th of these lines contains two integers t_{i} and r_{i} ($t_{i} \in \{1,2 \}$, 1 ≤ r_{i} ≤ n), meaning that the i-th manager sorts the first r_{i} numbers either in the non-descending (if t_{i} = 1) or non-ascending (if t_{i} = 2) order.
-----Output-----
Print n integers — the final report, which will be passed to Blake by manager number m.
-----Examples-----
Input
3 1
1 2 3
2 2
Output
2 1 3
Input
4 2
1 2 4 3
2 3
1 2
Output
2 4 1 3
-----Note-----
In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form.
In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake. | import sys
input = sys.stdin.buffer.readline
n, m = map(int, input().split())
a = list(map(int, input().split()))
s = []
for i in range(m):
t, r = map(int, input().split())
t -= 1
r -= 1
while s and s[-1][1] <= r:
s.pop()
s.append([t, r])
s.append([-1, -1])
sorted_slice = sorted(a[: s[0][1] + 1])
l = 0
r = s[0][1]
for i in range(len(s) - 1):
if s[i][0]:
for j in range(s[i][1], s[i + 1][1], -1):
a[j] = sorted_slice[l]
l += 1
else:
for j in range(s[i][1], s[i + 1][1], -1):
a[j] = sorted_slice[r]
r -= 1
print(*a) | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, the i-th manager either sorts first r_{i} numbers in non-descending or non-ascending order and then passes the report to the manager i + 1, or directly to Blake (if this manager has number i = m).
Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence a_{i} of length n and the description of each manager, that is value r_{i} and his favourite order. You are asked to speed up the process and determine how the final report will look like.
-----Input-----
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of commodities in the report and the number of managers, respectively.
The second line contains n integers a_{i} (|a_{i}| ≤ 10^9) — the initial report before it gets to the first manager.
Then follow m lines with the descriptions of the operations managers are going to perform. The i-th of these lines contains two integers t_{i} and r_{i} ($t_{i} \in \{1,2 \}$, 1 ≤ r_{i} ≤ n), meaning that the i-th manager sorts the first r_{i} numbers either in the non-descending (if t_{i} = 1) or non-ascending (if t_{i} = 2) order.
-----Output-----
Print n integers — the final report, which will be passed to Blake by manager number m.
-----Examples-----
Input
3 1
1 2 3
2 2
Output
2 1 3
Input
4 2
1 2 4 3
2 3
1 2
Output
2 4 1 3
-----Note-----
In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form.
In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake. | from sys import stdin, stdout
nmbr = lambda: int(input())
lst = lambda: list(map(int, input().split()))
for _ in range(1):
n, m = lst()
a = lst()
l = [lst() for _ in range(m)]
suf = [0] * (1 + m)
suf[m - 1] = l[m - 1][1]
for i in range(m - 2, -1, -1):
suf[i] = max(suf[i + 1], l[i][1])
chng = []
for i in range(m):
if suf[i] == l[i][1]:
if not chng or l[i][0] != chng[-1][0]:
chng += [[l[i][0], l[i][1]]]
if chng[0][0] == 1:
pos = chng[0][1] - 1
ans = sorted(a[: pos + 1]) + a[pos + 1 :]
next = 1
else:
pos = chng[0][1] - 1
ans = sorted(a[: pos + 1], reverse=True) + a[pos + 1 :]
next = 1
ln = len(chng)
c = 0
if ln >= 2:
c = chng[1][1] - 1
start, end = 0, c
a = ans.copy()
for i in range(1, ln):
if next & 1:
diff = chng[i][1] - (0 if i + 1 >= ln else chng[i + 1][1])
while diff > 0:
ans[c] = a[start]
c -= 1
start += 1
diff -= 1
else:
diff = chng[i][1] - (0 if i + 1 >= ln else chng[i + 1][1])
while diff > 0:
ans[c] = a[end]
c -= 1
end -= 1
diff -= 1
next += 1
print(*ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER NUMBER VAR LIST LIST VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, the i-th manager either sorts first r_{i} numbers in non-descending or non-ascending order and then passes the report to the manager i + 1, or directly to Blake (if this manager has number i = m).
Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence a_{i} of length n and the description of each manager, that is value r_{i} and his favourite order. You are asked to speed up the process and determine how the final report will look like.
-----Input-----
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of commodities in the report and the number of managers, respectively.
The second line contains n integers a_{i} (|a_{i}| ≤ 10^9) — the initial report before it gets to the first manager.
Then follow m lines with the descriptions of the operations managers are going to perform. The i-th of these lines contains two integers t_{i} and r_{i} ($t_{i} \in \{1,2 \}$, 1 ≤ r_{i} ≤ n), meaning that the i-th manager sorts the first r_{i} numbers either in the non-descending (if t_{i} = 1) or non-ascending (if t_{i} = 2) order.
-----Output-----
Print n integers — the final report, which will be passed to Blake by manager number m.
-----Examples-----
Input
3 1
1 2 3
2 2
Output
2 1 3
Input
4 2
1 2 4 3
2 3
1 2
Output
2 4 1 3
-----Note-----
In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form.
In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake. | n, m = [int(i) for i in input().split()]
nl = [int(i) for i in input().split()]
ml = []
for i in range(m):
t, r = [int(k) for k in input().split()]
ml.append((t, r))
mll = [(0, 0)]
for i in range(m - 1, -1, -1):
if i == m - 1:
mll.append(ml[i])
elif i != m - 1 and ml[i][1] > mll[-1][1]:
mll.append(ml[i])
temp = sorted(nl[: mll[len(mll) - 1][1]])
x, y = 0, len(temp) - 1
for i in range(len(mll) - 1, 0, -1):
t, r = mll[i]
for j in range(r - 1, mll[i - 1][1] - 1, -1):
if t == 1:
nl[j] = temp[y]
y -= 1
elif t == 2:
nl[j] = temp[x]
x += 1
for i in range(n):
nl[i] = str(nl[i])
print(" ".join(nl)) | 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 LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, the i-th manager either sorts first r_{i} numbers in non-descending or non-ascending order and then passes the report to the manager i + 1, or directly to Blake (if this manager has number i = m).
Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence a_{i} of length n and the description of each manager, that is value r_{i} and his favourite order. You are asked to speed up the process and determine how the final report will look like.
-----Input-----
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of commodities in the report and the number of managers, respectively.
The second line contains n integers a_{i} (|a_{i}| ≤ 10^9) — the initial report before it gets to the first manager.
Then follow m lines with the descriptions of the operations managers are going to perform. The i-th of these lines contains two integers t_{i} and r_{i} ($t_{i} \in \{1,2 \}$, 1 ≤ r_{i} ≤ n), meaning that the i-th manager sorts the first r_{i} numbers either in the non-descending (if t_{i} = 1) or non-ascending (if t_{i} = 2) order.
-----Output-----
Print n integers — the final report, which will be passed to Blake by manager number m.
-----Examples-----
Input
3 1
1 2 3
2 2
Output
2 1 3
Input
4 2
1 2 4 3
2 3
1 2
Output
2 4 1 3
-----Note-----
In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form.
In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake. | n, m = [int(i) for i in input().split()]
rev = [int(i) for i in input().split()]
leng = len(rev)
result = list()
mark = list()
while m > 0:
t, r = [int(i) for i in input().split()]
while len(mark) > 0 and r >= mark[-1][1]:
mark.pop()
mark.append([t, r])
m -= 1
mark.append([0, 0])
length = len(mark)
start = mark[0][1]
seq = mark[0][0]
x, y = 0, start - 1
result = sorted(rev[:start])
for i in range(1, length):
for j in range(mark[i - 1][1], mark[i][1], -1):
if mark[i - 1][0] == 1:
rev[j - 1] = result[y]
y -= 1
else:
rev[j - 1] = result[x]
x += 1
for i in range(0, leng):
print(rev[i], sep=" ", end=" ") | 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 FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR STRING STRING |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.