description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: As...
def binarySearch(arr, elem): a = 0 b = len(arr) - 1 i = -1 while a <= b: mid = (a + b) // 2 element = arr[mid] if element == elem: i = mid if element >= elem: b = mid - 1 else: a = mid + 1 return i class Solution: def...
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VA...
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: As...
class Solution: def getMinDiff(self, arr, n, k): arr.sort() ans = arr[-1] - arr[0] smallest = arr[0] + k largest = arr[-1] - k mi, ma = float("inf"), float("-inf") for i in range(n - 1): mi = min(smallest, arr[i + 1] - k) ma = max(largest, arr...
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_C...
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: As...
class Solution: def getMinDiff(self, arr, n, k): arr.sort() mn = arr[0] mx = arr[n - 1] diff = mx - mn for i in range(1, n): mn = min(arr[0] + k, arr[i] - k) mx = max(arr[-1] - k, arr[i - 1] + k) diff = min(diff, mx - mn) return di...
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL V...
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: As...
class Solution: def getMinDiff(self, arr, n, k): point = arr point.sort() currentMin = point[0] currentMax = point[-1] minSum = abs(currentMax - currentMin) for i in range(1, n): currentMin = min(point[0] + k, point[i] - k) currentMax = max(po...
CLASS_DEF FUNC_DEF ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSI...
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: As...
class Solution: def getMinDiff(self, arr, n, k): t = sorted(arr) max1 = t[n - 1] min1 = t[0] res = max1 - min1 for i in range(n): max1 = max(t[i - 1] + k, t[n - 1] - k) min1 = min(t[i] - k, t[0] + k) res = min(res, max1 - min1) ret...
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN V...
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: As...
class Solution: def getMinDiff(self, arr, n, k): arr.sort() ans = arr[n - 1] - arr[0] for i in range(1, n): ans = min( ans, max(arr[i - 1] + k, arr[n - 1] - k) - min(arr[0] + k, arr[i] - k) ) return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR RETURN VAR
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: As...
class Solution: def getMinDiff(self, arr, n, k): arr.sort() n = len(arr) var1 = max(arr) var2 = min(arr) result = var1 - var2 for i in range(1, len(arr)): maxi = max(arr[n - 1] - k, arr[i - 1] + k) mini = min(arr[0] + k, arr[i] - k) ...
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP...
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: As...
class Solution: def getMinDiff(self, arr, n, k): d = [] mapped = [(0) for i in range(n)] c = 0 for i in range(n): d.append((arr[i] + k, i)) d.append((arr[i] - k, i)) d.sort(key=lambda x: x[0]) j = 0 ans = 1e100 for i in range(n...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR BIN_OP ...
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: As...
class Solution: def getMinDiff(self, arr, n, k): if n == 1: return 0 arr.sort() prevDiff = abs(arr[0] - arr[n - 1]) for i in range(1, n): maxi = max(arr[i - 1] + k, arr[n - 1] - k) mini = min(arr[0] + k, arr[i] - k) prevDiff = min(prev...
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR...
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: As...
class Solution: def getMinDiff(self, arr, n, k): arr.sort() min_diff = arr[-1] - arr[0] for i in range(n - 1): min_tower = min(arr[0] + k, arr[i + 1] - k) max_tower = max(arr[i] + k, arr[-1] - k) diff = max_tower - min_tower min_diff = min(min...
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR V...
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: As...
class Solution: def getMinDiff(self, arr, n, k): arr.sort() ans = arr[-1] - arr[0] tmax = arr[-1] tmin = arr[0] for x in range(1, n): tmax = max(arr[-1] - k, arr[x - 1] + k) tmin = min(arr[0] + k, arr[x] - k) ans = min(ans, tmax - tmin) ...
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CAL...
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: As...
class Solution: def getMinDiff(self, arr, n, k): arr.sort() ans = arr[n - 1] - arr[0] small = arr[0] + k large = arr[n - 1] - k for i in range(n - 1): mi = min(small, arr[i + 1] - k) ma = max(large, arr[i] + k) if small < 0: ...
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF V...
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: As...
class Solution: def getMinDiff(self, arr, n, k): v = [] taken = [0] * n for i in range(n): v.append([arr[i] - k, i]) v.append([arr[i] + k, i]) v.sort() elements_in_range = 0 left = right = 0 while elements_in_range < n and right < len(...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER NUMBER VAR NUM...
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: As...
class Solution: def getMinDiff(self, arr, n, k): aux = [] for i in range(n): aux.append([arr[i] - k, i]) aux.append([arr[i] + k, i]) aux.sort(key=lambda x: x[0]) counts = [(0) for i in range(n)] partial_min = 1e100 j = 0 index_count = ...
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBE...
Given an array arr[] denoting heights of N towers and a positive integer K, you have to modify the height of each tower either by increasing or decreasing them by K only once. Find out what could be the possible minimum difference of the height of shortest and longest towers after you have modified each tower. Note: As...
class Solution: def getMinDiff(self, arr, n, k): arr.sort() i = 0 ans = 1000000000.0 while i < n: mn = min([arr[0], arr[i] - k]) if i > 0: mx = max([arr[n - 1] - k, arr[i - 1]]) else: mx = arr[n - 1] - k ...
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR NUMBER BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR LIST...
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,...
n, k = map(int, input().split()) dic = dict() for _ in range(k): u, v, d = map(int, input().split()) if u not in dic: dic[u] = -d else: dic[u] -= d if v not in dic: dic[v] = d else: dic[v] += d dic_neg = dict() dic_pos = dict() for key, value in dic.items(): if va...
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_...
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,...
from sys import stdin def iin(): return int(stdin.readline()) def lin(): return list(map(int, stdin.readline().split())) def main(): n, m = lin() dept = [0] * (n + 1) for _ in range(m): v, u, d = lin() dept[v] += d dept[u] -= d ps = [] mn = [] for i in range...
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR...
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,...
n, m = map(int, input().split()) balance = [0] * n for _ in range(m): u, v, w = map(int, input().split()) u -= 1 v -= 1 balance[u] -= w balance[v] += w i = 0 j = 0 ans = [] while i < n: if balance[i] < 0: while balance[j] <= 0: j += 1 delta = min(-balance[i], balance[...
ASSIGN VAR 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 VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR NUMBER WHILE VAR VA...
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,...
import sys zz = 1 sys.setrecursionlimit(10**5) if zz: input = sys.stdin.readline else: sys.stdin = open("input.txt", "r") sys.stdout = open("all.txt", "w") di = [[-1, 0], [1, 0], [0, 1], [0, -1]] def fori(n): return [fi() for i in range(n)] def inc(d, c, x=1): d[c] = d[c] + x if c in d else x ...
IMPORT ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF NU...
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,...
def f(): n, m = [int(s) for s in input().split()] bank = [0] * (n + 1) for i in range(m): u, v, d = [int(s) for s in input().split()] bank[u] += d bank[v] -= d borrow = [] lend = [] for i in range(1, n + 1): if bank[i] > 0: borrow.append([i, bank[i]]) ...
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR 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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR V...
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,...
import sys def main(): n, m = readIntArr() balance = [[0, 0, i] for i in range(n + 1)] for _ in range(m): u, v, d = readIntArr() balance[u][1] += d balance[v][0] += d balance.pop(0) for i in range(n): delta = min(balance[i][0], balance[i][1]) balance[i][0] -...
IMPORT FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER V...
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,...
n, m = map(int, input().split()) bal = [0] * n for i in range(m): x, y, z = map(int, input().split()) bal[x - 1] -= z bal[y - 1] += z owe = [] for i in range(n): if bal[i] < 0: owe.append([i, -bal[i]]) ans = [] for i in range(n): while bal[i] > 0 and owe: val = min(bal[i], owe[0][1])...
ASSIGN VAR 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 VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VA...
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,...
import sys input = sys.stdin.readline N, M = map(int, input().split()) A = [0] * N for _ in range(M): a, b, d = map(int, input().split()) A[a - 1] += d A[b - 1] -= d Plus = [] Minus = [] for i in range(N): if A[i] > 0: Plus.append((i, A[i])) if A[i] < 0: Minus.append((i, -A[i])) ans...
IMPORT ASSIGN VAR VAR ASSIGN VAR 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 VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR...
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,...
import sys N, M = map(int, input().split()) money = [0] * N for u, v, d in (map(int, l.split()) for l in sys.stdin): money[u - 1] -= d money[v - 1] += d plus, minus = [], [] for i, m in enumerate(money): if m > 0: plus.append([i, m]) elif m < 0: minus.append([i, -m]) ans = [] for i, m i...
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR LIST LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR IF VAR NUMBER...
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,...
import sys reader = (s.rstrip() for s in sys.stdin) input = reader.__next__ n, m = [int(x) for x in input().split()] dp = [0] * (n + 1) for i in range(m): x, y, z = [int(x) for x in input().split()] if x == y: continue dp[x] -= z dp[y] += z delta = [] for i in range(n + 1): delta.append([dp...
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR 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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_C...
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,...
import sys input = sys.stdin.readline n, m = map(int, input().split()) D = [0] * (n + 1) for i in range(m): x, y, z = map(int, input().split()) D[x] += z D[y] -= z PLUS = [] MINUS = [] for i in range(n + 1): if D[i] > 0: PLUS.append([D[i], i]) elif D[i] < 0: MINUS.append([-D[i], i])...
IMPORT ASSIGN VAR VAR ASSIGN VAR 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR...
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,...
import sys input = sys.stdin.readline n, m = map(int, input().split()) info = [list(map(int, input().split())) for i in range(m)] val = [0] * n for i in range(m): a, b, cost = info[i] a -= 1 b -= 1 val[a] -= cost val[b] += cost minus = [] plus = [] for i in range(n): if val[i] < 0: minu...
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASS...
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,...
import sys input = sys.stdin.readline l = input().split() n = int(l[0]) m = int(l[1]) owe = [(0) for i in range(n)] for i in range(m): l = input().split() u = int(l[0]) v = int(l[1]) d = int(l[2]) u -= 1 v -= 1 owe[u] += d owe[v] -= d owers = [] loan = [] for i in range(n): if owe[i...
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CA...
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,...
n, m = map(int, input().split()) A = [0] * (n + 1) P = [] N = [] for i in range(m): u, v, d = map(int, input().split()) A[u] -= d A[v] += d for i in range(1, n + 1): if A[i] > 0: P.append([A[i], i]) if A[i] < 0: N.append([A[i], i]) i = j = 0 R = [] while i < len(P): r = min(P[i][...
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FU...
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,...
n, m = map(int, input().split()) lst = [0] * (n + 1) for i in range(m): u, v, d = map(int, input().split()) lst[u] -= d lst[v] += d dena = [] dene_wala = [] lena = [] lene_wala = [] for j in range(len(lst)): if lst[j] < 0: dena.append(abs(lst[j])) dene_wala.append(j) elif lst[j] > 0:...
ASSIGN VAR 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ...
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,...
n, m = map(int, input().split()) d = {} for i in range(m): x, y, a = map(int, input().split()) d[x] = d.get(x, 0) - a d[y] = d.get(y, 0) + a pos = [] neg = [] for i in d: if d[i] < 0: neg.append([d[i], i]) elif d[i] > 0: pos.append([d[i], i]) ans = [] i = 0 j = 0 while i < len(neg) a...
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUM...
There are $n$ people in this world, conveniently numbered $1$ through $n$. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or needs, so he borrows money from someone else, with the idea that he will repay the loan later with interest. Let $d(a,...
import sys input = sys.stdin.readline inf = 100000000000000000 mod = 998244353 n, m = map(int, input().split()) B = [0] * n JIE = [] HUAN = [] for i in range(m): x, y, z = map(int, input().split()) x -= 1 y -= 1 B[x] += z B[y] -= z for i in range(n): if B[i] > 0: JIE.append(i) elif ...
IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR FOR VA...
Suppose you had an array $A$ of $n$ elements, each of which is $0$ or $1$. Let us define a function $f(k,A)$ which returns another array $B$, the result of sorting the first $k$ elements of $A$ in non-decreasing order. For example, $f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$. Note that the first $4$ elements were sorted....
from builtins import map def main(): for t in range(int(input())): n = int(input()) ans = [(0) for i in range(n)] a = list(map(lambda ai: int(ai), input().split(" "))) k = sum(a) // n lb = n - k for i in range(1, k + 1): a[n - i] -= i for i in ra...
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER...
Suppose you had an array $A$ of $n$ elements, each of which is $0$ or $1$. Let us define a function $f(k,A)$ which returns another array $B$, the result of sorting the first $k$ elements of $A$ in non-decreasing order. For example, $f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$. Note that the first $4$ elements were sorted....
def solve(bits): first_one_encountered = False depth = 0 next_zero = set() answer = [] for i, sum in enumerate(bits): if sum == 0: answer.append(0) elif not first_one_encountered: answer.append(1) first_one_encountered = True depth = su...
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CA...
Suppose you had an array $A$ of $n$ elements, each of which is $0$ or $1$. Let us define a function $f(k,A)$ which returns another array $B$, the result of sorting the first $k$ elements of $A$ in non-decreasing order. For example, $f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$. Note that the first $4$ elements were sorted....
for i in range(int(input())): n = int(input()) dat = list(map(int, input().split())) s = sum(dat) num = s // n nul = [(0) for i in range(n)] c = 0 rs = [(0) for i in range(n)] for i in range(n - 1, -1, -1): c -= nul[i] if num: c += 1 if i - num >= ...
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUN...
Suppose you had an array $A$ of $n$ elements, each of which is $0$ or $1$. Let us define a function $f(k,A)$ which returns another array $B$, the result of sorting the first $k$ elements of $A$ in non-decreasing order. For example, $f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$. Note that the first $4$ elements were sorted....
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) it = -1 ans = [1] * n for i in range(n): if ans[i] == 0: it = a[i] + i elif a[i] == 0: it = i else: it = a[i] if it == n: break a...
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIG...
Suppose you had an array $A$ of $n$ elements, each of which is $0$ or $1$. Let us define a function $f(k,A)$ which returns another array $B$, the result of sorting the first $k$ elements of $A$ in non-decreasing order. For example, $f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$. Note that the first $4$ elements were sorted....
T = int(input()) for t in range(T): N = int(input()) C = list(map(int, input().split())) ans = [0] * N k = sum(C) // N i = N - 1 while i > -1 and k > 0: if C[i] == N: ans[i] = 1 k -= 1 else: C[i - k] += N - i i -= 1 print(*ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR ASS...
Suppose you had an array $A$ of $n$ elements, each of which is $0$ or $1$. Let us define a function $f(k,A)$ which returns another array $B$, the result of sorting the first $k$ elements of $A$ in non-decreasing order. For example, $f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$. Note that the first $4$ elements were sorted....
def solve(): n = int(input()) c = list(map(int, input().split())) sum = 0 for i in range(n): sum += c[i] ones = sum // n ans = [(1) for _ in range(n + 1)] first_0 = c[0] ans[first_0] = 0 for i in range(n): if ans[i] == 1: if c[i] == 0: ans[...
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR...
Suppose you had an array $A$ of $n$ elements, each of which is $0$ or $1$. Let us define a function $f(k,A)$ which returns another array $B$, the result of sorting the first $k$ elements of $A$ in non-decreasing order. For example, $f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$. Note that the first $4$ elements were sorted....
def reversesortsum(nums): total = sum(nums) k = total // n ans = [0] * n b = [0] * n lf = n - k for i in range(lf, n): b[i] = n - 1 for i in range(n - 1, -1, -1): if lf > i: break cur = nums[i] - (b[i] - i) if cur == i + 1: ans[i] = 1 ...
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP V...
Suppose you had an array $A$ of $n$ elements, each of which is $0$ or $1$. Let us define a function $f(k,A)$ which returns another array $B$, the result of sorting the first $k$ elements of $A$ in non-decreasing order. For example, $f(4,[0,1,1,0,0,1,0]) = [0,0,1,1,0,1,0]$. Note that the first $4$ elements were sorted....
import sys def solve(): inp = sys.stdin.readline n = int(input()) c = list(map(int, inp().split())) a = ["0"] * n sub = [0] * (n + 1) have = 0 j = 0 cur = 0 for i in range(0, n): sub[i - have + 1] += 1 sub[i + 1] -= 1 if j >= i - have + 1: cur +=...
IMPORT FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN...
This is an easier version of the next problem. In this version, $q = 0$. A sequence of integers is called nice if its elements are arranged in blocks like in $[3, 3, 3, 4, 1, 1]$. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible...
import sys input = sys.stdin.readline n, q = map(int, input().split()) A = list(map(int, input().split())) if max(A) == min(A): print(0) sys.exit() L = max(A) MM = [[200005, -1, i] for i in range(L + 1)] COUNT = [0] * (L + 1) for i in range(n): a = A[i] MM[a][0] = min(MM[a][0], i) MM[a][1] = max(MM...
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR VAR FUNC_CALL VAR BIN_OP VA...
This is an easier version of the next problem. In this version, $q = 0$. A sequence of integers is called nice if its elements are arranged in blocks like in $[3, 3, 3, 4, 1, 1]$. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible...
from sys import stdin n, m = list(map(int, stdin.readline().strip().split())) s = list(map(int, stdin.readline().strip().split())) mx = 200000 arr = [(-1) for i in range(mx + 1)] visited = [(False) for i in range(mx + 1)] cnt = [(0) for i in range(mx + 1)] for i in range(n): arr[s[i]] = i x = 0 ans = 0 inf = mx + ...
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR ...
This is an easier version of the next problem. In this version, $q = 0$. A sequence of integers is called nice if its elements are arranged in blocks like in $[3, 3, 3, 4, 1, 1]$. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible...
MAXN = 200100 n, q = list(map(int, input().split())) a = list(map(int, input().split())) lpos = [-1] * MAXN for i in range(n): lpos[a[i]] = i need = 0 i = 0 while i < n: start = i r = lpos[a[i]] cnts = {} while i <= r: r = max(r, lpos[a[i]]) if a[i] in cnts: cnts[a[i]] +=...
ASSIGN VAR NUMBER 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 VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR V...
This is an easier version of the next problem. In this version, $q = 0$. A sequence of integers is called nice if its elements are arranged in blocks like in $[3, 3, 3, 4, 1, 1]$. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible...
from sys import stdin n, q = tuple(int(x) for x in stdin.readline().split()) tpl = tuple(x for x in stdin.readline().split()) dic = {} amt = {} for i in range(n): dic[tpl[i]] = i if tpl[i] not in amt: amt[tpl[i]] = 1 else: amt[tpl[i]] += 1 ans = 0 counter = 0 while counter < n: right_bo...
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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR V...
This is an easier version of the next problem. In this version, $q = 0$. A sequence of integers is called nice if its elements are arranged in blocks like in $[3, 3, 3, 4, 1, 1]$. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible...
import itertools import sys n, q = map(int, input().split()) blocks = list(map(int, input().split())) dif = dict() for i in range(n): if blocks[i] in dif: dif[blocks[i]][1] = i dif[blocks[i]][2] = dif[blocks[i]][2] + 1 elif blocks[i] not in dif: dif[blocks[i]] = [i, i, 1] rez = 0 end = ...
IMPORT IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR V...
This is an easier version of the next problem. In this version, $q = 0$. A sequence of integers is called nice if its elements are arranged in blocks like in $[3, 3, 3, 4, 1, 1]$. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible...
n, q = map(int, input().split()) A = list(map(int, input().split())) left = {} right = {} for i in range(n): if A[i] not in left: left[A[i]] = i right[A[i]] = i E = [] for elem in left: E.append([left[elem], -1]) E.append([right[elem], 1]) E.sort() u = 0 b = 0 cntr = {} ans = 0 for i in range(n)...
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 FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER EXPR FUNC_...
This is an easier version of the next problem. In this version, $q = 0$. A sequence of integers is called nice if its elements are arranged in blocks like in $[3, 3, 3, 4, 1, 1]$. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible...
MAXN = 200100 n, q = map(int, input().split()) a = list(map(int, input().split())) lpos = [-1] * MAXN for i in range(n): lpos[a[i]] = i need = 0 i = 0 while i < n: start = i r = lpos[a[i]] j = i + 1 while j < r: r = max(r, lpos[a[j]]) j += 1 cnts = {} while i <= r: if...
ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN ...
This is an easier version of the next problem. In this version, $q = 0$. A sequence of integers is called nice if its elements are arranged in blocks like in $[3, 3, 3, 4, 1, 1]$. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible...
n, _q = map(int, input().split()) mni = [-1] * 200001 mxi = [-1] * 200001 cnt = [0] * 200001 nd = 0 a = list(map(int, input().split())) for i, v in enumerate(a): if mni[v] == -1: mni[v] = i nd += 1 mxi[v] = i cnt[v] += 1 r = 0 z = 0 currmax = 0 for i, v in enumerate(a): if i == mni[v]: ...
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ...
This is an easier version of the next problem. In this version, $q = 0$. A sequence of integers is called nice if its elements are arranged in blocks like in $[3, 3, 3, 4, 1, 1]$. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible...
n, q = map(int, input().split()) a = list(map(int, input().split())) d = {} def max_frequent(s, e, a): d = {} for x in a[s : e + 1]: if x not in d: d[x] = 0 d[x] += 1 return e - s + 1 - max(list(d.values())) for i, x in enumerate(a): if x not in d: d[x] = [] d...
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 FUNC_DEF ASSIGN VAR DICT FOR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR FU...
This is an easier version of the next problem. In this version, $q = 0$. A sequence of integers is called nice if its elements are arranged in blocks like in $[3, 3, 3, 4, 1, 1]$. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible...
N, Q = list(map(int, input().split())) A = [int(a) for a in input().split()] B = sorted(list(set(A))) M = len(B) IA = {} for i in range(M): IA[B[i]] = i A = [IA[a] for a in A] L = [N] * M R = [-1] * M C = [0] * M for i in range(N): L[A[i]] = min(L[A[i]], i) R[A[i]] = max(R[A[i]], i) C[A[i]] += 1 X = [] ...
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN...
This is an easier version of the next problem. In this version, $q = 0$. A sequence of integers is called nice if its elements are arranged in blocks like in $[3, 3, 3, 4, 1, 1]$. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible...
n, q = map(int, input().split()) A = list(map(int, input().split())) sizes = dict() for j in range(n): if A[j] in sizes: sizes[A[j]][2] += 1 sizes[A[j]][1] = j else: sizes[A[j]] = [j, j, 1] answer = 0 end = -1 max_size = -1 for j in range(n): end = max(end, sizes[A[j]][1]) max_si...
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR LIST VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER...
This is an easier version of the next problem. In this version, $q = 0$. A sequence of integers is called nice if its elements are arranged in blocks like in $[3, 3, 3, 4, 1, 1]$. Formally, if two elements are equal, everything in between must also be equal. Let's define difficulty of a sequence as a minimum possible...
import sys input = sys.stdin.buffer.readline def find(x): if par[x] == x: return x par[x] = find(par[x]) return par[x] def union(a, b): xa = find(a) xb = find(b) if size[xa] > size[xb]: xa, xb = xb, xa par[xa] = xb size[xb] += size[xa] n, m = map(int, input().split...
IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR...
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: ...
import sys input = sys.stdin.readline class Bit: def __init__(self, n): self.size = n self.tree = [0] * (n + 1) def sum(self, i): s = 0 while i > 0: s += self.tree[i] i -= i & -i return s def add(self, i, x): while i <= self.size:...
IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR 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 LIST FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VA...
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: ...
def argsort(seq): return sorted(range(len(seq)), key=seq.__getitem__) t = int(input()) for _ in range(t): n, m = list(map(int, input().split())) x_all = list(map(int, input().split())) sorted_x = argsort(x_all) cnt = 0 for k in range(n): x = sorted(sorted_x[k * m : (k + 1) * m]) ...
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL 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 FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR ...
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: ...
import sys class fenwick_tree: __slots__ = ["n", "data"] def __init__(self, n): self.n = n self.data = [0] * self.n def add(self, p, x): p += 1 while p <= self.n: self.data[p - 1] += x p += p & -p def sum(self, l, r): return self._sum(...
IMPORT CLASS_DEF ASSIGN VAR LIST STRING STRING FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN...
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: ...
import sys input = sys.stdin.readline class fenwicktree: def __init__(self, Size): self.Tree = [0] * Size def add(self, Index, Value): Index += 1 while Index <= len(self.Tree): self.Tree[Index - 1] += Value Index += Index & -Index def get(self, R): ...
IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL ...
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: ...
for _ in range(int(input())): n, m = map(int, input().split()) tot = n * m a = list(map(int, input().split())) l = list(range(tot)) l.sort(key=lambda x: (a[x], x)) mat = [] for i in range(n): mat.append(l[m * i : m * (i + 1)]) for i in range(n): mat[i].sort(key=lambda x: ...
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR...
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: ...
from sys import stdin input = stdin.readline def answer(): s = [[(0) for i in range(m)] for j in range(n)] ans, ind = 0, 0 for i in range(n): for j in range(m): x = d[ind] // m y = d[ind] % m value = 0 for k in range(y + 1): value +=...
ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR ...
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: ...
import sys input = sys.stdin.readline def s(a): n = len(a) r = 0 for i in range(n): if a[i] == 0: if i > 0 and a[i - 1] == 1 or i + 1 < n and a[i + 1] == 1: r += 2 else: r += 1 return r def solve(): n, m = map(int, input().split())...
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIG...
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: ...
T = int(input()) for t in range(T): n, m = map(int, input().split()) a = list(enumerate(map(int, input().split()))) a.sort(key=lambda x: (x[1], x[0])) first = 0 num = a[0][1] for i in range(1, n * m): if a[i][1] != num: a[first:i] = reversed(a[first:i]) first = i ...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER ...
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: ...
def inp(): return int(input()) def st(): return input().rstrip("\n") def lis(): return list(map(int, input().split())) def ma(): return map(int, input().split()) t = inp() while t: t -= 1 n, m = ma() a = lis() b = sorted(a) r = [] dic = {} cc = 0 row = [[(-1) for ...
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR...
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: ...
class segt: def __init__(self, n): self.bp = 1 while self.bp < n: self.bp <<= 1 self.heap = [0] * (2 * self.bp) def add(self, idx): idx += self.bp self.heap[idx] = 1 while idx > 0: idx //= 2 self.heap[idx] = self.heap[idx * 2]...
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN...
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: ...
import sys class BIT: def __init__(self, n): self.n = n self.data = [0] * n def build(self, arr): for i, a in enumerate(arr): self.data[i] = a for i in range(1, self.n + 1): if i + (i & -i) <= self.n: self.data[i + (i & -i) - 1] += self...
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_DEF VAR NUMBER WHILE VAR VAR VAR BIN...
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: ...
t = int(input()) for _ in range(t): n, m = map(int, input().split()) a = list(map(int, input().split())) a = sorted((a[i], i) for i in range(len(a))) r, d = 0, 0 a.append((10**9 + 9, -1)) b = 0 ca = [] ans = [] for i in range(n * m + 1): if a[b][0] != a[i][0]: rw ...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN...
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: ...
a = int(input()) for i in range(a): ok = dict() p1, p2 = map(int, input().split()) p3 = list(map(int, input().split())) op = p3[:] for j in range(p1 * p2): ok[p3[j]] = [] for j in range(p1 * p2): ok[p3[j]].append(j) p3 = sorted(p3) miss = [] for j in range(p1 * p2 - 1...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR LIST FOR VAR FUNC_CALL VAR BIN_OP V...
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: ...
import sys input = sys.stdin.buffer.readline def solve(): indices = list(range(N * M)) indices.sort(key=lambda i: A[i]) indexOf = {} for i, val in enumerate(indices): indexOf[val] = i indices.sort(key=lambda i: (A[i], indexOf[i] // M, -i)) ans = 0 row = [0] * M for i in range(...
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CA...
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: ...
for _ in range(int(input())): n, m = map(int, input().split()) a = list(map(int, input().split())) d = {} for i in range(n * m): d[a[i]] = d.get(a[i], []) d[a[i]].append(i) o = [[] for _ in range(n)] i = k = 0 dd = sorted(d) for j in range(n): while len(d[dd[i]][k...
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST ...
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: ...
import sys pin = sys.stdin.readline def update(tree, n): while n < len(tree): tree[n] += 1 n += n & -n def psum(tree, n): ans = 0 while n: ans += tree[n] n -= n & -n return ans for T in range(int(pin())): N, M = map(int, pin().split()) A = sorted([(v, k) fo...
IMPORT ASSIGN VAR VAR FUNC_DEF WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ...
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: ...
t = int(input()) for _ in range(t): n, m = tuple(map(int, input().split())) sp = input().split() sp = [(i, int(sp[i])) for i in range(n * m)] sp.sort(key=lambda x: (x[1], x[0])) new = [] for i in range(0, n * m, m): new.append(sorted(sp[i : i + m], key=lambda x: (x[1], -1 * x[0]))) a...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR ...
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: ...
def solve(): n, m = map(int, input().split()) l = list(map(int, input().split())) sit = sorted([[l[i], i] for i in range(n * m)]) grid = [[(0) for i in range(m)] for j in range(n)] sit_number = {} for x in range(len(sit)): sit_number[sit[x][1]] = [x // m, x % m] ans = 0 for i in ...
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VA...
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: ...
def mergeSort(arr, n): temp_arr = [0] * n return _mergeSort(arr, temp_arr, 0, n - 1) def _mergeSort(arr, temp_arr, left, right): inv_count = 0 if left < right: mid = (left + right) // 2 inv_count += _mergeSort(arr, temp_arr, left, mid) inv_count += _mergeSort(arr, temp_arr, mid...
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIG...
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: ...
for _ in range(int(input())): n, m = map(int, input().split()) l = [int(x) for x in input().split()] ans = [[l[i], i] for i in range(n * m)] ans.sort() inconv = 0 cnt = 0 for i in range(n): lst = ans[i * m : i * m + m] lst.sort(key=lambda x: x[1]) for i in range(m): ...
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR...
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: ...
import sys input = iter(sys.stdin.read().splitlines()).__next__ def solve(): n, m = map(int, input().split()) sights = {} for i, sight in enumerate(map(int, input().split())): if sight not in sights: sights[sight] = [] sights[sight].append(i) seating = [None] * (n * m) ...
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR VAR ASSIGN VAR NU...
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: ...
TESTS = int(input()) for t in range(TESTS): n, m = map(int, input().split()) people = list(map(int, input().split())) seats = people.copy() seats.sort() lookup = {} for i, s in enumerate(seats): if s not in lookup.keys(): lookup[s] = [i] else: lookup[s].ap...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR LIST ...
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: ...
import sys input = sys.stdin.readline class Bit: def __init__(self, n): self.size = n self.tree = [0] * (n + 1) def range_sum(self, l, r): return self.sum(r - 1) - self.sum(l - 1) def sum(self, i): i += 1 s = 0 while i > 0: s += self.tree[i] ...
IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP FUNC_CALL...
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: ...
def pos(sa, k, l=0, r=None): if r is None: r = len(sa) m = (l + r) // 2 if l == r: return l elif k > sa[m]: return pos(sa, k, m + 1, r) elif k < sa[m]: return pos(sa, k, l, m) else: return m def val(q): inc = 0 slist = [] for i in q: ...
FUNC_DEF NUMBER NONE IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN VAR IF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR EXPR ...
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: ...
import sys input = sys.stdin.readline for _ in range(int(input())): n, m = map(int, input().split()) a = list(map(int, input().split())) s = sorted((v, i) for i, v in enumerate(a)) suma = 0 for i in range(n): start = s[m * i][0] end = s[m * (i + 1) - 1][0] if start != end: ...
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP...
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: ...
t = int(input()) for _ in range(t): n, m = map(int, input().split(" ")) arr = list(map(int, input().split())) a = [] k = 0 for i in arr: a.append((i, k)) k += 1 a.sort() ans = 0 for i in range(0, n * m, m): y = a[i : i + m] y.sort(key=lambda x: x[1]) ...
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR...
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: ...
import sys class SegmentTree: def __init__(self, data, default=0, func=lambda x, y: x + y): self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size : ...
IMPORT CLASS_DEF FUNC_DEF NUMBER BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_...
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: ...
import sys input = sys.stdin.readline for _ in range(int(input())): n, m = map(int, input().strip().split()) arr = [[int(x)] for x in input().strip().split()] for x in range(n * m): arr[x].append(x + 1) arr.sort() cnt = 0 for r in range(0, n * m, m): for i in range(r, r + m): ...
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR N...
It is the hard version of the problem. The only difference is that in this version $1 \le n \le 300$. In the cinema seats can be represented as the table with $n$ rows and $m$ columns. The rows are numbered with integers from $1$ to $n$. The seats in each row are numbered with consecutive integers from left to right: ...
def mergeSort(myList, count): if len(myList) > 1: mid = len(myList) // 2 left = myList[:mid] right = myList[mid:] mergeSort(left, count) mergeSort(right, count) i = 0 j = 0 k = 0 while i < len(left) and j < len(right): if left[i] < ...
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER BIN_OP FUNC_CA...
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by ...
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() l = 0 h = len(people) - 1 remain_capacity = limit if len(people) == 0: return 0 boat = 1 ppl_in_boat = 2 while l <= h: if people[h] <...
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBE...
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by ...
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() print(people) boats = 0 left_index = 0 right_index = len(people) - 1 while left_index <= right_index: if people[left_index] + people[right_index] <= limit: ...
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by ...
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: bucket = [0] * (limit + 1) for i in people: bucket[i] += 1 start = 0 end = len(bucket) - 1 count = 0 while start <= end: while start <= end and bucket[start] <= 0:...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMB...
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by ...
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people = sorted(people) count = 0 left = 0 right = len(people) - 1 while left <= right: cur = people[left] + people[right] if cur <= limit: left += 1 ...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by ...
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: if people == None or len(people) == 0: return 0 people.sort() start = 0 end = len(people) - 1 counter = 0 while start <= end: if people[start] + people[end] <= lim...
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NONE FUNC_CALL VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by ...
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() p1 = 0 p2 = len(people) - 1 count = 1 cur = 0 curNum = 0 while p1 <= p2: if people[p2] <= limit - cur and curNum < 2: cur = cur + people[...
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_...
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by ...
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: if not people: return 0 people = sorted(people) left = 0 right = len(people) - 1 board_cnt = 0 while left <= right: if left == right: board_cnt += ...
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by ...
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() boats = 0 l = 0 r = len(people) - 1 while r > l: boats += 1 if people[l] + people[r] <= limit: l += 1 r = r - 1 e...
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by ...
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: boats = 0 people = sorted(people) print(people) i, j = 0, len(people) - 1 while i <= j: if people[i] + people[j] > limit: j -= 1 else: i +=...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by ...
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() num_boats = 0 last = len(people) - 1 first = 0 while first < last: if people[first] + people[last] <= limit: last -= 1 first += 1 ...
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by ...
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: l = len(people) if l <= 1: return 1 people.sort() i = 0 j = l - 1 c = 0 while i < j: print((i, j, c)) if people[i] + people[j] <= limit: ...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR NUM...
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by ...
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: heap = [] people.sort() heap.append((people[len(people) - 1], 1)) i = len(people) - 2 while i >= 0: ele = people[i] if limit >= ele + heap[0][0] and heap[0][1] < 2: ...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ...
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by ...
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() lo = 0 count = 0 for hi in range(len(people))[::-1]: if lo < hi and people[lo] + people[hi] <= limit: lo += 1 if lo == hi: return len...
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by ...
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort() N = len(people) num_boats = 0 if len(people) == 1: return 1 less_than_half = [p for p in people if p <= limit / 2] more_than_half = [p for p in people if p > lim...
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR...
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by ...
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people.sort(reverse=True) boats = people[: (1 + len(people)) // 2] onboard = [1] * len(boats) i = 0 j = len(boats) k = len(people) - 1 while j <= k: if i == len(boats)...
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VA...
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by ...
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: left = 0 right = len(people) - 1 boat = 0 people.sort() while left <= right: if left == right: boat += 1 break elif people[left] + people[r...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by ...
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: people = sorted(people, reverse=True) i = 0 j = len(people) - 1 n = 0 while True: if i >= j: break n += 1 w1 = people[i] i += 1 ...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by ...
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: new = sorted(people) i, j = 0, len(people) - 1 res = 0 while i <= j: if new[j] + new[i] <= limit: i += 1 j -= 1 res += 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
The i-th person has weight people[i], and each boat can carry a maximum weight of limit. Each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. Return the minimum number of boats to carry every given person.  (It is guaranteed each person can be carried by ...
class Solution: def numRescueBoats(self, people: List[int], limit: int) -> int: minBoats = 0 people.sort() left = 0 right = len(people) - 1 while left <= right: if people[left] + people[right] <= limit: left += 1 minBoats += 1 ...
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR