description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
xs = collections.defaultdict(set)
ys = collections.defaultdict(set)
for point in points:
x, y = point[0], point[1]
xs[x].add(y)
ys[y].add(x)
area = 40000**2 + 1
for x in xs:
for y1, y2 in itertools.product(xs[x], xs[x]):
if y1 <= y2:
continue
A = set(xx for xx in ys[y1] & ys[y2] if xx < x)
if len(A) > 0:
area = min(area, (y1 - y2) * (x - max(A)))
return 0 if area == 40000**2 + 1 else area | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER NUMBER VAR VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
columns = collections.defaultdict(list)
res = float("inf")
for x, y in points:
columns[x].append(y)
y_pairs = dict()
for x in sorted(columns):
ys = sorted(columns[x])
for i in range(len(ys) - 1):
for j in range(i + 1, len(ys)):
if (ys[i], ys[j]) in y_pairs:
res = min(res, (x - y_pairs[ys[i], ys[j]]) * (ys[j] - ys[i]))
y_pairs[ys[i], ys[j]] = x
return res if res != float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
x_memo = collections.defaultdict(set)
for x, y in points:
x_memo[x].add(y)
x_lst = sorted(list(x_memo.keys()))
res = math.inf
for i in range(len(x_lst)):
for j in range(i + 1, len(x_lst)):
dx = x_lst[j] - x_lst[i]
y_common = x_memo[x_lst[i]].intersection(x_memo[x_lst[j]])
if len(y_common) < 2:
continue
y_common = sorted(list(y_common))
for k in range(len(y_common) - 1):
res = min(res, dx * (y_common[k + 1] - y_common[k]))
if math.isfinite(res):
return res
else:
return 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR RETURN VAR RETURN NUMBER VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
min_area = 999999999999999999
points = [(l[0], l[1]) for l in points]
s_points = set(points)
for p1 in points:
for p2 in points:
x1, y1 = p1
x2, y2 = p2
if x1 == x2 or y1 == y2:
continue
area = abs(x2 - x1) * abs(y2 - y1)
if area < min_area:
if (x1, y2) in s_points and (x2, y1) in s_points:
min_area = area
if min_area is 999999999999999999:
return 0
return min_area | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER RETURN VAR VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
xAxis = {}
yAxis = {}
minArea = None
for point in points:
if xAxis.get(point[0]):
xAxis[point[0]].append(point[1])
else:
xAxis[point[0]] = [point[1]]
if yAxis.get(point[1]):
yAxis[point[1]].append(point[0])
else:
yAxis[point[1]] = [point[0]]
for point in points:
topLeft = xAxis[point[0]]
bottomRight = yAxis[point[1]]
for tlPoint in topLeft:
if tlPoint == point[1]:
continue
possibleTopRight = yAxis[tlPoint]
topRight = [
value
for value in possibleTopRight
if value in bottomRight and value != point[0]
]
for trPoint in topRight:
area = abs((trPoint - point[0]) * (tlPoint - point[1]))
if area != 0 and (minArea == None or area < minArea):
minArea = area
xAxis[point[0]].remove(point[1])
yAxis[point[1]].remove(point[0])
return 0 if minArea == None else minArea | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NONE FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER LIST VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER LIST VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NONE VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR NONE NUMBER VAR VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
if not points:
return 0
x_lines, y_lines = {}, {}
for x, y in points:
x_lines[x] = x_lines.get(x, set()) | {y}
y_lines[y] = y_lines.get(y, set()) | {x}
res = float("inf")
for x, y_s in x_lines.items():
if len(y_s) < 2:
continue
y_s = list(y_s)
for i in range(len(y_s)):
for j in range(i + 1, len(y_s)):
y1, y2 = y_s[i], y_s[j]
inter = y_lines[y1].intersection(y_lines[y2])
if len(inter) <= 1:
continue
for x2 in inter:
if x2 == x:
continue
res = min(res, abs(y1 - y2) * abs(x - x2))
return res if res != float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR DICT DICT FOR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
points.sort(key=lambda x: (x[0], x[1]))
mx_y = defaultdict(list)
for x, y in points:
mx_y[x].append(y)
res = float("inf")
seenY_pairs = {}
for x in mx_y:
if len(mx_y[x]) < 2:
continue
for i in range(len(mx_y[x]) - 1):
y1 = mx_y[x][i]
for j in range(i + 1, len(mx_y[x])):
y2 = mx_y[x][j]
if (y1, y2) in seenY_pairs:
width = x - seenY_pairs[y1, y2]
height = y2 - y1
res = min(res, width * height)
seenY_pairs[y1, y2] = x
if res == float("inf"):
return 0
else:
return res | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
points.sort()
points_set = set([tuple(point) for point in points])
smallest = float("inf")
for i, (x1, y1) in enumerate(points):
for j, (x2, y2) in enumerate(points[i:]):
if (
x1 < x2
and y1 < y2
and (x1, y2) in points_set
and (x2, y1) in points_set
):
area = (x2 - x1) * (y2 - y1)
smallest = min(smallest, area)
return smallest if smallest != float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
record = {}
points.sort(key=lambda x: [x[0], x[1]])
res = float("inf")
temp = [points[0][1]]
for i in range(1, len(points)):
if points[i][0] == points[i - 1][0]:
temp.append(points[i][1])
else:
if len(temp) >= 2:
for j in range(len(temp) - 1):
for k in range(j + 1, len(temp)):
if (temp[j], temp[k]) in record:
res = min(
res,
(points[i - 1][0] - record[temp[j], temp[k]])
* (temp[k] - temp[j]),
)
record[temp[j], temp[k]] = points[i - 1][0]
temp = [points[i][1]]
if len(temp) >= 2:
for j in range(len(temp) - 1):
for k in range(j + 1, len(temp)):
if (temp[j], temp[k]) in record:
res = min(
res,
(points[-1][0] - record[temp[j], temp[k]])
* (temp[k] - temp[j]),
)
if res == float("inf"):
return 0
return res | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF 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 NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST VAR VAR NUMBER IF 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 NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
data = set()
res = 2**31 - 1
for x1, y1 in points:
for x2, y2 in data:
if (x1, y2) in data and (x2, y1) in data:
res = min(res, abs(x1 - x2) * abs(y1 - y2))
data.add((x1, y1))
return res if res < 2**31 - 1 else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR NUMBER VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
area = float("inf")
x_order = collections.defaultdict(list)
y_order = collections.defaultdict(list)
for p in points:
x_order[p[0]].append(p[1])
y_order[p[1]].append(p[0])
xs = sorted(list(x_order.items()), key=lambda x: x[0])
for col in xs:
if len(col[1]) < 2:
continue
for i in range(len(col[1]) - 1):
j = i + 1
while j < len(col[1]):
common_x = list(set(y_order[col[1][i]]) & set(y_order[col[1][j]]))
common_x.remove(col[0])
if common_x:
for x in common_x:
a = abs(col[0] - x) * abs(col[1][i] - col[1][j])
area = min(area, a)
j += 1
return area if area < float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
if len(points) < 4:
return 0
min_area = 40000 * 40000 + 1
seen_points = set()
for x, y in points:
seen_points.add((x, y))
for p1_x, p1_y in points:
for p2_x, p2_y in points:
if p1_x > p2_x and p1_y > p2_y:
if (p1_x, p2_y) in seen_points and (p2_x, p1_y) in seen_points:
area = abs(p2_x - p1_x) * abs(p2_y - p1_y)
if area > 0:
min_area = min(min_area, area)
if min_area == 40000 * 40000 + 1:
return 0
return min_area | CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN NUMBER RETURN VAR VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
len_x = {x for x, y in points}
len_y = {y for x, y in points}
if len(len_x) == len(points) or len(len_y) == len(points):
return 0
memo = collections.defaultdict(list)
if len_x > len_y:
for x, y in points:
memo[x].append(y)
else:
for x, y in points:
memo[y].append(x)
res = float("inf")
memo2 = {}
for i in sorted(memo):
memo[i].sort()
for j in range(len(memo[i])):
for k in range(j + 1, len(memo[i])):
if (memo[i][j], memo[i][k]) in memo2:
res = min(
res,
(memo[i][k] - memo[i][j])
* abs(memo2[memo[i][j], memo[i][k]] - i),
)
memo2[memo[i][j], memo[i][k]] = i
return res if res < float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
pts = set()
for p in points:
pts.add((p[0], p[1]))
ans = float("inf")
flag = False
P = len(points)
for i in range(P):
for j in range(i + 1, P):
if points[i][0] == points[j][0] or points[i][1] == points[j][1]:
continue
else:
dx1 = points[i][0]
dy1 = points[j][1]
dx2 = points[j][0]
dy2 = points[i][1]
if (dx1, dy1) in pts and (dx2, dy2) in pts:
area = abs(dy1 - dy2) * abs(dx1 - dx2)
ans = min(area, ans)
flag = True
return ans if flag else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR VAR NUMBER VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
point_set = set()
points.sort()
for point in points:
point_set.add((point[0], point[1]))
def getArea(point1: List[int], point2: List[int]) -> int:
if point2[0] <= point1[0] or point2[1] <= point1[1]:
return 0
if (point1[0], point2[1]) in point_set and (
point2[0],
point1[1],
) in point_set:
return (point2[0] - point1[0]) * (point2[1] - point1[1])
return 0
min_area = math.inf
N = len(points)
for i in range(N):
start_point = points[i]
for j in range(i + 1, N):
area = getArea(start_point, points[j])
if area > 0:
min_area = min(min_area, area)
return min_area if min_area != math.inf else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_DEF VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR NUMBER VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
points.sort()
n = len(points)
result = math.inf
seen = set()
for i in range(n):
x1, y1 = points[i]
for j in range(i + 1, n):
x2, y2 = points[j]
if x2 == x1:
continue
a = x1, y2, x2, y1
if a in seen:
result = min(result, (x2 - x1) * abs(y2 - y1))
seen.add((x1, y1, x2, y2))
return 0 if math.isinf(result) else result | CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
s = set()
for point in points:
x = point[0]
y = point[1]
s.add((x, y))
res = float("inf")
for i in range(len(points) - 1):
for j in range(i + 1, len(points)):
x1 = points[i][0]
y1 = points[i][1]
x2 = points[j][0]
y2 = points[j][1]
if x1 == x2 or y1 == y2:
continue
if (x1, y2) in s and (x2, y1) in s:
res = min(res, abs((y2 - y1) * (x2 - x1)))
return res if res != float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
def intersection(lst1, lst2):
return list(set(lst1) & set(lst2))
col = defaultdict(list)
ans = float("inf")
for point in points:
col[point[0]].append(point[1])
cols = list(col.keys())
for i1 in range(len(cols)):
for i2 in range(i1 + 1, len(cols)):
col1 = cols[i1]
col2 = cols[i2]
il = sorted(intersection(col[col1], col[col2]))
for i in range(1, len(il)):
ans = min(ans, (il[i] - il[i - 1]) * abs(col2 - col1))
return ans if ans < float("inf") else 0 | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR |
Given a set of points in the xy-plane, determine the minimum area of a rectangle formed from these points, with sides parallel to the x and y axes.
If there isn't any rectangle, return 0.
Example 1:
Input: [[1,1],[1,3],[3,1],[3,3],[2,2]]
Output: 4
Example 2:
Input: [[1,1],[1,3],[3,1],[3,3],[4,1],[4,3]]
Output: 2
Note:
1 <= points.length <= 500
0 <= points[i][0] <= 40000
0 <= points[i][1] <= 40000
All points are distinct. | class Solution:
def minAreaRect(self, points: List[List[int]]) -> int:
mx = defaultdict(set)
for x, y in points:
mx[x].add(y)
ans, n = float("inf"), len(points)
for i in range(n - 1):
x1, y1 = points[i]
for j in range(i + 1, n):
x2, y2 = points[j]
if x2 == x1 or y2 == y1 or y2 not in mx[x1] or y1 not in mx[x2]:
continue
ans = min(abs(x2 - x1) * abs(y2 - y1), ans)
return 0 if ans == float("inf") else ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
a = []
for i in range(len(matrix)):
a.extend(matrix[i])
a.sort()
l = len(a) // 2
if len(a) % 2 == 0:
return a[l + (l - 1)]
else:
return a[l] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR VAR |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
arr = []
for ele in matrix:
for num in ele:
arr.append(num)
arr.sort()
index = len(arr) // 2
return arr[index] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
lis = []
for i in range(R):
lis += matrix[i]
lis.sort()
return lis[len(lis) // 2] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
def countNums(row, target):
l, r = 0, C - 1
while l <= r:
mid = (l + r) // 2
if matrix[row][mid] <= target:
l = mid + 1
else:
r = mid - 1
return l
low, high = 1, 2000
total = R * C // 2
while low <= high:
numsberLessThanTarget = 0
mid = (low + high) // 2
for row in range(0, R):
numsberLessThanTarget += countNums(row, mid)
if numsberLessThanTarget > total:
high = mid - 1
else:
low = mid + 1
return low | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
soln = []
for i in range(R):
for j in range(C):
soln.append(matrix[i][j])
soln.sort()
med = len(soln) // 2
return soln[med] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
check = []
for i in matrix:
check.extend(i)
b = sorted(check)
return b[len(b) // 2] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
temp = []
for i in matrix:
temp.extend(i)
temp.sort()
length = R * C
return temp[length // 2] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR BIN_OP VAR NUMBER |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
def NumOfLess(arr, k):
l = 0
r = len(arr)
count = 0
while l < r:
mid = (l + r) // 2
if arr[mid] <= k:
count = mid + 1
l = mid + 1
else:
r = mid
return count
l = 1
r = 1000000000.0
median = 0
total = R * C
while l <= r:
lessEqual = 0
mid = (l + r) // 2
for i in range(R):
ub = NumOfLess(matrix[i], mid)
lessEqual += ub
if lessEqual > total // 2:
median = mid
r = mid - 1
else:
l = mid + 1
return int(median) | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
low = 1
high = 2000
while low <= high:
mid = (low + high) // 2
count = 0
for i in range(R):
count += self.countsmallerthanmid(matrix[i], mid)
if count <= R * C // 2:
low = mid + 1
else:
high = mid - 1
return int(low)
def countsmallerthanmid(self, row, mid):
l = 0
h = len(row) - 1
while l <= h:
md = (l + h) // 2
if row[md] <= mid:
l = md + 1
else:
h = md - 1
return l | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
a = []
for i in range(len(matrix)):
a.extend(matrix[i])
a.sort()
b = len(a)
if b % 2 != 0:
d = b // 2
return a[d]
else:
e = b // 2
c = a[b // 2] + a[e - 1]
return c / 2 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
l = []
for r in range(len(matrix)):
for c in range(len(matrix[0])):
l.append(matrix[r][c])
l.sort()
mid = len(l) // 2
if len(l) % 2 == 0:
median = (l[mid - 1] + l[mid]) / 2
else:
median = l[mid]
return median | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
minV, maxV = matrix[0][0], matrix[0][C - 1]
for i in range(1, R):
if matrix[i][0] < minV:
minV = matrix[i][0]
if matrix[i][C - 1] > maxV:
maxV = matrix[i][C - 1]
res = minV
low, high = minV, maxV
while low <= high:
mid = (low + high) // 2
count = 0
for i in range(R):
count += self.elementsLessOrEqualToMid(matrix[i], mid, C)
if count <= R * C // 2:
low = mid + 1
else:
high = mid - 1
return low
def elementsLessOrEqualToMid(self, arr, item, C):
low, high = 0, C - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] <= item:
low = mid + 1
else:
high = mid - 1
return low | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
s = 1
e = 2000
n = R * C
mid = 0
while s <= e:
mid = (s + e) // 2
ans = 0
for i in range(R):
l = 0
h = C - 1
while l <= h:
m = (l + h) // 2
if matrix[i][m] <= mid:
l = m + 1
else:
h = m - 1
ans += l
if ans <= n // 2:
s = mid + 1
else:
e = mid - 1
return s | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
l = [j for i in matrix for j in i]
l.sort()
if len(l) % 2 == 0:
v = len(l) // 2
return l[v] + l[v - 1] / 2
else:
v = len(l) // 2
return l[v] | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def no_of_elements_lesser_or_equal(self, matrix, item, C):
cnt = 0
for arr in matrix:
low, high = 0, C - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] > item:
high = mid - 1
else:
low = mid + 1
cnt += low
return cnt
def median(self, matrix, R, C):
low, high, n = 1, 2000, R * C
while low <= high:
mid = (low + high) // 2
cnt = self.no_of_elements_lesser_or_equal(matrix, mid, C)
if cnt > n // 2:
high = mid - 1
else:
low = mid + 1
return low | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER BIN_OP VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, r, c):
def count(mat, mid):
l = 0
h = len(mat) - 1
while l <= h:
m = (l + h) // 2
if mat[m] > mid:
h = m - 1
else:
l = m + 1
return l
low = 0
high = 2001
while low <= high:
mid = (low + high) // 2
cnt = 0
for mat in matrix:
cnt += count(mat, mid)
if cnt <= r * c // 2:
low = mid + 1
else:
high = mid - 1
return low | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
list = []
for x in matrix:
list += x
list.sort()
n = len(list)
if len(list) % 2 != 0:
return list[int((n + 1) / 2 - 1)]
return (list[int(n / 2 - 1)] + list[int(n / 2)]) / 2 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
def numLessEqualMatrix(num):
total = 0
for i in range(R):
start = 0
end = C - 1
ans = -1
while start <= end:
mid = (start + end) // 2
if matrix[i][mid] <= num:
ans = mid
start = mid + 1
else:
end = mid - 1
total += ans + 1
return total
start = 0
end = 10**9
while start < end:
mid = (start + end) // 2
if numLessEqualMatrix(mid) < R * C / 2:
start = mid + 1
else:
end = mid
return start | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
l = []
for i in range(len(matrix)):
for j in range(len(matrix[i])):
l.append(matrix[i][j])
m = sorted(l)
le = len(m)
med = le // 2
return m[med] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
def binarySearch(arr, low, high, value):
while low <= high:
mid = (low + high) // 2
if arr[mid] > value:
high = mid - 1
else:
low = mid + 1
return low
low = 1
high = 2000
while low <= high:
count = 0
mid = (low + high) // 2
for i in range(R):
count = count + binarySearch(matrix[i], 0, C - 1, mid)
if count <= R * C // 2:
low = mid + 1
else:
high = mid - 1
return low | CLASS_DEF FUNC_DEF FUNC_DEF WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
mn = matrix[0][0]
mx = matrix[0][C - 1]
for i in range(R):
if matrix[i][0] < mn:
mn = matrix[i][0]
if matrix[i][C - 1] > mx:
mx = matrix[i][C - 1]
target_count = (R * C + 1) // 2
while mn < mx:
count = 0
mid = (mn + mx) // 2
for i in range(R):
for j in range(C):
if matrix[i][j] > mid:
break
count += 1
if count < target_count:
mn = mid + 1
else:
mx = mid
return mx | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
a = []
for i in range(len(matrix)):
a += matrix[i]
a = sorted(a)
if len(a) % 2 == 0:
return (a[len(a) // 2] + a[len(a) // 2 - 1]) / 2
else:
return a[len(a) // 2] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
l = []
for i in range(len(matrix)):
for j in range(len(matrix[0])):
l.append(matrix[i][j])
l.sort()
s = 0
e = len(l) - 1
mid = (s + e) // 2
return l[mid] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
r = []
for i in range(R):
for j in range(C):
r.append(matrix[i][j])
r = sorted(r)
return r[int((len(r) - 1) / 2)] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
new = []
for row in matrix:
new.extend(row)
new.sort()
n = R * C
median = (n + 1) // 2
return new[median - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR BIN_OP VAR NUMBER |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
nums = []
for i in matrix:
nums.extend(i)
mid = len(nums) // 2
nums.sort()
return nums[mid] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR VAR |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
l = []
for i in matrix:
for j in i:
l.append(j)
l.sort()
n = int(len(l) / 2) + 1
return l[n - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR BIN_OP VAR NUMBER |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
list1 = []
for r in range(len(matrix)):
for c in range(len(matrix[0])):
list1.append(matrix[r][c])
n = len(list1)
list1.sort()
if n % 2 == 0:
return (list1[n // 2] + list1[n // 2 + 1]) / 2
return list1[n // 2] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER RETURN VAR BIN_OP VAR NUMBER |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | import sys
def findcount(arr, target):
left = 0
right = len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] <= target:
left = mid + 1
else:
right = mid - 1
return left
class Solution:
def median(self, arr, R, C):
left = -1
right = -1
for row in range(R):
left = min(left, arr[row][0])
right = max(right, arr[row][-1])
desired = (R * C + 1) // 2
while left < right:
mid = (left + right) // 2
cnt = 0
for i in range(R):
cnt += findcount(arr[i], mid)
if cnt < desired:
left = mid + 1
else:
right = mid
return left | IMPORT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
l = 1
h = 2000
def helper(i, elem):
lo = 0
hi = C - 1
while lo <= hi:
mid = (lo + hi) // 2
if matrix[i][mid] <= elem:
lo = mid + 1
else:
hi = mid - 1
return lo
while l <= h:
mid = (l + h) // 2
count = 0
for i in range(R):
count += helper(i, mid)
if count <= R * C // 2:
l = mid + 1
else:
h = mid - 1
return l | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
p = -1
n = R * C
h = [0] * n
for i in range(R):
for j in range(C):
p = p + 1
h[p] = matrix[i][j]
h.sort()
if n % 2 != 0:
q = n // 2
ans = h[q]
else:
q = n // 2
ans = (h[q] + h[q - 1]) / 2
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
list_1 = []
for sublist in matrix:
for val in sublist:
list_1.append(val)
list_1.sort()
return list_1[int(R * C / 2)] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
final = []
for i in range(0, R):
for j in range(0, C):
final.append(matrix[i][j])
final = sorted(final)
mid = R * C // 2
return final[mid] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution(object):
def median(self, matrix, R, C):
def countLesserNums(medianNum):
countOfSmallerNums = 0
for row in range(R):
left, right = 0, C - 1
while left <= right:
col = (left + right) // 2
if matrix[row][col] <= medianNum:
left = col + 1
else:
right = col - 1
countOfSmallerNums += left
return countOfSmallerNums
n = R * C
start, end = 0, 2000
while start <= end:
medianNum = (start + end) // 2
medianIndex = n // 2
lesserNumsCount = countLesserNums(medianNum)
if lesserNumsCount <= medianIndex:
start = medianNum + 1
else:
end = medianNum - 1
return start | CLASS_DEF VAR FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
arr = []
for i in range(len(matrix)):
for j in range(len(matrix[0])):
arr.append(matrix[i][j])
arr.sort()
return arr[len(arr) // 2] | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | class Solution:
def median(self, matrix, R, C):
ans = []
for row in matrix:
for i in row:
ans.append(i)
ans.sort()
N = len(ans)
median = ans[N // 2]
k = 0
for i in range(R):
for j in range(C):
matrix[i][j] = ans[k]
k += 1
return median | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR |
Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.
Example 1:
Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.
Example 2:
Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.
Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.
Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)
Constraints:
1 <= R, C <= 400
1 <= matrix[i][j] <= 2000 | from itertools import chain
class Solution:
def median(self, matrix, R, C):
from itertools import chain
result = list(chain(*matrix))
result.sort()
return result[len(result) // 2] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | class BitTree:
def __init__(self, size):
self.size = size
self.arr = [(0) for _ in range(size + 1)]
def add(self, idx, val):
i = idx
while i <= self.size:
self.arr[i] += val
i += i & -i
def sum(self, idx):
ret = 0
i = idx
while i > 0:
ret += self.arr[i]
i -= i & -i
return ret
def find_left(self, idx, k):
x = self.sum(idx)
l, r = 1, idx
while l <= r:
m = (l + r) // 2
tmp = self.sum(idx) - self.sum(m - 1)
if tmp > k:
l = m + 1
else:
r = m - 1
return l
def find_right(self, idx, k):
x = self.sum(idx)
l, r = idx, self.size
while l <= r:
m = (l + r) // 2
tmp = self.sum(m - 1) - self.sum(idx)
if tmp < k:
r = m - 1
else:
l = m + 1
return r
def find_left(lst, ans, idx, k, tag):
idx = lst[idx][0]
while k > 0 and idx >= 1:
ans[idx] = tag
k -= 1
idx = lst[idx][0]
return max(idx, 0)
def find_right(lst, ans, idx, k, tag, n):
idx = lst[idx][1]
while k > 0 and idx <= n:
ans[idx] = tag
k -= 1
idx = lst[idx][1]
return min(idx, n + 1)
def main():
line = input()
n, k = int(line.split(" ")[0]), int(line.split(" ")[1])
line = input()
ans = ["0" for _ in range(n + 2)]
arr = []
lst = []
for i in range(n + 2):
lst.append([max(i - 1, 0), min(i + 1, n + 1)])
for i, val in enumerate(line.split(" ")):
arr.append((i + 1, int(val)))
arr.sort(key=lambda x: x[1], reverse=True)
tag = 1
for item in arr:
idx = item[0]
if ans[idx] != "0":
continue
ans[idx] = str(tag)
left = find_left(lst, ans, idx, k, str(tag))
right = find_right(lst, ans, idx, k, str(tag), n)
lst[left][1] = right
lst[right][0] = left
tag = 3 - tag
print("".join(ans[1:-1]))
main() | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR NUMBER WHILE VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER NUMBER EXPR FUNC_CALL VAR |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | n, k = tuple([int(i) for i in input().split()])
data = [int(i) for i in input().split()]
nextup = {}
nextdown = {}
nextup[data[n - 1]] = -1
nextdown[data[0]] = -1
for i in range(n - 1):
nextup[data[i]] = data[i + 1]
nextdown[data[i + 1]] = data[i]
l = list(range(1, n + 1))
switch = {"1": "2", "2": "1"}
char = "2"
for j in range(n - 1, -1, -1):
i = l[j]
if type(i) == int:
char = switch[char]
b = i
c = i
l[i - 1] = char
found = 0
while found < k:
t = nextdown[b]
if t == -1:
break
found += 1
l[t - 1] = char
b = t
found = 0
while found < k:
t = nextup[c]
if t == -1:
break
found += 1
l[t - 1] = char
c = t
nextdown[nextup[c]] = nextdown[b]
nextup[nextdown[b]] = nextup[c]
for i in data:
print(l[i - 1], end="")
print() | ASSIGN VAR VAR FUNC_CALL 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 DICT ASSIGN VAR DICT ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR DICT STRING STRING STRING STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | n, k = map(int, input().split())
A = list(map(int, input().split()))
A = [(a, i) for i, a in enumerate(A)]
A.sort(key=lambda x: x[0])
A.reverse()
left = [-1] * n
for i in range(1, n):
left[i] = i - 1
right = [-1] * n
for i in range(n - 1):
right[i] = i + 1
vis = [0] * n
join = [0] * n
jo = 1
for i in range(n):
if not vis[A[i][1]]:
cur = A[i][1]
vis[cur] = 1
join[cur] = jo
if left[cur] != -1:
right[left[cur]] = right[cur]
if right[cur] != -1:
left[right[cur]] = left[cur]
for j in range(k):
cur = left[cur]
if cur == -1:
break
vis[cur] = 1
join[cur] = jo
if left[cur] != -1:
right[left[cur]] = right[cur]
if right[cur] != -1:
left[right[cur]] = left[cur]
cur = A[i][1]
for j in range(k):
cur = right[cur]
if cur == -1:
break
vis[cur] = 1
join[cur] = jo
if left[cur] != -1:
right[left[cur]] = right[cur]
if right[cur] != -1:
left[right[cur]] = left[cur]
jo = 1 if jo == 2 else 2
print(*join, sep="") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | inf = 10**6
n, k = map(int, input().split())
a = [-inf] + list(map(int, input().split()))
ans = [-1] * (n + 10)
leftnext = [-1] * (n + 10)
rightnext = [-1] * (n + 10)
for i in range(1, n + 1):
rightnext[i] = i + 1
leftnext[i] = i - 1
visited = [False] * (n + 10)
stack = [(a[i], i) for i in range(1, n + 1)]
stack.sort()
phase = 0
loop = 0
while stack:
loop += 1
while stack and visited[stack[-1][1]] == True:
stack.pop()
if not stack:
continue
value, index = stack.pop()
ans[index] = phase + 1
rightcnt, leftcnt = 0, 0
lend, rend = -1, -1
now = index
while rightcnt <= k and now <= n:
rightcnt += 1
now = rightnext[now]
if rightcnt <= k:
visited[now] = True
ans[now] = phase + 1
rend = now
now = index
while leftcnt <= k and now >= 1:
leftcnt += 1
now = leftnext[now]
if leftcnt <= k:
visited[now] = True
ans[now] = phase + 1
lend = now
leftnext[rend] = lend
rightnext[lend] = rend
phase += 1
phase %= 2
print("".join(map(str, ans[1 : n + 1]))) | ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER WHILE VAR VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | n, k = map(int, input().split())
list1 = list(map(int, input().split()))
dict1 = {i: a for a, i in enumerate(list1)}
right = {i: (i + 1) for i in range(n)}
left = {i: (i - 1) for i in range(n)}
ans = [0] * n
t = 1
for i in range(n, 0, -1):
idx = dict1.get(i)
if ans[idx] == 0:
ans[idx] = t
r, l = right[idx], left[idx]
for p in range(k):
if l == -1:
break
ans[l] = t
l = left[l]
for p in range(k):
if r >= n:
break
ans[r] = t
r = right[r]
if l >= 0:
right[l] = r
if r < n:
left[r] = l
t = 3 - t
print("".join(list(map(str, ans)))) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | n, k = map(int, input().split())
students = list(map(int, input().split()))
order = sorted(range(n), key=lambda x: -students[x])
neighbours = [[t - 1, t + 1] for t in range(n)]
neighbours[-1][1] = -1
turn = 0
team = [0] * n
for i in range(n):
if team[order[i]] == 0:
team[order[i]] = turn % 2 + 1
left, right = neighbours[order[i]]
for _ in range(k):
if left != -1:
team[left] = turn % 2 + 1
neighbours[order[i]][0] = neighbours[left][0]
if neighbours[left][0] != -1:
neighbours[neighbours[left][0]][1] = order[i]
left = neighbours[left][0]
if right != -1:
team[right] = turn % 2 + 1
neighbours[order[i]][1] = neighbours[right][1]
if neighbours[right][1] != -1:
neighbours[neighbours[right][1]][0] = order[i]
right = neighbours[right][1]
left, right = neighbours[order[i]]
if left != -1:
neighbours[left][1] = right
if right != -1:
neighbours[right][0] = left
turn += 1
print(*team, sep="") | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | n, k = map(int, input().split())
skill = list(map(int, input().split()))
who = 1
stu = [1] * (n + 1)
lstu = [0] * (n + 1)
rstu = [0] * (n + 1)
take = [0] * n
stu_pos = {}
for i in range(n):
stu_pos[skill[i]] = i
for i in skill:
if stu_pos[i] == 0:
lstu[i] = -1
else:
lstu[i] = skill[stu_pos[i] - 1]
if stu_pos[i] == n - 1:
rstu[i] = -1
else:
rstu[i] = skill[stu_pos[i] + 1]
for i in range(n):
key = n - i
if stu[key] == 0:
continue
pick = []
pick.append(key)
stu[key] = 0
pos = key
for _ in range(k):
pos = lstu[pos]
if pos == -1:
break
pick.append(pos)
stu[pos] = 0
if pos != -1:
pos = lstu[pos]
lpos = pos
pos = key
for _ in range(k):
pos = rstu[pos]
if pos == -1:
break
pick.append(pos)
stu[pos] = 0
if pos != -1:
pos = rstu[pos]
rpos = pos
lstu[rpos] = lpos
rstu[lpos] = rpos
for j in pick:
if who % 2 == 1:
take[stu_pos[j]] = 1
if who % 2 == 0:
take[stu_pos[j]] = 2
who += 1
s = ""
for i in take:
s += str(i)
print(s) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | def input_ints():
return [int(x) for x in input().split()]
class Node:
def __init__(self, val):
self.prev = None
self.next = None
self.val = val
self.team = None
_, k = input_ints()
nodes = [Node(x) for x in input_ints()]
order = [i for i, x in sorted(enumerate(nodes), key=lambda x: x[1].val, reverse=True)]
for a, b in zip(nodes, nodes[1:]):
a.next = b
b.prev = a
team = 1
for i in order:
node = nodes[i]
if node.team:
continue
left = node
for _ in range(k + 1):
if left:
left.team = team
left = left.prev
else:
break
right = node
for _ in range(k + 1):
if right:
right.team = team
right = right.next
else:
break
if left:
left.next = right
if right:
right.prev = left
team = (team == 1) + 1
print("".join(str(x.team) for x in nodes)) | FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR IF VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | n, k = (int(i) for i in input().split())
j = 0
ratings = [
[pos, rat, 1, 1] for pos, rat in enumerate([int(i) for i in input().split()])
]
sorted_by_rating = sorted(ratings, key=lambda r: r[1], reverse=True)
final = [(0) for i in range(n)]
t = 0
for i in range(n):
index = sorted_by_rating[i][0]
if final[index] == 0:
t = 1 + t % 2
final[index] = t
left_x = 0
right_x = n - 1
num = k
x = index - 1
while x >= 0 and num != 0:
if final[x] == 0:
final[x] = t
left_x = x
x -= ratings[x][2]
num -= 1
else:
x -= ratings[x][2]
num = k
x = index + 1
while x < n and num != 0:
if final[x] == 0:
final[x] = t
right_x = x
x += ratings[x][3]
num -= 1
else:
x += ratings[x][3]
ratings[left_x][3] = right_x - left_x
ratings[right_x][2] = right_x - left_x
for i in final:
print(i, end="") | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | n, k = [int(x) for x in input().split(" ")]
skills = [int(x) for x in input().split(" ")]
skills_sorted = sorted(skills)
skills_sorted.reverse()
dict_skills = {}
left = []
right = []
for idx in range(len(skills)):
dict_skills[skills[idx]] = [idx, False]
left.append(idx - 1)
right.append(idx + 1)
right[-1] = -1
unselected = len(skills)
pointer_max = 0
turn = "2"
res = ""
while unselected > 0:
if turn == "2":
turn = "1"
else:
turn = "2"
while pointer_max < len(skills):
max_num = skills_sorted[pointer_max]
if dict_skills[max_num][1] != False:
pointer_max += 1
max_num = skills_sorted[pointer_max]
else:
pointer_max += 1
max_num_idx = dict_skills[max_num][0]
dict_skills[max_num][1] = turn
unselected -= 1
break
if unselected == 0:
break
left_idx = max_num_idx
right_idx = max_num_idx
for i in range(k):
left_idx = left[left_idx]
if left_idx != -1:
dict_skills[skills[left_idx]][1] = turn
unselected -= 1
if unselected == 0:
break
else:
break
for i in range(k):
right_idx = right[right_idx]
if right_idx != -1:
dict_skills[skills[right_idx]][1] = turn
unselected -= 1
if unselected == 0:
break
else:
break
if right_idx != -1:
right_idx = right[right_idx]
if left_idx != -1:
left_idx = left[left_idx]
if right_idx != -1:
left[right_idx] = left_idx
if left_idx != -1:
right[left_idx] = right_idx
_str = ""
for num in skills:
_str += dict_skills[num][1]
print(_str) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR LIST VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING WHILE VAR NUMBER IF VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | n, k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
team = [0] * n
nxt = [(x + 1) for x in range(len(a))]
nxt[-1] = n + 2 * k
prv = [(x - 1) for x in range(len(a))]
prv[0] = -2 * k
loc = [0] * (len(a) + 1)
for i, v in enumerate(a):
loc[v] = i
left = n
val = 1
while left > 0:
start = loc[left]
left = left - 1
if team[start] > 0:
continue
team[start] = val
nx = nxt[start]
pv = prv[start]
if pv >= 0:
nxt[pv] = nx
if nx < n:
prv[nx] = pv
ctdown = k
while nx < n and ctdown > 0:
team[nx] = val
if prv[nx] >= 0:
nxt[prv[nx]] = nxt[nx]
if nxt[nx] < n:
prv[nxt[nx]] = prv[nx]
nx = nxt[nx]
ctdown = ctdown - 1
ctdown = k
while pv >= 0 and ctdown > 0:
team[pv] = val
if prv[pv] >= 0:
nxt[prv[pv]] = nxt[pv]
if nxt[pv] < n:
prv[nxt[pv]] = prv[pv]
pv = prv[pv]
ctdown = ctdown - 1
val = 3 - val
print("".join(str(x) for x in team)) | 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 NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | n, k = map(int, input().split())
ls = list(map(int, input().split()))
res = {}
avail = {}
left = {}
right = {}
left[0] = -1
for i in range(1, n):
left[i] = i - 1
right[n - 1] = -1
for i in range(n - 1):
right[i] = i + 1
for i in range(n):
avail[i] = True
ls = [(ls[i], i) for i in range(n)]
ls.sort(key=lambda x: x[0])
ls.reverse()
turn = 1
for i in range(n):
if avail[ls[i][1]] == True:
avail[ls[i][1]] = False
cur = ls[i][1]
res[cur] = turn
if left[cur] != -1:
right[left[cur]] = right[cur]
if right[cur] != -1:
left[right[cur]] = left[cur]
for _ in range(k):
cur = right[cur]
if cur == -1:
break
avail[cur] = False
res[cur] = turn
if left[cur] != -1:
right[left[cur]] = right[cur]
if right[cur] != -1:
left[right[cur]] = left[cur]
cur = ls[i][1]
for _ in range(k):
cur = left[cur]
if cur == -1:
break
avail[cur] = False
res[cur] = turn
if left[cur] != -1:
right[left[cur]] = right[cur]
if right[cur] != -1:
left[right[cur]] = left[cur]
if turn == 1:
turn = 2
else:
turn = 1
for i in range(n):
print(res[i], end="") | 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 DICT ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | n, k = map(int, input().split())
a = list(map(int, input().split()))
pos = {}
pre = [(i - 1) for i in range(n + 2)]
nex = [(i + 1) for i in range(n + 2)]
for i, j in enumerate(a):
pos[j] = i + 1
group = 1
taken = [(0) for _ in range(n + 2)]
right = left = 0
for i in range(n, 0, -1):
if taken[pos[i]] != 0:
continue
group = (group + 1) % 2
p = k
now = pos[i]
taken[now] = group + 1
while p > 0 and now <= n:
now = nex[now]
if taken[now] == 0:
taken[now] = group + 1
p -= 1
right = now
p = k
now = pos[i]
while p > 0 and now > 0:
now = pre[now]
if taken[now] == 0:
taken[now] = group + 1
p -= 1
left = now
nex[left] = right
pre[right] = left
ans = [str(i) for i in taken]
print("".join(ans[1 : n + 1])) | 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 DICT ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER BIN_OP VAR NUMBER |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | n, k = map(int, input().split())
arr = [int(i) for i in input().split()]
d = {arr[i]: i for i in range(n)}
cur_max = n
ans = [(0) for i in range(n)]
turn = 1
pre, nxt = [(i - 1) for i in range(n)], [(i + 1) for i in range(n)]
for i in range(n, 0, -1):
if i not in d:
continue
left = d[i]
right = d[i]
ans[d[i]] = turn
cur = k
while left >= 0 and cur > 0:
if pre[left] < 0:
break
ans[pre[left]] = turn
del d[arr[pre[left]]]
left = pre[left]
cur -= 1
cur = k
while right < n and cur > 0:
if nxt[right] >= n:
break
ans[nxt[right]] = turn
del d[arr[nxt[right]]]
right = nxt[right]
cur -= 1
if pre[left] >= 0:
nxt[pre[left]] = nxt[right]
if nxt[right] < n:
pre[nxt[right]] = pre[left]
turn = 1 if turn == 2 else 2
print("".join(str(i) for i in ans)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | n, k = map(int, input().split())
a = list(map(int, input().split()))
connect_node = [[-1, -1] for x in range(n)]
mark = [(-1) for _ in range(n)]
completed = [(0) for _ in range(n)]
done = [(0) for _ in range(n)]
for i in range(n):
mark[a[i] - 1] = i
if i < n - 1:
connect_node[i][1] = i + 1
if i > 0:
connect_node[i][0] = i - 1
total = 0
idex = n - 1
team = "1"
while total < n:
if completed[idex] == 0:
found = mark[idex]
else:
while completed[idex] != 0:
idex -= 1
found = mark[idex]
completed[idex] = 1
total += 1
done[found] = team
count = k
right = connect_node[found][1]
while right != -1 and count and connect_node[right] != [-1, -1]:
num = a[right]
completed[num - 1] = 1
done[right] = team
total += 1
count -= 1
temp = right
right = connect_node[right][1]
connect_node[temp] = [-1, -1]
count = k
left = connect_node[found][0]
while left != -1 and count and connect_node[left] != [-1, -1]:
num = a[left]
completed[num - 1] = 1
done[left] = team
total += 1
count -= 1
temp = left
left = connect_node[left][0]
connect_node[temp] = [-1, -1]
connect_node[found] = [-1, -1]
if left != -1 and right != -1:
connect_node[left][1] = right
connect_node[right][0] = left
else:
connect_node[right][0] = -1
connect_node[left][1] = -1
if team == "1":
team = "2"
else:
team = "1"
print("".join(done)) | 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 NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR LIST NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR LIST NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR LIST NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR LIST NUMBER NUMBER ASSIGN VAR VAR LIST NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | n, k = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [0] * n
s = sorted(enumerate(a), key=lambda x: x[1], reverse=True)
p = 1
first = 0
last = n - 1
spaces = []
sn = 10
ssn = 100
fl = True
for i in s:
j = i[0]
if b[j] > 0:
continue
jk = k + 1
g = 0
while j >= first and jk > 0:
if b[j] == 0:
b[j] = p
jk -= 1
else:
g += 1
if fl and g > sn:
g = 0
fl = False
for sp in spaces[::-1]:
if sp[0] < j < sp[1]:
j = sp[0]
fl = True
break
j -= 1
if jk > 0:
first = i[0] + k
jf = j
j = i[0]
jk = k
g = 0
while j <= last and jk > 0:
if b[j] == 0:
b[j] = p
jk -= 1
else:
g += 1
if fl and g > sn:
g = 0
fl = False
for sp in spaces[::-1]:
if sp[0] < j < sp[1]:
j = sp[1]
fl = True
break
j += 1
if jk > 0:
last = i[0] - k
jl = j
if jl - jf > ssn:
spaces = [x for x in spaces if not jf <= x[0] <= x[1] <= jl]
spaces.append((jf + 1, jl - 1))
fl = True
p = 1 if p == 2 else 2
print(*b, sep="") | 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 NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | import sys
ilist = [int(i) for i in input().split()]
n, k = ilist[0], ilist[1]
a = [int(i) for i in input().split()]
left, right = [], []
for i in range(n):
left.append(i - 1)
right.append(i + 1)
val_idx = [[a[i], i] for i in range(n)]
val_idx = sorted(val_idx, key=lambda x: x[0], reverse=True)
result = [(0) for i in range(n)]
finished = 0
team = 1
for val, idx in val_idx:
if finished == n:
break
if result[idx] != 0:
continue
result[idx] = team
finished += 1
left_k = k
most_left = left[idx]
while left_k > 0 and most_left >= 0:
result[most_left] = team
finished += 1
most_left = left[most_left]
left_k -= 1
right_k = k
most_right = right[idx]
while right_k > 0 and most_right < n:
result[most_right] = team
finished += 1
most_right = right[most_right]
right_k -= 1
if most_left >= 0:
right[most_left] = most_right
if most_right < n:
left[most_right] = most_left
if team == 1:
team = 2
else:
team = 1
print("".join([str(i) for i in result])) | IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | import sys
input = sys.stdin.readline
def main():
n, k = map(int, input().split())
A = list(map(int, input().split()))
B = []
for i, a in enumerate(A):
B.append((a, i + 1))
B.sort()
bit = [0] * (n + 1)
def bit_query(i, bit):
res = 0
while i > 0:
res += bit[i]
i -= i & -i
return res
def bit_update(i, x, bit, n):
while i <= n:
bit[i] += x
i += i & -i
def lower_bound(x, bit, n):
s = 0
pos = 0
depth = n.bit_length()
v = 1 << depth
for i in range(depth, -1, -1):
k = pos + v
if k <= n and s + bit[k] < x:
s += bit[k]
pos += v
v >>= 1
return pos + 1
for i in range(1, n + 1):
bit_update(i, 1, bit, n)
ans = [0] * n
who = 0
while bit_query(n, bit):
while B:
a, i = B.pop()
if bit_query(i, bit) - bit_query(i - 1, bit) == 0:
continue
else:
break
temp = []
pos = bit_query(i, bit)
temp.append(i)
for d in range(1, k + 1):
j = pos - d
if j == 0:
break
else:
p = lower_bound(j, bit, n)
temp.append(p)
for d in range(1, k + 1):
j = pos + d
if j > bit_query(n, bit):
break
else:
p = lower_bound(j, bit, n)
temp.append(p)
for p in temp:
bit_update(p, -1, bit, n)
ans[p - 1] = str(1 + who)
who = 1 - who
print("".join(ans))
main() | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | n, k = list(map(int, input().split()))
row = list(map(int, input().split()))
ranked = list([x[1] for x in sorted([(-x[1], x[0]) for x in enumerate(row)])])
prev = list(range(-1, n - 1))
nxt = list(range(1, n + 1))
teams = [0] * n
current = 1
total = 0
for i in ranked:
if total > n:
break
if not teams[i]:
teams[i] = current
took = 1
count = k
left = prev[i]
while count > 0 and left != -1:
teams[left] = current
left = prev[left]
took += 1
count -= 1
count = k
right = nxt[i]
while count > 0 and right != n:
teams[right] = current
right = nxt[right]
took += 1
count -= 1
if left != -1:
nxt[left] = right
if right != n:
prev[right] = left
total += took
current = 2 if current == 1 else 1
print("".join(map(str, teams))) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | n, k = map(int, input().split())
a = list(map(int, input().split()))
def mysolution(n, k, a):
pre = {}
nex = {}
d = {}
for i in range(n):
pre[i] = i - 1
nex[i] = i + 1
d[a[i]] = i
a.sort(reverse=True)
res = [0] * n
turn = 1
for x in a:
index = d[x]
if res[index]:
continue
res[index] = turn
left = right = index
for i in range(k):
left = pre[left]
if left <= -1:
break
res[left] = turn
for i in range(k):
right = nex[right]
if right >= n:
break
res[right] = turn
if left >= 0:
left = pre[left]
if right < n:
right = nex[right]
nex[left] = right
pre[right] = left
turn = 3 - turn
return "".join(map(str, res))
print(mysolution(n, k, 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 FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | def turn(maxgen, V, A, k, val):
max_node = next(maxgen)
left_node = get_prev(max_node)
right_node = get_next(max_node)
V[remove(max_node, A)] = val
rcnt = 0
while rcnt < k and right_node is not None:
V[remove(right_node, A)] = val
right_node = get_next(right_node)
rcnt += 1
lcnt = 0
while lcnt < k and left_node is not None:
V[remove(left_node, A)] = val
left_node = get_prev(left_node)
lcnt += 1
return lcnt + rcnt + 1
def get_next(node):
return node[1][1]
def get_prev(node):
return node[1][0]
def pushback(value, prev_node):
next_node = None
new_node = [value, [prev_node, next_node]]
if prev_node[1][1] is None:
prev_node[1][1] = new_node
else:
raise Exception
return new_node
def remove(node, A):
(node_value, node_index), (node_prev, node_next) = node
if node_prev is not None:
node_prev[1][1] = node_next
if node_next is not None:
node_next[1][0] = node_prev
A[node_value - 1] = None
return node_index
def parseraw(raw, A):
head = [None, [None, None]]
temp = ""
sep = " "
ind = 0
lasti = len(raw) - 1
for i, v in enumerate(raw):
if v == sep or i == lasti:
if i == lasti:
temp += v
j = int(temp)
if head[0] is None:
head[0] = j, ind
last_node = head
else:
last_node = pushback((j, ind), last_node)
A[j - 1] = last_node
ind += 1
temp = ""
else:
temp += v
def max_gen(A):
for i in reversed(A):
if i is not None:
yield i
n, k = map(int, input().split())
raw = input()
A = [None] * n
V = [None] * n
parseraw(raw, A)
maxgen = max_gen(A)
first = -1
while n:
val = 1 if first else 2
n -= turn(maxgen, V, A, k, val)
first = ~first
print(*V, sep="") | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF RETURN VAR NUMBER NUMBER FUNC_DEF RETURN VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NONE ASSIGN VAR LIST VAR LIST VAR VAR IF VAR NUMBER NUMBER NONE ASSIGN VAR NUMBER NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR IF VAR NONE ASSIGN VAR NUMBER NUMBER VAR IF VAR NONE ASSIGN VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NONE RETURN VAR FUNC_DEF ASSIGN VAR LIST NONE LIST NONE NONE ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER NONE ASSIGN VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR STRING VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF VAR NONE EXPR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR STRING |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | import sys
input = sys.stdin.readline
n, k = map(int, input().split())
left = [(i - 1) for i in range(n)]
right = [(i + 1) for i in range(n)]
ans = [0] * n
def next(i):
upd = [i]
i = right[i]
while i < n and ans[i] != 0:
upd.append(i)
i = right[i]
for x in upd:
right[x] = i
return i
def prev(i):
upd = [i]
i = left[i]
while i >= 0 and ans[i] != 0:
upd.append(i)
i = left[i]
for x in upd:
left[x] = i
return i
a = list(map(int, input().split()))
pos = [0] * (n + 1)
for i in range(n):
pos[a[i]] = i
team = 1
for x in range(n, 0, -1):
i = pos[x]
if ans[i] != 0:
continue
ans[i] = team
j = i
for _ in range(k):
j = next(j)
if j >= n:
break
ans[j] = team
j = i
for _ in range(k):
j = prev(j)
if j < 0:
break
ans[j] = team
team = 3 - team
print("".join(map(str, ans))) | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | n, k = map(int, input().split())
bin_ = list(map(int, input().split()))
sorted_bin = []
command = [0] * n
next_ = [i for i in range(1, n)] + [-1]
prev_ = [-1] + [i for i in range(0, n - 1)]
for elem in enumerate(bin_):
sorted_bin.append(elem)
sorted_bin.sort(key=lambda x: -x[1])
flag = 0
for ind, value in sorted_bin:
if command[ind] != 0:
continue
command[ind] = flag + 1
next_i = ind
for _ in range(k):
next_i = next_[next_i]
if next_i != -1:
command[next_i] = 1 + flag
else:
break
prev_i = ind
for _ in range(k):
prev_i = prev_[prev_i]
if prev_i != -1:
command[prev_i] = 1 + flag
else:
break
if prev_i != -1:
prev_i = prev_[prev_i]
if next_i != -1:
next_i = next_[next_i]
if prev_i != -1:
next_[prev_i] = next_i
if next_i != -1:
prev_[next_i] = prev_i
flag = 1 - flag
for i in command:
print(i, end="") | 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 ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR NUMBER VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | IN = input
rint = lambda: int(IN())
rmint = lambda: map(int, IN().split())
rlist = lambda: list(rmint())
n, k = rmint()
pr = [i for i in range(-1, n - 1)]
nx = [i for i in range(+1, n + 1)]
ans = [0] * n
p = [0] * n
i = 0
for g in rlist():
p[n - (g - 1) - 1] = i
i += 1
def dl(x, t):
ans[x] = t
if nx[x] < n:
pr[nx[x]] = pr[x]
if pr[x] >= 0:
nx[pr[x]] = nx[x]
t = 1
for c in p:
if ans[c]:
continue
dl(c, t)
j = pr[c]
for i in range(k):
if j < 0:
break
dl(j, t)
j = pr[j]
j = nx[c]
for i in range(k):
if j >= n:
break
dl(j, t)
j = nx[j]
t = 3 - t
for o in ans:
print(o, end="") | ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | n, k = [int(x) for x in input().split()]
l1 = [(int(x) - 1) for x in input().split()]
l2 = [(0) for x in range(0, n)]
l3 = [(0) for x in range(0, n)]
l4 = [[] for x in range(0, n)]
for i in range(0, n):
l4[i].append(i - 1)
l4[i].append(i + 1)
for i in range(0, n):
l3[l1[i]] = i
i = n - 1
x = 1
while i >= 0:
t = l3[i]
if l2[t] != 0:
i -= 1
continue
else:
l2[t] = x
j = 1
m = l4[t][1]
while j <= k and m < n:
if l2[m] != 0:
if l4[m][1] > n - 1:
break
m = l4[m][1]
continue
l2[m] = x
if l4[m][1] > n - 1:
break
m = l4[m][1]
j += 1
a = m
j = 1
m = l4[t][0]
while j <= k and m > -1:
if l2[m] != 0:
if l4[m][0] < 0:
break
m = l4[m][0]
continue
l2[m] = x
if l4[m][0] < 0:
break
m = l4[m][0]
j += 1
b = m
if a < n:
l4[a][0] = b
if b > -1:
l4[b][1] = a
if x == 1:
x = 2
else:
x = 1
i -= 1
for element in l2:
print(element, end="") | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | import sys
n, k = list(map(int, input().split()))
l = list(map(int, input().split()))
for i in range(n):
l[i] -= 1
taken = [0] * n
place = [0] * n
nast = [(i + 1) for i in range(n)]
poprz = [(i - 1) for i in range(n)]
for i in range(n):
place[l[i]] = i
best = n - 1
turn = 2
while True:
turn = 3 - turn
p = place[best]
pp = p
taken[p] = turn
for i in range(k):
if nast[pp] < n:
pp = nast[pp]
taken[pp] = turn
else:
pp = n
break
if pp < n:
right = nast[pp]
else:
right = n
pp = p
for i in range(k):
if poprz[pp] >= 0:
pp = poprz[pp]
taken[pp] = turn
else:
pp = -1
break
if pp >= 0:
left = poprz[pp]
else:
left = -1
if left > -1:
nast[left] = right
if right < n:
poprz[right] = left
while True:
if best < 0:
for f in taken:
print(f, end="")
return
if taken[place[best]] != 0:
best -= 1
else:
break | IMPORT 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 FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR WHILE NUMBER IF VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING RETURN IF VAR VAR VAR NUMBER VAR NUMBER |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | import sys
def input():
return sys.stdin.readline().rstrip()
def slv():
n, k = map(int, input().split())
s = list(map(int, input().split()))
ans = [0] * n
v = [(s[i], i) for i in range(n)]
v.sort(key=lambda x: x[0])
leftperson = [(i - 1) for i in range(n)]
rightperson = [(i + 1) for i in range(n)]
seen = [False] * n
sign = 0
while v:
skill_val, idx = v.pop()
if seen[idx] == True:
continue
seen[idx] = True
if sign == 0:
ans[idx] = sign + 1
else:
ans[idx] = sign + 1
left_addition = 0
p = idx
while left_addition < k and leftperson[p] >= 0:
p = leftperson[p]
left_addition += 1
seen[p] = True
ans[p] = sign + 1
L = leftperson[p]
right_addition = 0
p = idx
while right_addition < k and rightperson[p] < n:
p = rightperson[p]
right_addition += 1
seen[p] = True
ans[p] = sign + 1
R = rightperson[p]
if L >= 0:
rightperson[L] = R
if R < n:
leftperson[R] = L
sign ^= 1
res = "".join(map(lambda x: str(x), ans))
print(res)
return
def main():
t = 1
for i in range(t):
slv()
return
main() | IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN EXPR FUNC_CALL VAR |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | N, K = list(map(int, input().split()))
A = [(int(a) - 1) for a in input().split()]
T = [0] * N
inv = [0] * N
for i in range(N):
inv[A[i]] = i
L = [(a - 1) for a in range(N)]
R = [(a + 1) for a in range(N)]
c = 0
t = 2
i = N - 1
while c < N and i >= 0:
j = inv[i]
if T[j]:
i -= 1
continue
t = 3 - t
T[j] = t
c += 1
k = K
pj = j
while True:
nj = L[pj]
if k == 0 or nj < 0:
break
T[nj] = t
c += 1
k -= 1
pj = nj
ll = nj
k = K
pj = j
while True:
nj = R[pj]
if k == 0 or nj >= N:
break
T[nj] = t
c += 1
k -= 1
pj = nj
rr = nj
if rr < N:
L[rr] = ll
if ll >= 0:
R[ll] = rr
i -= 1
print("".join(map(str, T))) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | import sys
input = sys.stdin.readline
n, k = list(map(int, input().split()))
A = list(map(int, input().split()))
A_INV = [-1] * n
for i in range(n):
A_INV[A[i] - 1] = i
L = list(range(-1, n - 1))
R = list(range(1, n + 1))
USELIST = [0] * n
ANS = [0] * n
NOW = 1
for a in A_INV[::-1]:
if USELIST[a] == 1:
continue
USELIST[a] = 1
ANS[a] = NOW
if 0 <= a < n and 0 <= L[a] < n:
R[L[a]] = R[a]
if 0 <= a < n and 0 <= R[a] < n:
L[R[a]] = L[a]
r = a
for step in range(k):
r = R[r]
if r < 0 or r >= n:
break
ANS[r] = NOW
USELIST[r] = 1
if 0 <= r < n and 0 <= L[r] < n:
R[L[r]] = R[r]
if 0 <= r < n and 0 <= R[r] < n:
L[R[r]] = L[r]
l = a
for step in range(k):
l = L[l]
if l < 0 or l >= n:
break
ANS[l] = NOW
USELIST[l] = 1
if 0 <= l < n and 0 <= L[l] < n:
R[L[l]] = R[l]
if 0 <= l < n and 0 <= R[l] < n:
L[R[l]] = L[l]
NOW = 3 - NOW
print("".join(map(str, ANS))) | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER IF NUMBER VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER IF NUMBER VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF NUMBER VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | n, k = list(map(int, input().split()))
sk = list(map(int, input().split()))
new_sk = [(j, i) for i, j in enumerate(sk)]
new_sk = sorted(new_sk, key=lambda x: x[0], reverse=True)
prevs = [-1] * n
nexts = [-1] * n
for i in range(n):
if i < n - 1:
nexts[i] = i + 1
if i > 0:
prevs[i] = i - 1
maxi = new_sk[-1]
used = set()
i = 0
t = -1
flag = False
while i <= n:
while new_sk[i][0] in used:
i += 1
if i >= n:
flag = True
break
if flag:
break
idx = new_sk[i][1]
maxi = new_sk[i][0]
used.add(maxi)
sk[idx] = t
l = 0
l_idx = prevs[idx]
while l < k and l_idx != -1:
used.add(sk[l_idx])
sk[l_idx] = t
l_idx = prevs[l_idx]
l += 1
prevs[idx] = l_idx
r = 0
r_idx = nexts[idx]
while r < k and r_idx != -1:
used.add(sk[r_idx])
sk[r_idx] = t
r_idx = nexts[r_idx]
r += 1
nexts[idx] = r_idx
if prevs[idx] != -1:
nexts[prevs[idx]] = nexts[idx]
if nexts[idx] != -1:
prevs[nexts[idx]] = prevs[idx]
t = -1 if t == -2 else -2
for i in sk:
if i == -1:
print(1, end="")
else:
print(2, end="") | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | def delete(i, grp):
l = left[i]
r = right[i]
if l != -1:
right[l] = r
if r != n:
left[r] = l
used[i] = grp
n, k = map(int, input().split())
arr = list(map(int, input().split()))
left = [-1] * n
right = [n] * n
used = [-1] * n
for i in range(n):
left[i] = i - 1
right[i] = i + 1
order = sorted(range(n), key=lambda i: arr[i], reverse=True)
grp = 1
for i in order:
if used[i] != -1:
continue
l = left[i]
r = right[i]
delete(i, grp)
cnt = 0
while l != -1 and cnt < k:
cnt += 1
temp = left[l]
delete(l, grp)
l = temp
cnt = 0
while r != n and cnt < k:
cnt += 1
temp = right[r]
delete(r, grp)
r = temp
if grp == 1:
grp = 2
else:
grp = 1
print(*used, sep="") | FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | n, k = map(int, input().split())
arr = list(map(int, input().split()))
indices = range(n)
indices = sorted(indices, key=lambda x: -arr[x])
team_num = [0] * n
list_nxt = list(range(1, n + 1))
list_nxt[-1] = -1
list_prev = list(range(-1, n - 1))
cur_team = 1
for ind in indices:
if team_num[ind] != 0:
continue
left_neigh, right_neigh = ind, ind
for i in range(k + 1):
team_num[right_neigh], right_neigh = cur_team, list_nxt[right_neigh]
if right_neigh == -1:
break
for i in range(k + 1):
team_num[left_neigh], left_neigh = cur_team, list_prev[left_neigh]
if left_neigh == -1:
break
if left_neigh != -1:
list_nxt[left_neigh] = right_neigh
if right_neigh != -1:
list_prev[right_neigh] = left_neigh
cur_team = 3 - cur_team
print("".join(list(map(str, team_num)))) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | class Node:
def __init__(self, v, i):
self.v = v
self.i = i
self.left = None
self.right = None
n, k = map(int, input().split())
nums = list(map(int, input().split()))
nodes = [Node(v, i) for i, v in enumerate(nums)]
sort = [[v, i] for i, v in enumerate(nums)]
sort = sorted(sort, key=lambda x: [x[0], x[1]], reverse=True)
for i in range(len(nodes)):
if i < len(nodes) - 1:
nodes[i].right = nodes[i + 1]
if i > 0:
nodes[i].left = nodes[i - 1]
ans = [0] * n
team = 1
for pair in sort:
v, i = pair
if nodes[i].i == None:
continue
ans[i] = team if team == 1 else 2
cur = nodes[i].right
right = None
for j in range(k):
if cur == None:
break
right = cur.right
ans[cur.i] = team if team == 1 else 2
cur.i = None
cur = right
cur = nodes[i].left
left = None
for j in range(k):
if cur == None:
break
left = cur.left
ans[cur.i] = team if team == 1 else 2
cur.i = None
cur = left
if left:
left.right = right
if right:
right.left = left
team = team ^ 1
print("".join([str(i) for i in ans])) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR LIST VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NONE ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NONE ASSIGN VAR VAR IF VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | n, k = map(int, input().split())
lst = map(int, input().split())
def debug():
print("-")
lst = [(0) for _ in range(n)]
for i in range(n):
node = table[i]
lst[node.index] = node.team
print("".join(str(x) for x in lst))
print("-")
class Node:
def __init__(self, index):
self.index = index
self.team = 0
self.prev = None
self.next = None
table = [None for _ in range(n)]
prev_node = None
for index, element in enumerate(lst):
node = Node(index)
if prev_node:
prev_node.next = node
node.prev = prev_node
table[element - 1] = node
prev_node = node
team = 1
for i in reversed(range(n)):
if table[i].team != 0:
continue
node = table[i]
node.team = team
next_node = node.next
for j in range(k):
if next_node is None:
break
next_node.team = team
next_node = next_node.next
prev_node = node.prev
for j in range(k):
if prev_node is None:
break
prev_node.team = team
prev_node = prev_node.prev
if prev_node is not None:
prev_node.next = next_node
if next_node is not None:
next_node.prev = prev_node
team = 1 if team == 2 else 2
lst = [(0) for _ in range(n)]
for i in range(n):
node = table[i]
lst[node.index] = node.team
print("".join(str(x) for x in lst)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE VAR FUNC_CALL VAR VAR ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | import sys
n, k = map(int, input().split())
numbers = list(map(int, input().split()))
teams = [(0) for i in range(len(numbers))]
sys.setrecursionlimit(5000)
class Value:
def __init__(self, skill, index, previousVal=None, nextVal=None):
self.skill = skill
self.index = index
self.previousVal = previousVal
self.nextVal = nextVal
def __gt__(self, other):
return self.skill > other.skill
def __repr__(self):
return str(self)
def __str__(self):
return "(skill: " + str(self.skill) + ",index: " + str(self.index) + ")"
def addPrevious(value, team, k):
myValues.pop(value.index)
teams[value.index] = team
if value.nextVal:
value.nextVal.previousVal = value.previousVal
if value.previousVal:
value.previousVal.nextVal = value.nextVal
if k > 0 and value.previousVal:
addPrevious(value.previousVal, team, k - 1)
def addNext(value, team, k):
myValues.pop(value.index)
teams[value.index] = team
if value.nextVal:
value.nextVal.previousVal = value.previousVal
if value.previousVal:
value.previousVal.nextVal = value.nextVal
if k > 0 and value.nextVal:
addNext(value.nextVal, team, k - 1)
def addToTeam(value, team, k):
myValues.pop(value.index)
teams[value.index] = team
if value.nextVal:
value.nextVal.previousVal = value.previousVal
if value.previousVal:
value.previousVal.nextVal = value.nextVal
if k > 0:
if value.nextVal:
addNext(value.nextVal, team, k - 1)
if value.previousVal:
addPrevious(value.previousVal, team, k - 1)
def getValue(someObject):
return someObject[1].skill
if k < 2000:
myValues = {}
currValue = Value(numbers[0], 0)
myValues[0] = currValue
for i in range(1, len(numbers)):
newValue = Value(numbers[i], i, currValue)
myValues[i] = newValue
currValue = newValue
while True:
if currValue.previousVal:
currValue.previousVal.nextVal = currValue
currValue = currValue.previousVal
else:
break
currTeam = 1
sortedList = sorted(myValues.items(), key=getValue, reverse=True)
for i in sortedList:
if i[0] in myValues:
currTeam = (currTeam + 1) % 2
addToTeam(i[1], currTeam + 1, k)
else:
def value(doubleValues):
return doubleValues[1]
def addToTeam(values, team):
for i in values:
teams[i[0]] = team
myValues = []
currTeam = 1
for i in range(len(numbers)):
myValues.append((i, numbers[i]))
while len(myValues) > 0:
currTeam = (currTeam + 1) % 2
maxIndex = myValues.index(max(myValues, key=value))
if maxIndex < k and maxIndex + k >= len(myValues):
addToTeam(myValues, currTeam + 1)
myValues = []
elif maxIndex < k:
addToTeam(myValues[: maxIndex + k + 1], currTeam + 1)
myValues = myValues[maxIndex + k + 1 :]
elif maxIndex + k >= len(myValues) - 1:
addToTeam(myValues[maxIndex - k :], currTeam + 1)
myValues = myValues[: maxIndex - k]
else:
addToTeam(myValues[maxIndex - k : maxIndex + k + 1], currTeam + 1)
myValues = myValues[: maxIndex - k] + myValues[maxIndex + k + 1 :]
teamString = ""
for i in teams:
teamString += str(i)
print(teamString) | IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER CLASS_DEF FUNC_DEF NONE NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF RETURN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR IF VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF RETURN VAR NUMBER IF VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_DEF RETURN VAR NUMBER FUNC_DEF FOR VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | import sys
__version__ = "1.0"
__date__ = "2019-04-17"
def remove_me(to_left, to_right, index):
left_id = to_left[index]
right_id = to_right[index]
if right_id < len(to_left):
to_left[right_id] = left_id
if left_id >= 0:
to_right[left_id] = right_id
return left_id, right_id
def join_team(n, k, students):
teams = ["0"] * n
mydict = dict(zip(students, range(n)))
to_left = [(i - 1) for i in range(n)]
to_right = [(i + 1) for i in range(n)]
next_chosen = n
chosen_total = 0
while chosen_total < n:
for couch in ["1", "2"]:
while teams[mydict[next_chosen]] != "0":
next_chosen -= 1
if next_chosen < 1:
return teams
teams[mydict[next_chosen]] = couch
chosen_total += 1
left_id, right_id = remove_me(to_left, to_right, mydict[next_chosen])
for i in range(k):
if left_id < 0:
break
teams[left_id] = couch
chosen_total += 1
left_id, right_id = remove_me(to_left, to_right, left_id)
for i in range(k):
if right_id >= n:
break
teams[right_id] = couch
chosen_total += 1
left_id, right_id = remove_me(to_left, to_right, right_id)
return teams
def main(argv=None):
n, k = map(int, input().split())
students = list(map(int, input().split()))
print("".join(join_team(n, k, students)))
return 0
STATUS = main()
sys.exit(STATUS) | IMPORT ASSIGN VAR STRING ASSIGN VAR STRING FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR FOR VAR LIST STRING STRING WHILE VAR VAR VAR STRING VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF NONE ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
There are $n$ students standing in a row. Two coaches are forming two teams — the first coach chooses the first team and the second coach chooses the second team.
The $i$-th student has integer programming skill $a_i$. All programming skills are distinct and between $1$ and $n$, inclusive.
Firstly, the first coach will choose the student with maximum programming skill among all students not taken into any team, and $k$ closest students to the left of him and $k$ closest students to the right of him (if there are less than $k$ students to the left or to the right, all of them will be chosen). All students that are chosen leave the row and join the first team. Secondly, the second coach will make the same move (but all students chosen by him join the second team). Then again the first coach will make such move, and so on. This repeats until the row becomes empty (i. e. the process ends when each student becomes to some team).
Your problem is to determine which students will be taken into the first team and which students will be taken into the second team.
-----Input-----
The first line of the input contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) — the number of students and the value determining the range of chosen students during each move, respectively.
The second line of the input contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$), where $a_i$ is the programming skill of the $i$-th student. It is guaranteed that all programming skills are distinct.
-----Output-----
Print a string of $n$ characters; $i$-th character should be 1 if $i$-th student joins the first team, or 2 otherwise.
-----Examples-----
Input
5 2
2 4 5 3 1
Output
11111
Input
5 1
2 1 3 5 4
Output
22111
Input
7 1
7 2 1 3 5 4 6
Output
1121122
Input
5 1
2 4 5 3 1
Output
21112
-----Note-----
In the first example the first coach chooses the student on a position $3$, and the row becomes empty (all students join the first team).
In the second example the first coach chooses the student on position $4$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team).
In the third example the first coach chooses the student on position $1$, and the row becomes $[1, 3, 5, 4, 6]$ (students with programming skills $[2, 7]$ join the first team). Then the second coach chooses the student on position $5$, and the row becomes $[1, 3, 5]$ (students with programming skills $[4, 6]$ join the second team). Then the first coach chooses the student on position $3$, and the row becomes $[1]$ (students with programming skills $[3, 5]$ join the first team). And then the second coach chooses the remaining student (and the student with programming skill $1$ joins the second team).
In the fourth example the first coach chooses the student on position $3$, and the row becomes $[2, 1]$ (students with programming skills $[3, 4, 5]$ join the first team). Then the second coach chooses the student on position $1$, and the row becomes empty (and students with programming skills $[1, 2]$ join the second team). | import sys
class Main:
def __init__(self):
self.buff = None
self.index = 0
def next(self):
if self.buff is None or self.index == len(self.buff):
self.buff = sys.stdin.readline().split()
self.index = 0
val = self.buff[self.index]
self.index += 1
return val
def next_int(self):
return int(self.next())
def solve(self):
n = self.next_int()
k = self.next_int()
x = [self.next_int() for _ in range(0, n)]
bf = [(x - 1) for x in range(0, n)]
nx = [(x + 1) for x in range(0, n)]
t = [(0) for _ in range(0, n)]
od = [(0) for _ in range(0, n)]
for i in range(0, n):
od[x[i] - 1] = i
tt = 1
for i in range(n - 1, -1, -1):
if t[od[i]] == 0:
o = od[i]
t[o] = tt
for _ in range(0, k):
o = bf[o]
if o == -1:
break
t[o] = tt
oo = od[i]
for _ in range(0, k):
oo = nx[oo]
if oo == n:
break
t[oo] = tt
if o != -1 and bf[o] != -1:
nx[bf[o]] = oo if oo == n else nx[oo]
if oo != n and nx[oo] != n:
bf[nx[oo]] = o if o == -1 else bf[o]
tt = 3 - tt
print("".join(map(lambda xx: str(xx), t)))
Main().solve() | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL FUNC_CALL VAR |
Given a string, sort it in decreasing order based on the frequency of characters.
Example 1:
Input:
"tree"
Output:
"eert"
Explanation:
'e' appears twice while 'r' and 't' both appear once.
So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.
Example 2:
Input:
"cccaaa"
Output:
"cccaaa"
Explanation:
Both 'c' and 'a' appear three times, so "aaaccc" is also a valid answer.
Note that "cacaca" is incorrect, as the same characters must be together.
Example 3:
Input:
"Aabb"
Output:
"bbAa"
Explanation:
"bbaA" is also a valid answer, but "Aabb" is incorrect.
Note that 'A' and 'a' are treated as two different characters. | class Solution:
def frequencySort(self, s):
counter = {}
for c in s:
if c in counter:
counter[c] += 1
else:
counter[c] = 1
res = ""
while counter:
m = max(counter, key=counter.get)
res += m * counter[m]
del counter[m]
return res | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR STRING WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR |
Given a string, sort it in decreasing order based on the frequency of characters.
Example 1:
Input:
"tree"
Output:
"eert"
Explanation:
'e' appears twice while 'r' and 't' both appear once.
So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.
Example 2:
Input:
"cccaaa"
Output:
"cccaaa"
Explanation:
Both 'c' and 'a' appear three times, so "aaaccc" is also a valid answer.
Note that "cacaca" is incorrect, as the same characters must be together.
Example 3:
Input:
"Aabb"
Output:
"bbAa"
Explanation:
"bbaA" is also a valid answer, but "Aabb" is incorrect.
Note that 'A' and 'a' are treated as two different characters. | class Solution:
def frequencySort(self, s):
res = ""
cnt = collections.Counter(s)
tmps = cnt.most_common()
for tmp in tmps:
res += tmp[0] * tmp[1]
return res | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR |
Given a string, sort it in decreasing order based on the frequency of characters.
Example 1:
Input:
"tree"
Output:
"eert"
Explanation:
'e' appears twice while 'r' and 't' both appear once.
So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.
Example 2:
Input:
"cccaaa"
Output:
"cccaaa"
Explanation:
Both 'c' and 'a' appear three times, so "aaaccc" is also a valid answer.
Note that "cacaca" is incorrect, as the same characters must be together.
Example 3:
Input:
"Aabb"
Output:
"bbAa"
Explanation:
"bbaA" is also a valid answer, but "Aabb" is incorrect.
Note that 'A' and 'a' are treated as two different characters. | class Solution:
def frequencySort(self, s):
counter = collections.Counter(s)
colls = sorted(counter.items(), key=lambda k: k[1], reverse=True)
res = ""
for k, v in colls:
res += k * v
return res | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR |
Given a string, sort it in decreasing order based on the frequency of characters.
Example 1:
Input:
"tree"
Output:
"eert"
Explanation:
'e' appears twice while 'r' and 't' both appear once.
So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.
Example 2:
Input:
"cccaaa"
Output:
"cccaaa"
Explanation:
Both 'c' and 'a' appear three times, so "aaaccc" is also a valid answer.
Note that "cacaca" is incorrect, as the same characters must be together.
Example 3:
Input:
"Aabb"
Output:
"bbAa"
Explanation:
"bbaA" is also a valid answer, but "Aabb" is incorrect.
Note that 'A' and 'a' are treated as two different characters. | class Solution:
def frequencySort(self, s):
charToFreq = {}
freqToChar = {}
for c in s:
if c not in charToFreq:
charToFreq[c] = 0
charToFreq[c] += 1
for key, value in list(charToFreq.items()):
if value not in freqToChar:
freqToChar[value] = []
freqToChar[value].append(key)
result = []
for key in range(len(s), -1, -1):
if key in freqToChar:
for char in freqToChar[key]:
result += [char] * key
return "".join(result) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR FOR VAR VAR VAR VAR BIN_OP LIST VAR VAR RETURN FUNC_CALL STRING VAR |
Given a string, sort it in decreasing order based on the frequency of characters.
Example 1:
Input:
"tree"
Output:
"eert"
Explanation:
'e' appears twice while 'r' and 't' both appear once.
So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.
Example 2:
Input:
"cccaaa"
Output:
"cccaaa"
Explanation:
Both 'c' and 'a' appear three times, so "aaaccc" is also a valid answer.
Note that "cacaca" is incorrect, as the same characters must be together.
Example 3:
Input:
"Aabb"
Output:
"bbAa"
Explanation:
"bbaA" is also a valid answer, but "Aabb" is incorrect.
Note that 'A' and 'a' are treated as two different characters. | class Solution:
def frequencySort(self, s):
bucket = [None] * (len(s) + 1)
hash_table = {}
result = ""
for item in s:
hash_table[item] = hash_table.get(item, 0) + 1
for key, value in hash_table.items():
if bucket[value]:
bucket[value].append((key, value))
else:
bucket[value] = [(key, value)]
for i in range(len(bucket) - 1, -1, -1):
if bucket[i]:
for item in bucket[i]:
result += item[0] * item[1]
return result | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NONE BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR FOR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR |
Given a string, sort it in decreasing order based on the frequency of characters.
Example 1:
Input:
"tree"
Output:
"eert"
Explanation:
'e' appears twice while 'r' and 't' both appear once.
So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.
Example 2:
Input:
"cccaaa"
Output:
"cccaaa"
Explanation:
Both 'c' and 'a' appear three times, so "aaaccc" is also a valid answer.
Note that "cacaca" is incorrect, as the same characters must be together.
Example 3:
Input:
"Aabb"
Output:
"bbAa"
Explanation:
"bbaA" is also a valid answer, but "Aabb" is incorrect.
Note that 'A' and 'a' are treated as two different characters. | class Solution:
def frequencySort(self, s):
mapping = {}
for c in s:
mapping[c] = mapping[c] + 1 if c in mapping else 1
bucket = [[] for _ in range(len(s) + 1)]
for c, freq in mapping.items():
bucket[freq].append(c)
res = ""
for i in range(len(s), -1, -1):
for c in bucket[i]:
for _ in range(i):
res += c
return res | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given a string, sort it in decreasing order based on the frequency of characters.
Example 1:
Input:
"tree"
Output:
"eert"
Explanation:
'e' appears twice while 'r' and 't' both appear once.
So 'e' must appear before both 'r' and 't'. Therefore "eetr" is also a valid answer.
Example 2:
Input:
"cccaaa"
Output:
"cccaaa"
Explanation:
Both 'c' and 'a' appear three times, so "aaaccc" is also a valid answer.
Note that "cacaca" is incorrect, as the same characters must be together.
Example 3:
Input:
"Aabb"
Output:
"bbAa"
Explanation:
"bbaA" is also a valid answer, but "Aabb" is incorrect.
Note that 'A' and 'a' are treated as two different characters. | class Solution:
def frequencySort(self, s):
charToFreq = {}
freqToChar = {}
for c in s:
if c not in charToFreq:
charToFreq[c] = 0
charToFreq[c] += 1
print(charToFreq)
for key, value in list(charToFreq.items()):
if value not in freqToChar:
freqToChar[value] = []
freqToChar[value].append(key)
print(freqToChar)
result = []
for key in sorted(freqToChar, reverse=True):
for char in freqToChar[key]:
result += [char] * key
return "".join(result) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR VAR BIN_OP LIST VAR VAR RETURN FUNC_CALL STRING VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.