description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | line = input().strip()
d, c = [(-1) for char in line], [(-1) for char in line]
stack = []
for i, char in enumerate(line):
if char == "(":
stack.append(i)
elif not stack:
c[i] = -1
d[i] = -1
else:
d[i] = stack.pop()
if line[d[i] - 1] == ")" and c[d[i] - 1] != -1:
c[i] = c[d[i] - 1]
else:
c[i] = d[i]
max_length = max(_ - c[_] if c[_] != -1 else -1 for _ in range(len(line)))
count_max = sum(
1 if _ - c[_] == max_length and c[_] != -1 else 0 for _ in range(len(line))
)
if max_length == -1:
print("0 1")
else:
print(max_length + 1, count_max) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR IF VAR BIN_OP VAR VAR NUMBER STRING VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | def LongestBracketSequence(s):
stack = []
arr = [0] * len(s)
max, sum, count = [0, 0, 0]
longest_count = 0
for i in range(len(s)):
ch = s[i]
if ch == "(":
stack.append("(")
elif ch == ")" and len(stack) != 0:
top = stack[-1]
if top == "(":
stack.pop()
arr[i] = 2
for value in reversed(arr):
if value == 2:
if count < 0:
count = 1
if max < sum:
max = sum
longest_count = 1
elif max == sum:
longest_count += 1
sum = 2
else:
count += 1
sum += 2
else:
count -= 1
if count == -1:
if max < sum:
max = sum
longest_count = 1
elif max == sum:
longest_count += 1
sum = 0
if count == 0 and sum > 0:
if max < sum:
max = sum
longest_count = 1
elif max == sum:
longest_count += 1
if max == 0:
longest_count = 1
print(str(max) + " " + str(longest_count))
s = input()
LongestBracketSequence(s) | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR STRING IF VAR STRING FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | x = input()
ex = [-1] * len(x)
m = 0
f = [0] * len(x)
ans1 = -1
s = []
for index, i in enumerate(x):
if i == "(":
s.append(index)
elif len(s) > 0:
p = s.pop()
ex[index] = p
if p > 0 and ex[p - 1] > -1:
ex[index] = ex[p - 1]
f[index - ex[index]] += 1
ans1 = max(ans1, index - ex[index])
print(ans1 + 1, max(1, f[ans1])) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | s = input().lstrip(")")
max_len = 0
num_max = 1
count = 0
start_pos = {}
for i, c in enumerate(s):
if c == "(":
count += 1
if count not in start_pos:
start_pos[count] = i
else:
count -= 1
if count < 0:
count = 0
start_pos = {}
if count + 2 in start_pos:
del start_pos[count + 2]
if count + 1 in start_pos:
l = i - start_pos[count + 1] + 1
if l > max_len:
max_len = l
num_max = 1
elif l == max_len:
num_max += 1
print(max_len, num_max) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | import sys
cases = False
def fast_pow(a: int, b: int):
res = 1
while b > 0:
if b & 1:
res *= a
a *= a
b >>= 1
return res
def c2(n):
return n * (n - 1) // 2
def get():
return list(map(int, input().split()))
def bits(n: int):
return list(bin(n)).count("1")
def main(test_case=False):
n = int(input()) if test_case else 1
for _ in range(n):
test()
def flush():
sys.stdout.flush()
def parr(arr):
print(*arr, sep=" ")
def gcd(a, b):
while b:
if b % a == 0:
break
tmp = a
a = b % a
b = tmp
return a
def ext_gcd(a: int, b: int):
if b == 0:
return [a, [1, 0]]
res = ext_gcd(b, a % b)
g = res[0]
x1 = res[1][0]
y1 = res[1][1]
x = y1
y = x1 - y1 * (a // b)
return [g, [x, y]]
stack, pair, first = [], [], []
def test():
global stack, pair, first
s = list(input())
n = len(s)
pair = [-1] * n
first = [-1] * n
i = 0
while i < n:
if s[i] == "(":
stack.append(i)
elif stack:
d = stack.pop()
pair[i] = d
first[i] = d
if pair[i] > 0 and first[pair[i] - 1] != -1:
first[i] = first[pair[i] - 1]
else:
pair[i] = -1
first[i] = -1
i += 1
i = 0
mx = 0
cnt = 1
while i < n:
if first[i] == -1:
i += 1
continue
ln = i - first[i] + 1
if ln > mx:
mx = ln
cnt = 1
elif ln == mx:
cnt += 1
i += 1
print(mx, cnt)
main(cases) | IMPORT ASSIGN VAR NUMBER FUNC_DEF VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF VAR RETURN FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR STRING FUNC_DEF NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR VAR STRING FUNC_DEF WHILE VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF VAR VAR IF VAR NUMBER RETURN LIST VAR LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR RETURN LIST VAR LIST VAR VAR ASSIGN VAR VAR VAR LIST LIST LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | inputs = input()
c = [-1] * len(inputs)
stack = []
ans, count = 0, 1
for index, item in enumerate(inputs):
if item == "(":
stack.append(index)
elif stack:
x = stack.pop()
if x:
c[index] = x if c[x - 1] == -1 else c[x - 1]
else:
c[index] = x
tmp = index - c[index] + 1
if ans < tmp:
ans, count = tmp, 1
elif ans == tmp:
count += 1
print(ans, count) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR IF VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | s = input()
st = list()
c = 0
x = 0
max = 0
st.append(-1)
for i in range(len(s)):
if s[i] == "(":
st.append(i)
else:
st.pop()
if st:
c = i - st[-1]
if c > max:
max = c
x = 1
elif c >= 1 and c == max:
x += 1
else:
st.append(i)
if x >= 1:
print(str(max) + " " + str(x))
else:
print("0 1") | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | def main():
m = cnt = 1
stack = [1]
for c in input():
if c == "(":
stack.append(1)
elif len(stack) < 2:
stack[0] = 1
else:
x = stack.pop() + stack.pop() + 1
if m < x:
m, cnt = x, 1
elif m == x:
cnt += 1
stack.append(x)
print(m - 1, cnt)
main() | FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | s = input()
a = []
for i in range(len(s)):
if len(a) != 0 and s[a[-1]] == "(" and s[i] == ")":
a.pop()
else:
a.append(i)
if len(a) == len(s):
print(0, 1)
elif len(a) == 0:
print(len(s), 1)
else:
b = [a[0] - 0]
for i in range(len(a) - 1):
b.append(a[i + 1] - a[i] - 1)
b.append(len(s) - a[-1] - 1)
m = max(b)
print(m, b.count(m)) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER STRING VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | s = input()
s1 = 0
s2 = 0
ans = []
st = []
c = 0
for i in s:
if i == "(":
s1 += 1
st.append(i)
elif s1 > 0:
c1 = 0
while st[-1] != "(":
k = st.pop()
c1 += k
c1 += 2
st.pop()
s1 -= 1
st.append(c1)
else:
st.append(")")
ans = []
c = 0
kk = 1
for i in st:
if i == ")" or i == "(":
ans.append(c)
c = 0
else:
c += i
kk = 0
ans.append(c)
if kk == 1:
print(0, 1)
else:
print(max(ans), ans.count(max(ans))) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | string = input()
st = list()
mx = occ = 0
scores = [1] * len(string) + [0]
for idx, c in enumerate(string):
if len(st) == 0:
st.append(idx)
elif string[st[-1]] == "(" and c == ")":
popped = st.pop()
scores[idx + 1] += scores[idx] + scores[popped]
combined = scores[idx] + scores[popped]
if combined > mx:
mx = combined
occ = 1
elif combined == mx:
occ += 1
else:
st.append(idx)
if mx == 0:
print(mx, 1)
else:
print(mx, occ) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR VAR LIST NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | def ii():
return int(input())
def si():
return input()
def mi():
return map(int, input().split())
def msi():
return map(str, input().split())
def li():
return list(mi())
s = si()
dp = [0] * (len(s) + 1)
ocnt = 0
for i in range(len(s)):
if s[i] == "(":
ocnt += 1
elif ocnt > 0:
ocnt -= 1
if s[i - 1] == ")":
dp[i] = dp[i - 1] + 2
elif i > 1:
if s[i - 2] == ")":
dp[i] = 2 + dp[i - 2]
else:
dp[i] = 2
else:
dp[i] = 2
dp[i] = dp[i] + dp[i - dp[i]]
if max(dp) == 0:
print(0, 1)
else:
print(max(dp), dp.count(max(dp))) | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | s = input()
stack, c, l, ll = [-1], 1, 0, 0
for i in range(len(s)):
if s[i] == "(":
stack.append(i)
else:
stack.pop()
if stack:
k = stack[-1]
if i - stack[-1] > ll:
ll = i - stack[-1]
c = 1
elif i - stack[-1] == ll:
c += 1
else:
stack.append(i)
print(ll, c) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | E = [0] * 1000001
S = [0] * 1000001
f = [0] * 1000001
s = input()
stack = []
n = len(s)
mx = 0
for i in range(n):
if s[i] == "(":
stack.append(i)
elif stack == []:
S[i] = -1
E[i] = -1
else:
j = stack.pop()
S[i] = E[i] = j
if j > 0 and s[j - 1] == ")" and S[j - 1] >= 0:
E[i] = E[j - 1]
l = i - E[i] + 1
f[l] = f[l] + 1
mx = max(mx, l)
f[0] = 1
print(mx, end=" ")
print(f[mx]) | ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | s = input()
n = len(s)
d = {}
d[0] = -1
d[1] = 0
v = 1
st = []
m = 0
c = 1
st.append(s[0])
for i in range(1, n):
if s[i] == ")" and v > 0 and st[v - 1] == "(":
st.pop()
v -= 1
if m == i - d[v]:
c += 1
elif m < i - d[v]:
m = i - d[v]
c = 1
else:
v += 1
d[v] = i
st.append(s[i])
print(m, c) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING VAR NUMBER VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | bs = input().strip()
n = len(bs)
prevs = [-1] * n
starts = [-1] * n
opens = []
for i, b in enumerate(bs):
if b == "(":
opens.append(i)
elif opens:
j = opens.pop()
prevs[i] = j
if starts[j - 1] == -1:
starts[i] = j
else:
starts[i] = starts[j - 1]
best = 0
best_count = 1
for i, s in enumerate(starts):
if s != -1:
l = i - s + 1
if l > best:
best = l
best_count = 1
elif l == best:
best_count += 1
print(best, best_count) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | seq = input()
longest = 0
total = 0
st = [-1]
for i in range(len(seq)):
ch = seq[i]
if ch == ")":
if len(st) > 0 and st[-1] >= 0 and seq[st[-1]] == "(":
st.pop()
else:
st.append(i)
else:
st.append(i)
st.append(len(seq))
if len(st) > 0:
longest = st[0]
m = {}
for i in range(1, len(st)):
if st[i - 1] + 1 != st[i]:
length = st[i] - st[i - 1] - 1
longest = max(longest, length)
if length not in m:
m[length] = 1
else:
m[length] += 1
if longest > 0:
print(longest, m[longest])
else:
print(0, 1) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | from sys import stdin, stdout
s = stdin.readline().rstrip("\n")
n = len(s)
l1 = [-1] * n
l2 = [-1] * n
l3 = [0] * (n + 1)
l = []
for i in range(n):
if s[i] == "(":
l.append(i)
elif len(l):
a = l[-1]
l.pop()
l1[i] = i
if a != 0:
if l2[a - 1] != -1:
l2[i] = l2[a - 1]
else:
l2[i] = a
else:
l2[i] = 0
l3[l1[i] - l2[i] + 1] += 1
ans1, ans2 = 0, 1
for i in range(n, -1, -1):
if l3[i] != 0:
ans1 = i
ans2 = l3[i]
break
print(ans1, ans2) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | st = input()
b = []
for j in st:
b.append(j)
n = len(b)
arr = []
dp = [0] * n
j = 0
while j < n:
if b[j] == ")":
if arr == []:
pass
elif arr[-1][0] == b[j]:
pass
else:
p = arr.pop()
dp[j] = j - p[1] + 1
if p[1] - 1 >= 0 and b[p[1] - 1] == ")":
dp[j] += dp[p[1] - 1]
else:
arr.append([b[j], j])
j += 1
p = dp.count(max(dp))
if max(dp) == 0:
print(0, 1)
else:
print(max(dp), p) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING IF VAR LIST IF VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER STRING VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | s = input()
a = []
n = len(s)
c = [(-1) for i in range(n)]
d = [(-1) for i in range(n)]
for i in range(len(s)):
if s[i] == "(":
a.append(i)
elif len(a) > 0:
d[i] = a[-1]
c[i] = d[i]
a.pop()
if d[i] - 1 > -1:
if c[d[i] - 1] != -1:
c[i] = c[d[i] - 1]
length = 0
ans = 0
for i in range(n):
if c[i] is not -1:
if length < i - c[i] + 1:
length = i - c[i] + 1
ans = 1
elif length == i - c[i] + 1:
ans += 1
if length == 0:
print(0, 1)
else:
print(length, ans) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN 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 FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR IF BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | s = input().strip()
max_string = 0
total_max_strings = 0
stack = [None] * len(s)
arr = [None] * len(s)
top = -1
for index, bracket in enumerate(s):
if bracket == "(":
top += 1
if arr[index - 1] != None:
stack[top] = arr[index - 1]
else:
stack[top] = index
elif top > -1:
temp_len = index - stack[top] + 1
if max_string < temp_len:
max_string = temp_len
total_max_strings = 1
elif max_string == temp_len:
total_max_strings += 1
arr[index] = stack[top]
top -= 1
print(max_string, total_max_strings or 1) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER NONE ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | t = input()
a = [0] * (len(t) + 1)
m = n = k = 0
for i in range(len(t)):
j = t[i]
if j == "(":
k += 1
if k > 0:
a[k] = i
else:
k -= 1
if k < 0:
k = 0
a[k] = i + 1
w = 0
elif k == 0:
w = i - a[k] + 1
else:
w = i - a[k]
if w > m:
m = w
n = 1
elif w == m:
n += 1
if m == 0:
n = 1
print(m, n) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR STRING VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | s = input()
n = len(s)
dp = []
stack = []
ans = []
for i in range(n):
if s[i] == "(":
dp.append(i)
stack.append(i)
ans.append(i)
else:
if not stack:
dp.append(-1)
ans.append(-1)
continue
dp.append(stack[-1])
if dp[-1] - 1 >= 0 and s[dp[-1] - 1] == ")" and ans[dp[-1] - 1] != -1:
ans.append(ans[dp[-1] - 1])
else:
ans.append(dp[-1])
stack.pop()
maxx = 0
count = 1
for i in range(len(ans)):
if i == ans[i]:
continue
diff = i - ans[i] + 1
if ans[i] != -1 and diff > maxx:
maxx = diff
count = 1
elif ans[i] != -1 and diff == maxx:
count += 1
print(maxx, count) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER STRING VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | def magic(value=None):
pairs = {}
maximum, count = 0, 0
num_of_opened, indices_of_opened = 0, []
for i, char in enumerate(value or input()):
if char == "(":
indices_of_opened.append(i)
elif indices_of_opened:
o = indices_of_opened.pop()
if o - 1 in pairs:
start_of_previous = pairs[o - 1]
pairs[i] = start_of_previous
item_size = i - start_of_previous + 1
else:
item_size = i - o + 1
pairs[i] = o
if item_size > maximum:
maximum = item_size
count = 1
elif item_size == maximum:
count += 1
if not maximum:
return "0 1"
return "%s %s" % (maximum, count)
print(magic()) | FUNC_DEF NONE ASSIGN VAR DICT ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER LIST FOR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER IF VAR RETURN STRING RETURN BIN_OP STRING VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | input_str = input().strip()
stack = []
maxlen = 0
subs_count = 0
count = 0
open_bracks = 0
for i in input_str:
if i == "(":
stack.append("(")
open_bracks += 1
elif stack != [] and open_bracks > 0:
open_bracks -= 1
temp = 2
if stack[-1] != "(":
temp += int(stack.pop())
if stack != [] and stack[-1] == "(":
stack.pop()
if stack != [] and stack[-1] not in ["(", "\n"]:
stack.append(temp + int(stack.pop()))
else:
stack.append(temp)
else:
stack.append("\n")
while stack != []:
val = stack.pop()
if val not in ["(", "\n"]:
val = int(val)
if maxlen < val:
maxlen = val
count = 1
elif maxlen == val:
count += 1
if maxlen == 0:
count = 1
print(maxlen, count) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER IF VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER STRING VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR LIST VAR NUMBER STRING EXPR FUNC_CALL VAR IF VAR LIST VAR NUMBER LIST STRING STRING EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING WHILE VAR LIST ASSIGN VAR FUNC_CALL VAR IF VAR LIST STRING STRING ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | s = input()
w, c, b, l = 0, 1, -1, []
for i in range(len(s)):
if s[i] == "(":
l.append(i)
elif l:
l.pop()
t = i - l[-1] if l else i - b
if t > w:
w, c = t, 1
elif t == w:
c += 1
else:
b = i
print(w, c) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | s = input().strip()
stack = []
valid = []
for k in range(len(s)):
if s[k] == "(":
stack.append(k)
elif s[k] == ")":
if not stack:
pass
else:
popped = stack.pop()
valid.append((popped, k))
num_substr = 0
maxlen = 0
if valid:
prev = valid[-1]
for k in range(len(valid) - 2, -1, -1):
if prev[0] < valid[k][0] and prev[1] > valid[k][1]:
prev = prev
elif prev[0] == valid[k][1] + 1:
prev = valid[k][0], prev[1]
else:
t = prev[1] - prev[0] + 1
if t > maxlen:
maxlen = t
num_substr = 1
elif t == maxlen:
num_substr += 1
prev = valid[k]
if valid:
t = prev[1] - prev[0] + 1
if t > maxlen:
maxlen = t
num_substr = 1
elif t == maxlen:
num_substr += 1
if num_substr == 0:
num_substr = 1
print("{} {}".format(maxlen, num_substr)) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | string = input()
stack = []
arr = []
for i in range(len(string)):
f = True
if stack:
if string[stack[-1]] == "(" and string[i] == ")":
stack.pop()
f = False
if f:
stack += (i,)
stack = [-1] + stack + [len(string)]
for i in range(0, len(stack) - 1):
if stack[i + 1] - stack[i] - 1 > 0:
arr += (stack[i + 1] - stack[i] - 1,)
if len(arr) == 0:
print(0, 1)
else:
print(max(arr), arr.count(max(arr))) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR IF VAR VAR NUMBER STRING VAR VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | a = input()
brakets = []
q = -1
ansmax = 0
quantity = 1
k = 0
for i in range(len(a)):
if a[i] == "(":
brakets.append(i)
elif brakets != []:
brakets.pop()
if brakets != []:
k = i - brakets[-1]
else:
k = i - q
if k > ansmax:
ansmax = k
quantity = 1
elif k == ansmax:
quantity += 1
else:
q = i
print()
print(ansmax, quantity) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR LIST EXPR FUNC_CALL VAR IF VAR LIST ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | s = str(input())
def solve(s, k):
stack = []
n = len(s)
ans = [0]
prev = 0
for i in range(n):
if s[i] == k:
if stack:
val = i + 1 - stack.pop()
if not stack:
prev += val
ans.append(prev)
else:
ans.append(val)
else:
prev = 0
else:
stack.append(i)
Max = max(ans)
return [Max, ans.count(Max)]
a, b = solve(s, ")")
c, d = solve(s[::-1], "(")
if a > c:
print(a, b)
elif a == c:
print(a, max(b, d))
else:
print(c, d) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN LIST VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER STRING IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | s = input()
d = [-1] * len(s)
e = [-1] * len(s)
stack = []
for i, c in enumerate(s):
if c == "(":
stack.append(i)
elif stack:
f = stack.pop()
d[i] = f
e[i] = f
if e[f - 1] != -1:
e[i] = e[f - 1]
best = 0
count = 0
for i in range(len(s)):
if e[i] != -1:
if best < i - e[i] + 1:
best = i - e[i] + 1
count = 0
if best == i - e[i] + 1:
count += 1
if best == 0:
print("0 1")
else:
print(best, count) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | def main():
m = cnt = 1
stack = [1]
for c in input():
if c == "(":
stack.append(1)
else:
try:
x = stack.pop() + stack.pop() + 1
if m < x:
m = x
cnt = 1
elif m == x:
cnt += 1
stack.append(x)
except IndexError:
stack = [1]
m -= 1
print(m, cnt if m else 1)
main() | FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | def main():
longest = 0
count = 1
left = list()
lead = 0
push = left.append
pop = left.pop
for paren in input():
if paren == "(":
push(0)
elif not left:
lead = 0
else:
span = pop() + 2
if left:
span += pop()
push(span)
else:
span += lead
lead = span
if span == longest:
count += 1
elif span > longest:
longest = span
count = 1
print(longest, count)
main() | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER IF VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | import sys
s = sys.stdin.readline().strip()
stack = []
dp = [0] * (len(s) + 1)
for i in range(len(s)):
if s[i] == ")":
if stack:
begin = stack.pop()
dp[i] = dp[begin - 1] + (i - begin + 1)
else:
stack.append(i)
mx = max(dp)
if mx == 0:
print(0, 1)
else:
print(mx, dp.count(mx)) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | s = input()
st = []
p = len(s)
for i in range(p):
if st != [] and s[i] == ")" and s[st[-1]] == "(":
st.pop()
else:
st.append(i)
if len(st) == p:
print("0 1")
else:
st.append(p)
d = [0] * (1000000 + 7)
k = -1
ma = 0
for i in st:
if i - k - 1 >= ma:
ma = i - k - 1
d[i - k - 1] += 1
k = i
print(ma, d[ma]) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR LIST VAR VAR STRING VAR VAR NUMBER STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | s = input()
n = len(s)
stack = []
dp = [(0) for i in range(0, n)]
max_size = 0
max_cnt = 0
for i in range(0, n):
c = s[i]
if c == "(":
stack.append(i)
elif len(stack) > 0:
prev = stack.pop()
temp = dp[prev - 1] + i - prev + 1
dp[i] = temp
if temp > max_size:
max_size = temp
max_cnt = 1
elif temp == max_size:
max_cnt += 1
if max_size == 0:
print("0 1")
else:
print(max_size, max_cnt) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | s = input()
n = len(s)
stack = []
early = [-1] * n
corrs = [-1] * n
for i in range(n):
if s[i] == "(":
stack.append(i)
elif stack:
corrs[i] = early[i] = stack.pop()
if early[i] and s[early[i] - 1] == ")" and early[early[i] - 1] != -1:
early[i] = early[early[i] - 1]
else:
early[i] = corrs[i] = -1
record = 0
count = 0
for i in range(n):
if early[i] == -1:
continue
length = i - early[i] + 1
if length > record:
record = length
count = 1
elif length == record:
count += 1
if record == 0:
print(0, 1)
else:
print(record, count) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR IF VAR VAR VAR BIN_OP VAR VAR NUMBER STRING VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | string = input()
close_ = open_ = max_len = 0
counter = 1
dp = [True] * len(string)
for idx in range(len(string)):
if string[idx] == "(":
open_ += 1
else:
close_ += 1
if open_ == close_:
len_ = 2 * open_
if len_ > max_len:
max_len, dp[idx - len_ + 1], counter = len_, False, 1
elif len_ == max_len:
dp[idx - len_ + 1] = False
counter += 1
elif close_ > open_:
close_ = open_ = 0
close_ = open_ = 0
for idx in range(len(string))[::-1]:
if string[idx] == "(":
open_ += 1
else:
close_ += 1
if open_ == close_:
len_ = 2 * open_
if len_ > max_len:
max_len, counter = len_, 1
elif len_ == max_len:
if dp[idx]:
counter += 1
elif open_ > close_:
close_ = open_ = 0
print(max_len, counter) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | st = [-1]
Max = Count = 0
for i, c in enumerate(input()):
if c == "(":
st.append(i)
elif len(st) > 1:
st.pop()
temp = i - st[-1]
if temp > Max:
Max = temp
Count = 1
elif temp == Max:
Count += 1
else:
st[0] = i
if Max:
print(Max, Count)
else:
print(0, 1) | ASSIGN VAR LIST NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | s = input()
m = [-1] * len(s)
e = [-1] * len(s)
f = [0] * len(s)
stack = []
l = 0
for i in range(len(s)):
if s[i] == "(":
stack.append(i)
elif len(stack) == 0:
m[i] = -1
else:
p = stack.pop()
m[i] = p
if p == 0:
e[i] = m[i]
elif s[p - 1] == ")" and m[p - 1] >= 0:
e[i] = e[p - 1]
else:
e[i] = m[i]
if e[i] >= 0:
c = i - e[i] + 1
f[c - 1] += 1
for k in range(len(f)):
if f[k] > 0:
l = k
if l > 0:
print(l + 1, f[l])
else:
print(l, 1) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR NUMBER |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | exp = input()
n = len(exp)
Pair = [-30] * (n + 1)
S = [0] * n
cnt = -1
for i in range(n):
if exp[i] == "(":
cnt += 1
S[cnt] = i
elif exp[i] == ")":
if cnt >= 0:
Pair[S[cnt]] = i
cnt -= 1
Max = 0
cnt = 1
last = -100
i = 0
while i < n:
tmp = Pair[i] - i + 1
if tmp > 0:
i += tmp
tmp = max(tmp, tmp + last)
else:
i += 1
last = tmp
if tmp > Max:
Max = tmp
cnt = 1
elif tmp == Max:
cnt += 1
print(Max, cnt) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR STRING IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | import sys
s = sys.stdin.readline().rstrip()
balance = 0
balanceRecord = {(0): -1}
minBalance = 0
maxLen = 0
maxCount = 1
for i in range(len(s)):
if s[i] == "(":
balance += 1
else:
balance -= 1
if balance < minBalance:
minBalance = balance
balanceRecord = {}
if balance in balanceRecord:
balanceRecord.pop(balance + 1)
newLen = i - balanceRecord[balance]
if newLen > maxLen:
maxLen = newLen
maxCount = 1
elif newLen == maxLen:
maxCount += 1
else:
balanceRecord[balance] = i
print(str(maxLen) + " " + str(maxCount)) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR DICT IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR |
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1 | s = input()
st = []
ans = []
for i, c in enumerate(s):
if c == "(":
st.append(i)
elif st:
q = st.pop()
cur = [q, i]
while True:
if ans:
x, y = ans[-1]
if x == cur[0] + 1 and y == cur[1] - 1:
ans.pop()
elif y == cur[0] - 1:
ans.pop()
cur[0] = x
else:
break
else:
break
ans.append(cur)
mx = 0
mc = 1
for x, y in ans:
if y - x + 1 > mx:
mx = y - x + 1
mc = 1
elif y - x + 1 == mx:
mc += 1
print(mx, mc) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR VAR WHILE NUMBER IF VAR ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR |
Dmitry has an array of $n$ non-negative integers $a_1, a_2, \dots, a_n$.
In one operation, Dmitry can choose any index $j$ ($1 \le j \le n$) and increase the value of the element $a_j$ by $1$. He can choose the same index $j$ multiple times.
For each $i$ from $0$ to $n$, determine whether Dmitry can make the $\mathrm{MEX}$ of the array equal to exactly $i$. If it is possible, then determine the minimum number of operations to do it.
The $\mathrm{MEX}$ of the array is equal to the minimum non-negative integer that is not in the array. For example, the $\mathrm{MEX}$ of the array $[3, 1, 0]$ is equal to $2$, and the array $[3, 3, 1, 4]$ is equal to $0$.
-----Input-----
The first line of input data contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input.
The descriptions of the test cases follow.
The first line of the description of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the array $a$.
The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le n$) — elements of the array $a$.
It is guaranteed that the sum of the values $n$ over all test cases in the test does not exceed $2\cdot10^5$.
-----Output-----
For each test case, output $n + 1$ integer — $i$-th number is equal to the minimum number of operations for which you can make the array $\mathrm{MEX}$ equal to $i$ ($0 \le i \le n$), or -1 if this cannot be done.
-----Examples-----
Input
5
3
0 1 3
7
0 1 2 3 4 3 2
4
3 0 0 0
7
4 6 2 3 5 0 5
5
4 0 1 0 4
Output
1 1 0 -1
1 1 2 2 1 0 2 6
3 0 1 4 3
1 0 -1 -1 -1 -1 -1 -1
2 1 0 2 -1 -1
-----Note-----
In the first set of example inputs, $n=3$:
to get $\mathrm{MEX}=0$, it is enough to perform one increment: $a_1$++;
to get $\mathrm{MEX}=1$, it is enough to perform one increment: $a_2$++;
$\mathrm{MEX}=2$ for a given array, so there is no need to perform increments;
it is impossible to get $\mathrm{MEX}=3$ by performing increments. | def solve():
n = int(input())
a = list(map(int, input().split()))
cnt = [(0) for i in range(n + 1)]
ans = []
a.sort()
for x in a:
cnt[x] += 1
ans.append(cnt[0])
s = 0
v = []
if cnt[0] == 0:
ans = [0]
for i in range(n):
ans.append(-1)
print(*ans)
return
if cnt[0] > 0:
for i in range(cnt[0] - 1):
v.append(0)
for i in range(n):
if cnt[i + 1] > 0:
ans.append(cnt[i + 1] + s)
for j in range(cnt[i + 1] - 1):
v.append(i + 1)
else:
ans.append(s)
if len(v) > 0:
s += i + 1 - v[-1]
v.pop(-1)
else:
break
ans += [-1] * (n + 1 - len(ans))
print(*ans)
t = 1
t = int(input())
for tc in range(t):
solve() | 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 VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR FOR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN IF VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Dmitry has an array of $n$ non-negative integers $a_1, a_2, \dots, a_n$.
In one operation, Dmitry can choose any index $j$ ($1 \le j \le n$) and increase the value of the element $a_j$ by $1$. He can choose the same index $j$ multiple times.
For each $i$ from $0$ to $n$, determine whether Dmitry can make the $\mathrm{MEX}$ of the array equal to exactly $i$. If it is possible, then determine the minimum number of operations to do it.
The $\mathrm{MEX}$ of the array is equal to the minimum non-negative integer that is not in the array. For example, the $\mathrm{MEX}$ of the array $[3, 1, 0]$ is equal to $2$, and the array $[3, 3, 1, 4]$ is equal to $0$.
-----Input-----
The first line of input data contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input.
The descriptions of the test cases follow.
The first line of the description of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the array $a$.
The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le n$) — elements of the array $a$.
It is guaranteed that the sum of the values $n$ over all test cases in the test does not exceed $2\cdot10^5$.
-----Output-----
For each test case, output $n + 1$ integer — $i$-th number is equal to the minimum number of operations for which you can make the array $\mathrm{MEX}$ equal to $i$ ($0 \le i \le n$), or -1 if this cannot be done.
-----Examples-----
Input
5
3
0 1 3
7
0 1 2 3 4 3 2
4
3 0 0 0
7
4 6 2 3 5 0 5
5
4 0 1 0 4
Output
1 1 0 -1
1 1 2 2 1 0 2 6
3 0 1 4 3
1 0 -1 -1 -1 -1 -1 -1
2 1 0 2 -1 -1
-----Note-----
In the first set of example inputs, $n=3$:
to get $\mathrm{MEX}=0$, it is enough to perform one increment: $a_1$++;
to get $\mathrm{MEX}=1$, it is enough to perform one increment: $a_2$++;
$\mathrm{MEX}=2$ for a given array, so there is no need to perform increments;
it is impossible to get $\mathrm{MEX}=3$ by performing increments. | def fun(i, j, memo, breakPoint):
for k in range(i, j):
if memo[k] >= 2:
breakPoint.append(k)
for _ in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
memo = {i: (0) for i in range(n + 2)}
for i in arr:
memo[i] += 1
mex = 0
for i in range(n + 1):
if memo[i] == 0:
mex = i
break
ans = []
breakPoint = [-1]
fun(0, mex, memo, breakPoint)
for i in range(n + 1):
if mex >= i:
ans.append(memo[i])
else:
bp = breakPoint[-1]
if bp != -1:
if memo[i]:
x = ans[mex]
x += mex - bp
x += memo[i]
ans.append(x)
else:
x = ans[mex]
x += mex - bp
memo[bp] -= 1
if memo[bp] == 1:
breakPoint.pop()
memo[mex] += 1
fun(mex, i, memo, breakPoint)
mex = i
memo[i] = 0
ans.append(x)
else:
ans.append(-1)
print(" ".join(map(str, ans))) | FUNC_DEF FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
Dmitry has an array of $n$ non-negative integers $a_1, a_2, \dots, a_n$.
In one operation, Dmitry can choose any index $j$ ($1 \le j \le n$) and increase the value of the element $a_j$ by $1$. He can choose the same index $j$ multiple times.
For each $i$ from $0$ to $n$, determine whether Dmitry can make the $\mathrm{MEX}$ of the array equal to exactly $i$. If it is possible, then determine the minimum number of operations to do it.
The $\mathrm{MEX}$ of the array is equal to the minimum non-negative integer that is not in the array. For example, the $\mathrm{MEX}$ of the array $[3, 1, 0]$ is equal to $2$, and the array $[3, 3, 1, 4]$ is equal to $0$.
-----Input-----
The first line of input data contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input.
The descriptions of the test cases follow.
The first line of the description of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the array $a$.
The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le n$) — elements of the array $a$.
It is guaranteed that the sum of the values $n$ over all test cases in the test does not exceed $2\cdot10^5$.
-----Output-----
For each test case, output $n + 1$ integer — $i$-th number is equal to the minimum number of operations for which you can make the array $\mathrm{MEX}$ equal to $i$ ($0 \le i \le n$), or -1 if this cannot be done.
-----Examples-----
Input
5
3
0 1 3
7
0 1 2 3 4 3 2
4
3 0 0 0
7
4 6 2 3 5 0 5
5
4 0 1 0 4
Output
1 1 0 -1
1 1 2 2 1 0 2 6
3 0 1 4 3
1 0 -1 -1 -1 -1 -1 -1
2 1 0 2 -1 -1
-----Note-----
In the first set of example inputs, $n=3$:
to get $\mathrm{MEX}=0$, it is enough to perform one increment: $a_1$++;
to get $\mathrm{MEX}=1$, it is enough to perform one increment: $a_2$++;
$\mathrm{MEX}=2$ for a given array, so there is no need to perform increments;
it is impossible to get $\mathrm{MEX}=3$ by performing increments. | def main():
test = int(input())
for idt in range(test):
n = int(input())
a = list(map(int, input().split()))
cnt = [0] * (n + 1)
for i in a:
cnt[i] += 1
tot = 0
stk = []
ans = [-1] * (n + 1)
for i in range(n + 1):
if tot == -1:
break
ans[i] = tot + cnt[i]
for _ in range(cnt[i]):
stk.append(i)
if len(stk) == 0:
tot = -1
else:
tot += i - stk.pop()
print(*ans, sep=" ")
return
main() | FUNC_DEF 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 BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING RETURN EXPR FUNC_CALL VAR |
Dmitry has an array of $n$ non-negative integers $a_1, a_2, \dots, a_n$.
In one operation, Dmitry can choose any index $j$ ($1 \le j \le n$) and increase the value of the element $a_j$ by $1$. He can choose the same index $j$ multiple times.
For each $i$ from $0$ to $n$, determine whether Dmitry can make the $\mathrm{MEX}$ of the array equal to exactly $i$. If it is possible, then determine the minimum number of operations to do it.
The $\mathrm{MEX}$ of the array is equal to the minimum non-negative integer that is not in the array. For example, the $\mathrm{MEX}$ of the array $[3, 1, 0]$ is equal to $2$, and the array $[3, 3, 1, 4]$ is equal to $0$.
-----Input-----
The first line of input data contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input.
The descriptions of the test cases follow.
The first line of the description of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the array $a$.
The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le n$) — elements of the array $a$.
It is guaranteed that the sum of the values $n$ over all test cases in the test does not exceed $2\cdot10^5$.
-----Output-----
For each test case, output $n + 1$ integer — $i$-th number is equal to the minimum number of operations for which you can make the array $\mathrm{MEX}$ equal to $i$ ($0 \le i \le n$), or -1 if this cannot be done.
-----Examples-----
Input
5
3
0 1 3
7
0 1 2 3 4 3 2
4
3 0 0 0
7
4 6 2 3 5 0 5
5
4 0 1 0 4
Output
1 1 0 -1
1 1 2 2 1 0 2 6
3 0 1 4 3
1 0 -1 -1 -1 -1 -1 -1
2 1 0 2 -1 -1
-----Note-----
In the first set of example inputs, $n=3$:
to get $\mathrm{MEX}=0$, it is enough to perform one increment: $a_1$++;
to get $\mathrm{MEX}=1$, it is enough to perform one increment: $a_2$++;
$\mathrm{MEX}=2$ for a given array, so there is no need to perform increments;
it is impossible to get $\mathrm{MEX}=3$ by performing increments. | from sys import stdin
input = stdin.readline
def answer():
d = dict()
for i in range(n):
d[a[i]] = d.get(a[i], 0) + 1
yes, m, extra = False, [], 0
for i in range(n + 1):
if yes == False:
print(d.get(i, 0) + extra, end=" ")
for j in range(d.get(i, 1) - 1):
m.append(i)
else:
print(-1, end=" ")
if d.get(i, 0) == 0:
if len(m) == 0:
yes = True
else:
extra += i - m.pop()
for T in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
answer()
print() | ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER STRING IF FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Dmitry has an array of $n$ non-negative integers $a_1, a_2, \dots, a_n$.
In one operation, Dmitry can choose any index $j$ ($1 \le j \le n$) and increase the value of the element $a_j$ by $1$. He can choose the same index $j$ multiple times.
For each $i$ from $0$ to $n$, determine whether Dmitry can make the $\mathrm{MEX}$ of the array equal to exactly $i$. If it is possible, then determine the minimum number of operations to do it.
The $\mathrm{MEX}$ of the array is equal to the minimum non-negative integer that is not in the array. For example, the $\mathrm{MEX}$ of the array $[3, 1, 0]$ is equal to $2$, and the array $[3, 3, 1, 4]$ is equal to $0$.
-----Input-----
The first line of input data contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input.
The descriptions of the test cases follow.
The first line of the description of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the array $a$.
The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le n$) — elements of the array $a$.
It is guaranteed that the sum of the values $n$ over all test cases in the test does not exceed $2\cdot10^5$.
-----Output-----
For each test case, output $n + 1$ integer — $i$-th number is equal to the minimum number of operations for which you can make the array $\mathrm{MEX}$ equal to $i$ ($0 \le i \le n$), or -1 if this cannot be done.
-----Examples-----
Input
5
3
0 1 3
7
0 1 2 3 4 3 2
4
3 0 0 0
7
4 6 2 3 5 0 5
5
4 0 1 0 4
Output
1 1 0 -1
1 1 2 2 1 0 2 6
3 0 1 4 3
1 0 -1 -1 -1 -1 -1 -1
2 1 0 2 -1 -1
-----Note-----
In the first set of example inputs, $n=3$:
to get $\mathrm{MEX}=0$, it is enough to perform one increment: $a_1$++;
to get $\mathrm{MEX}=1$, it is enough to perform one increment: $a_2$++;
$\mathrm{MEX}=2$ for a given array, so there is no need to perform increments;
it is impossible to get $\mathrm{MEX}=3$ by performing increments. | import sys
input = sys.stdin.readline
def solve():
n = int(input())
a = [int(i) for i in input().split()]
count = {}
for element in a:
if element in count.keys():
count[element] += 1
else:
count[element] = 1
ans = []
prev = 0
best = []
def get(i):
return 0 if i not in count.keys() else count[i]
for i in range(n + 1):
ans.append(prev + get(i))
if get(i) == 0:
if len(best) > 0:
have = best[-1]
count[have] -= 1
if get(have) == 0:
best.pop()
prev += i - have
count[i] = 1
else:
while len(ans) != n + 1:
ans.append(-1)
break
elif get(i) >= 2:
best.append(i)
count[i] -= 1
print(*ans)
for _ in range(int(input())):
solve() | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_DEF RETURN VAR FUNC_CALL VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER WHILE FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Dmitry has an array of $n$ non-negative integers $a_1, a_2, \dots, a_n$.
In one operation, Dmitry can choose any index $j$ ($1 \le j \le n$) and increase the value of the element $a_j$ by $1$. He can choose the same index $j$ multiple times.
For each $i$ from $0$ to $n$, determine whether Dmitry can make the $\mathrm{MEX}$ of the array equal to exactly $i$. If it is possible, then determine the minimum number of operations to do it.
The $\mathrm{MEX}$ of the array is equal to the minimum non-negative integer that is not in the array. For example, the $\mathrm{MEX}$ of the array $[3, 1, 0]$ is equal to $2$, and the array $[3, 3, 1, 4]$ is equal to $0$.
-----Input-----
The first line of input data contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input.
The descriptions of the test cases follow.
The first line of the description of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the array $a$.
The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le n$) — elements of the array $a$.
It is guaranteed that the sum of the values $n$ over all test cases in the test does not exceed $2\cdot10^5$.
-----Output-----
For each test case, output $n + 1$ integer — $i$-th number is equal to the minimum number of operations for which you can make the array $\mathrm{MEX}$ equal to $i$ ($0 \le i \le n$), or -1 if this cannot be done.
-----Examples-----
Input
5
3
0 1 3
7
0 1 2 3 4 3 2
4
3 0 0 0
7
4 6 2 3 5 0 5
5
4 0 1 0 4
Output
1 1 0 -1
1 1 2 2 1 0 2 6
3 0 1 4 3
1 0 -1 -1 -1 -1 -1 -1
2 1 0 2 -1 -1
-----Note-----
In the first set of example inputs, $n=3$:
to get $\mathrm{MEX}=0$, it is enough to perform one increment: $a_1$++;
to get $\mathrm{MEX}=1$, it is enough to perform one increment: $a_2$++;
$\mathrm{MEX}=2$ for a given array, so there is no need to perform increments;
it is impossible to get $\mathrm{MEX}=3$ by performing increments. | eps = 1e-09
t = int(input())
for t in range(t):
n = int(input())
a = list(map(int, input().split(" ")))
a = sorted(a)
cnt = [0] * (n + 1)
for x in a:
cnt[x] += 1
stk = []
j, tot = 0, 0
for i in range(n + 1):
while j < n and a[j] <= i - 1:
stk.append(a[j])
j += 1
if len(stk) == 0 and i > 0:
for j in range(i, n + 1):
print("-1", end=" ")
break
if len(stk) > 0:
tot += i - 1 - stk[-1]
stk.pop()
print(tot + cnt[i], end=" ")
print() | ASSIGN VAR NUMBER 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 STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING STRING IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR STRING EXPR FUNC_CALL VAR |
Dmitry has an array of $n$ non-negative integers $a_1, a_2, \dots, a_n$.
In one operation, Dmitry can choose any index $j$ ($1 \le j \le n$) and increase the value of the element $a_j$ by $1$. He can choose the same index $j$ multiple times.
For each $i$ from $0$ to $n$, determine whether Dmitry can make the $\mathrm{MEX}$ of the array equal to exactly $i$. If it is possible, then determine the minimum number of operations to do it.
The $\mathrm{MEX}$ of the array is equal to the minimum non-negative integer that is not in the array. For example, the $\mathrm{MEX}$ of the array $[3, 1, 0]$ is equal to $2$, and the array $[3, 3, 1, 4]$ is equal to $0$.
-----Input-----
The first line of input data contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input.
The descriptions of the test cases follow.
The first line of the description of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the array $a$.
The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le n$) — elements of the array $a$.
It is guaranteed that the sum of the values $n$ over all test cases in the test does not exceed $2\cdot10^5$.
-----Output-----
For each test case, output $n + 1$ integer — $i$-th number is equal to the minimum number of operations for which you can make the array $\mathrm{MEX}$ equal to $i$ ($0 \le i \le n$), or -1 if this cannot be done.
-----Examples-----
Input
5
3
0 1 3
7
0 1 2 3 4 3 2
4
3 0 0 0
7
4 6 2 3 5 0 5
5
4 0 1 0 4
Output
1 1 0 -1
1 1 2 2 1 0 2 6
3 0 1 4 3
1 0 -1 -1 -1 -1 -1 -1
2 1 0 2 -1 -1
-----Note-----
In the first set of example inputs, $n=3$:
to get $\mathrm{MEX}=0$, it is enough to perform one increment: $a_1$++;
to get $\mathrm{MEX}=1$, it is enough to perform one increment: $a_2$++;
$\mathrm{MEX}=2$ for a given array, so there is no need to perform increments;
it is impossible to get $\mathrm{MEX}=3$ by performing increments. | for _ in range(int(input())):
n = int(input())
a = [int(x) for x in input().split()]
a.sort()
a.append(10**9)
less = 0
equal = 0
unused = []
last = 0
for mex in range(n + 1):
while a[less + equal] < mex:
less += 1
while a[less + equal] == mex:
equal += 1
if less < mex or last < 0:
print("-1 " * (n + 1 - mex), end="")
break
print(equal + last, end=" ")
if not equal:
if not unused:
last = -1
else:
last += mex - unused.pop()
unused.extend([mex] * (equal - 1))
less += equal
equal = 0
print() | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP BIN_OP VAR NUMBER VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR STRING IF VAR IF VAR ASSIGN VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR |
Dmitry has an array of $n$ non-negative integers $a_1, a_2, \dots, a_n$.
In one operation, Dmitry can choose any index $j$ ($1 \le j \le n$) and increase the value of the element $a_j$ by $1$. He can choose the same index $j$ multiple times.
For each $i$ from $0$ to $n$, determine whether Dmitry can make the $\mathrm{MEX}$ of the array equal to exactly $i$. If it is possible, then determine the minimum number of operations to do it.
The $\mathrm{MEX}$ of the array is equal to the minimum non-negative integer that is not in the array. For example, the $\mathrm{MEX}$ of the array $[3, 1, 0]$ is equal to $2$, and the array $[3, 3, 1, 4]$ is equal to $0$.
-----Input-----
The first line of input data contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input.
The descriptions of the test cases follow.
The first line of the description of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the array $a$.
The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le n$) — elements of the array $a$.
It is guaranteed that the sum of the values $n$ over all test cases in the test does not exceed $2\cdot10^5$.
-----Output-----
For each test case, output $n + 1$ integer — $i$-th number is equal to the minimum number of operations for which you can make the array $\mathrm{MEX}$ equal to $i$ ($0 \le i \le n$), or -1 if this cannot be done.
-----Examples-----
Input
5
3
0 1 3
7
0 1 2 3 4 3 2
4
3 0 0 0
7
4 6 2 3 5 0 5
5
4 0 1 0 4
Output
1 1 0 -1
1 1 2 2 1 0 2 6
3 0 1 4 3
1 0 -1 -1 -1 -1 -1 -1
2 1 0 2 -1 -1
-----Note-----
In the first set of example inputs, $n=3$:
to get $\mathrm{MEX}=0$, it is enough to perform one increment: $a_1$++;
to get $\mathrm{MEX}=1$, it is enough to perform one increment: $a_2$++;
$\mathrm{MEX}=2$ for a given array, so there is no need to perform increments;
it is impossible to get $\mathrm{MEX}=3$ by performing increments. | result = []
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
array = [(0) for i in range(n + 1)]
for number in a:
array[number] += 1
deque = [0] * max(array[0] - 1, 0)
dlen = len(deque)
answer = [array[0]]
for i in range(1, n + 1):
if answer[i - 1] == -1:
answer.append(-1)
elif array[i - 1] != 0:
answer.append(answer[i - 1] - array[i - 1] + array[i])
elif dlen > 0:
answer.append(answer[i - 1] + (i - 1 - deque.pop()) + array[i])
dlen -= 1
else:
answer.append(-1)
deque += [i] * max(array[i] - 1, 0)
dlen += max(array[i] - 1, 0)
result.append(answer)
print("\n".join(map(lambda x: " ".join(map(str, x)), result))) | ASSIGN VAR LIST 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 VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR BIN_OP LIST VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
Dmitry has an array of $n$ non-negative integers $a_1, a_2, \dots, a_n$.
In one operation, Dmitry can choose any index $j$ ($1 \le j \le n$) and increase the value of the element $a_j$ by $1$. He can choose the same index $j$ multiple times.
For each $i$ from $0$ to $n$, determine whether Dmitry can make the $\mathrm{MEX}$ of the array equal to exactly $i$. If it is possible, then determine the minimum number of operations to do it.
The $\mathrm{MEX}$ of the array is equal to the minimum non-negative integer that is not in the array. For example, the $\mathrm{MEX}$ of the array $[3, 1, 0]$ is equal to $2$, and the array $[3, 3, 1, 4]$ is equal to $0$.
-----Input-----
The first line of input data contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input.
The descriptions of the test cases follow.
The first line of the description of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the array $a$.
The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le n$) — elements of the array $a$.
It is guaranteed that the sum of the values $n$ over all test cases in the test does not exceed $2\cdot10^5$.
-----Output-----
For each test case, output $n + 1$ integer — $i$-th number is equal to the minimum number of operations for which you can make the array $\mathrm{MEX}$ equal to $i$ ($0 \le i \le n$), or -1 if this cannot be done.
-----Examples-----
Input
5
3
0 1 3
7
0 1 2 3 4 3 2
4
3 0 0 0
7
4 6 2 3 5 0 5
5
4 0 1 0 4
Output
1 1 0 -1
1 1 2 2 1 0 2 6
3 0 1 4 3
1 0 -1 -1 -1 -1 -1 -1
2 1 0 2 -1 -1
-----Note-----
In the first set of example inputs, $n=3$:
to get $\mathrm{MEX}=0$, it is enough to perform one increment: $a_1$++;
to get $\mathrm{MEX}=1$, it is enough to perform one increment: $a_2$++;
$\mathrm{MEX}=2$ for a given array, so there is no need to perform increments;
it is impossible to get $\mathrm{MEX}=3$ by performing increments. | t = int(input())
while t:
n = int(input())
li = list(map(int, input().split()))
li.sort()
ans = [-1] * (n + 1)
d = [0] * (n + 1)
stack = []
s = 0
for i in range(len(li)):
d[li[i]] += 1
for i in range(n + 1):
if i > 0 and d[i - 1] == 0:
if not stack:
break
j = stack.pop()
s += i - j - 1
ans[i] = s + d[i]
while i > 0 and d[i - 1] > 1:
d[i - 1] -= 1
stack.append(i - 1)
print(" ".join(map(str, ans)))
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR WHILE VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER |
Dmitry has an array of $n$ non-negative integers $a_1, a_2, \dots, a_n$.
In one operation, Dmitry can choose any index $j$ ($1 \le j \le n$) and increase the value of the element $a_j$ by $1$. He can choose the same index $j$ multiple times.
For each $i$ from $0$ to $n$, determine whether Dmitry can make the $\mathrm{MEX}$ of the array equal to exactly $i$. If it is possible, then determine the minimum number of operations to do it.
The $\mathrm{MEX}$ of the array is equal to the minimum non-negative integer that is not in the array. For example, the $\mathrm{MEX}$ of the array $[3, 1, 0]$ is equal to $2$, and the array $[3, 3, 1, 4]$ is equal to $0$.
-----Input-----
The first line of input data contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input.
The descriptions of the test cases follow.
The first line of the description of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the array $a$.
The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le n$) — elements of the array $a$.
It is guaranteed that the sum of the values $n$ over all test cases in the test does not exceed $2\cdot10^5$.
-----Output-----
For each test case, output $n + 1$ integer — $i$-th number is equal to the minimum number of operations for which you can make the array $\mathrm{MEX}$ equal to $i$ ($0 \le i \le n$), or -1 if this cannot be done.
-----Examples-----
Input
5
3
0 1 3
7
0 1 2 3 4 3 2
4
3 0 0 0
7
4 6 2 3 5 0 5
5
4 0 1 0 4
Output
1 1 0 -1
1 1 2 2 1 0 2 6
3 0 1 4 3
1 0 -1 -1 -1 -1 -1 -1
2 1 0 2 -1 -1
-----Note-----
In the first set of example inputs, $n=3$:
to get $\mathrm{MEX}=0$, it is enough to perform one increment: $a_1$++;
to get $\mathrm{MEX}=1$, it is enough to perform one increment: $a_2$++;
$\mathrm{MEX}=2$ for a given array, so there is no need to perform increments;
it is impossible to get $\mathrm{MEX}=3$ by performing increments. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
l.sort()
extra = []
moves = 0
ans = [(-1) for i in range(n + 1)]
j = 0
for i in range(n + 1):
now = 0
f = 0
while j < n and l[j] == i:
extra.append(l[j])
now += 1
j += 1
if now > 0:
ans[i] = now + moves
extra.pop()
else:
ans[i] = now + moves
if len(extra) > 0:
moves = moves + (i - extra.pop())
else:
f = 1
if f:
break
print(*ans) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR |
Dmitry has an array of $n$ non-negative integers $a_1, a_2, \dots, a_n$.
In one operation, Dmitry can choose any index $j$ ($1 \le j \le n$) and increase the value of the element $a_j$ by $1$. He can choose the same index $j$ multiple times.
For each $i$ from $0$ to $n$, determine whether Dmitry can make the $\mathrm{MEX}$ of the array equal to exactly $i$. If it is possible, then determine the minimum number of operations to do it.
The $\mathrm{MEX}$ of the array is equal to the minimum non-negative integer that is not in the array. For example, the $\mathrm{MEX}$ of the array $[3, 1, 0]$ is equal to $2$, and the array $[3, 3, 1, 4]$ is equal to $0$.
-----Input-----
The first line of input data contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input.
The descriptions of the test cases follow.
The first line of the description of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the array $a$.
The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le n$) — elements of the array $a$.
It is guaranteed that the sum of the values $n$ over all test cases in the test does not exceed $2\cdot10^5$.
-----Output-----
For each test case, output $n + 1$ integer — $i$-th number is equal to the minimum number of operations for which you can make the array $\mathrm{MEX}$ equal to $i$ ($0 \le i \le n$), or -1 if this cannot be done.
-----Examples-----
Input
5
3
0 1 3
7
0 1 2 3 4 3 2
4
3 0 0 0
7
4 6 2 3 5 0 5
5
4 0 1 0 4
Output
1 1 0 -1
1 1 2 2 1 0 2 6
3 0 1 4 3
1 0 -1 -1 -1 -1 -1 -1
2 1 0 2 -1 -1
-----Note-----
In the first set of example inputs, $n=3$:
to get $\mathrm{MEX}=0$, it is enough to perform one increment: $a_1$++;
to get $\mathrm{MEX}=1$, it is enough to perform one increment: $a_2$++;
$\mathrm{MEX}=2$ for a given array, so there is no need to perform increments;
it is impossible to get $\mathrm{MEX}=3$ by performing increments. | t = int(input())
for _ in range(t):
n = int(input())
cot = [0] * (n + 1)
s = input().split()
nums = [int(x) for x in s]
for num in nums:
cot[num] += 1
pre = 0
cur = 0
v = []
ans = []
for i in range(n + 1):
if pre < i:
ans.append(-1)
while len(ans) < n + 1:
ans.append(-1)
break
else:
ans.append(cur + cot[i])
if cot[i] == 0 and len(v) > 0:
cur += i - v[-1]
v.pop()
else:
j = 1
while j <= cot[i] - 1:
v.append(i)
j += 1
pre += cot[i]
res = " ".join([str(a) for a in ans])
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER WHILE FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Dmitry has an array of $n$ non-negative integers $a_1, a_2, \dots, a_n$.
In one operation, Dmitry can choose any index $j$ ($1 \le j \le n$) and increase the value of the element $a_j$ by $1$. He can choose the same index $j$ multiple times.
For each $i$ from $0$ to $n$, determine whether Dmitry can make the $\mathrm{MEX}$ of the array equal to exactly $i$. If it is possible, then determine the minimum number of operations to do it.
The $\mathrm{MEX}$ of the array is equal to the minimum non-negative integer that is not in the array. For example, the $\mathrm{MEX}$ of the array $[3, 1, 0]$ is equal to $2$, and the array $[3, 3, 1, 4]$ is equal to $0$.
-----Input-----
The first line of input data contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input.
The descriptions of the test cases follow.
The first line of the description of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the array $a$.
The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le n$) — elements of the array $a$.
It is guaranteed that the sum of the values $n$ over all test cases in the test does not exceed $2\cdot10^5$.
-----Output-----
For each test case, output $n + 1$ integer — $i$-th number is equal to the minimum number of operations for which you can make the array $\mathrm{MEX}$ equal to $i$ ($0 \le i \le n$), or -1 if this cannot be done.
-----Examples-----
Input
5
3
0 1 3
7
0 1 2 3 4 3 2
4
3 0 0 0
7
4 6 2 3 5 0 5
5
4 0 1 0 4
Output
1 1 0 -1
1 1 2 2 1 0 2 6
3 0 1 4 3
1 0 -1 -1 -1 -1 -1 -1
2 1 0 2 -1 -1
-----Note-----
In the first set of example inputs, $n=3$:
to get $\mathrm{MEX}=0$, it is enough to perform one increment: $a_1$++;
to get $\mathrm{MEX}=1$, it is enough to perform one increment: $a_2$++;
$\mathrm{MEX}=2$ for a given array, so there is no need to perform increments;
it is impossible to get $\mathrm{MEX}=3$ by performing increments. | def solve():
n = int(input())
a = list(map(int, input().split()))
d = {}
for item in a:
if item in d:
d[item] += 1
else:
d[item] = 1
for i in range(n + 1):
if i not in d:
d[i] = 0
extras = []
tobeadded = 0
for i in range(n + 1):
if d[i] == 0:
print(0 + tobeadded, end=" ")
if extras == []:
break
k = extras.pop()
tobeadded += i - k
else:
print(d[i] + tobeadded, end=" ")
if d[i] > 1:
extras += [i] * (d[i] - 1)
print("-1 " * (n - i), end="")
print()
for _ in range(int(input())):
solve() | 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 DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR STRING IF VAR LIST ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR STRING IF VAR VAR NUMBER VAR BIN_OP LIST VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Dmitry has an array of $n$ non-negative integers $a_1, a_2, \dots, a_n$.
In one operation, Dmitry can choose any index $j$ ($1 \le j \le n$) and increase the value of the element $a_j$ by $1$. He can choose the same index $j$ multiple times.
For each $i$ from $0$ to $n$, determine whether Dmitry can make the $\mathrm{MEX}$ of the array equal to exactly $i$. If it is possible, then determine the minimum number of operations to do it.
The $\mathrm{MEX}$ of the array is equal to the minimum non-negative integer that is not in the array. For example, the $\mathrm{MEX}$ of the array $[3, 1, 0]$ is equal to $2$, and the array $[3, 3, 1, 4]$ is equal to $0$.
-----Input-----
The first line of input data contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input.
The descriptions of the test cases follow.
The first line of the description of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the array $a$.
The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le n$) — elements of the array $a$.
It is guaranteed that the sum of the values $n$ over all test cases in the test does not exceed $2\cdot10^5$.
-----Output-----
For each test case, output $n + 1$ integer — $i$-th number is equal to the minimum number of operations for which you can make the array $\mathrm{MEX}$ equal to $i$ ($0 \le i \le n$), or -1 if this cannot be done.
-----Examples-----
Input
5
3
0 1 3
7
0 1 2 3 4 3 2
4
3 0 0 0
7
4 6 2 3 5 0 5
5
4 0 1 0 4
Output
1 1 0 -1
1 1 2 2 1 0 2 6
3 0 1 4 3
1 0 -1 -1 -1 -1 -1 -1
2 1 0 2 -1 -1
-----Note-----
In the first set of example inputs, $n=3$:
to get $\mathrm{MEX}=0$, it is enough to perform one increment: $a_1$++;
to get $\mathrm{MEX}=1$, it is enough to perform one increment: $a_2$++;
$\mathrm{MEX}=2$ for a given array, so there is no need to perform increments;
it is impossible to get $\mathrm{MEX}=3$ by performing increments. | def solve():
n = int(input())
a = map(int, input().split())
fre = [0] * (n + 1)
for x in a:
fre[x] += 1
if fre[0] == 0:
ans = [-1] * (n + 1)
ans[0] = 0
print(*ans)
return
ans = [-1] * (n + 1)
ans[0] = fre[0]
stk = []
lagbe = 0
cost = 0
for i in range(n + 1):
ans[i] = cost + fre[i]
if fre[i]:
for j in range(1, fre[i]):
stk.append(i)
elif len(stk) == 0:
break
else:
cost += i - stk.pop()
print(*ans)
for _ in range(int(input())):
solve() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Dmitry has an array of $n$ non-negative integers $a_1, a_2, \dots, a_n$.
In one operation, Dmitry can choose any index $j$ ($1 \le j \le n$) and increase the value of the element $a_j$ by $1$. He can choose the same index $j$ multiple times.
For each $i$ from $0$ to $n$, determine whether Dmitry can make the $\mathrm{MEX}$ of the array equal to exactly $i$. If it is possible, then determine the minimum number of operations to do it.
The $\mathrm{MEX}$ of the array is equal to the minimum non-negative integer that is not in the array. For example, the $\mathrm{MEX}$ of the array $[3, 1, 0]$ is equal to $2$, and the array $[3, 3, 1, 4]$ is equal to $0$.
-----Input-----
The first line of input data contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input.
The descriptions of the test cases follow.
The first line of the description of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the array $a$.
The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le n$) — elements of the array $a$.
It is guaranteed that the sum of the values $n$ over all test cases in the test does not exceed $2\cdot10^5$.
-----Output-----
For each test case, output $n + 1$ integer — $i$-th number is equal to the minimum number of operations for which you can make the array $\mathrm{MEX}$ equal to $i$ ($0 \le i \le n$), or -1 if this cannot be done.
-----Examples-----
Input
5
3
0 1 3
7
0 1 2 3 4 3 2
4
3 0 0 0
7
4 6 2 3 5 0 5
5
4 0 1 0 4
Output
1 1 0 -1
1 1 2 2 1 0 2 6
3 0 1 4 3
1 0 -1 -1 -1 -1 -1 -1
2 1 0 2 -1 -1
-----Note-----
In the first set of example inputs, $n=3$:
to get $\mathrm{MEX}=0$, it is enough to perform one increment: $a_1$++;
to get $\mathrm{MEX}=1$, it is enough to perform one increment: $a_2$++;
$\mathrm{MEX}=2$ for a given array, so there is no need to perform increments;
it is impossible to get $\mathrm{MEX}=3$ by performing increments. | t = int(input())
out = ""
for _ in range(t):
n = int(input())
arr = [int(x) for x in input().split()]
count = [0] * (n + 1)
for x in arr:
count[x] += 1
previous = []
total = 0
this_ = 0
for i in range(n + 1):
out += str(total + count[i]) + " "
for x in range(count[i]):
previous.append(i)
if len(previous) == 0:
while i < n:
out += "-1 "
i += 1
break
total += i - previous.pop(-1)
out += "\n"
print(out) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER WHILE VAR VAR VAR STRING VAR NUMBER VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR STRING EXPR FUNC_CALL VAR VAR |
Dmitry has an array of $n$ non-negative integers $a_1, a_2, \dots, a_n$.
In one operation, Dmitry can choose any index $j$ ($1 \le j \le n$) and increase the value of the element $a_j$ by $1$. He can choose the same index $j$ multiple times.
For each $i$ from $0$ to $n$, determine whether Dmitry can make the $\mathrm{MEX}$ of the array equal to exactly $i$. If it is possible, then determine the minimum number of operations to do it.
The $\mathrm{MEX}$ of the array is equal to the minimum non-negative integer that is not in the array. For example, the $\mathrm{MEX}$ of the array $[3, 1, 0]$ is equal to $2$, and the array $[3, 3, 1, 4]$ is equal to $0$.
-----Input-----
The first line of input data contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input.
The descriptions of the test cases follow.
The first line of the description of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the array $a$.
The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le n$) — elements of the array $a$.
It is guaranteed that the sum of the values $n$ over all test cases in the test does not exceed $2\cdot10^5$.
-----Output-----
For each test case, output $n + 1$ integer — $i$-th number is equal to the minimum number of operations for which you can make the array $\mathrm{MEX}$ equal to $i$ ($0 \le i \le n$), or -1 if this cannot be done.
-----Examples-----
Input
5
3
0 1 3
7
0 1 2 3 4 3 2
4
3 0 0 0
7
4 6 2 3 5 0 5
5
4 0 1 0 4
Output
1 1 0 -1
1 1 2 2 1 0 2 6
3 0 1 4 3
1 0 -1 -1 -1 -1 -1 -1
2 1 0 2 -1 -1
-----Note-----
In the first set of example inputs, $n=3$:
to get $\mathrm{MEX}=0$, it is enough to perform one increment: $a_1$++;
to get $\mathrm{MEX}=1$, it is enough to perform one increment: $a_2$++;
$\mathrm{MEX}=2$ for a given array, so there is no need to perform increments;
it is impossible to get $\mathrm{MEX}=3$ by performing increments. | import sys
t = int(input())
for _ in range(t):
n = int(input())
d = {i: (0) for i in range(0, n + 1)}
m = list(map(int, sys.stdin.readline().split()))
ans = 0
ev = []
key = True
for i in m:
d[i] += 1
if d[0] == 0:
print("0", end=" ")
key = False
else:
print(ans + d[0], end=" ")
for j in range(d[0] - 1):
ev.append(0)
for i in range(1, n + 1):
if key == True:
if d[i - 1] > 0:
if i in d:
print(ans + d[i], end=" ")
for j in range(d[i] - 1):
ev.append(i)
else:
print(ans, end=" ")
elif ev and key == True:
ans += i - 1 - ev.pop()
if i in d:
print(ans + d[i], end=" ")
for j in range(d[i] - 1):
ev.append(i)
else:
print(ans)
else:
print("-1", end=" ")
key = False
else:
print("-1", end=" ")
print("") | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING IF VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR STRING |
Dmitry has an array of $n$ non-negative integers $a_1, a_2, \dots, a_n$.
In one operation, Dmitry can choose any index $j$ ($1 \le j \le n$) and increase the value of the element $a_j$ by $1$. He can choose the same index $j$ multiple times.
For each $i$ from $0$ to $n$, determine whether Dmitry can make the $\mathrm{MEX}$ of the array equal to exactly $i$. If it is possible, then determine the minimum number of operations to do it.
The $\mathrm{MEX}$ of the array is equal to the minimum non-negative integer that is not in the array. For example, the $\mathrm{MEX}$ of the array $[3, 1, 0]$ is equal to $2$, and the array $[3, 3, 1, 4]$ is equal to $0$.
-----Input-----
The first line of input data contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input.
The descriptions of the test cases follow.
The first line of the description of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the array $a$.
The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le n$) — elements of the array $a$.
It is guaranteed that the sum of the values $n$ over all test cases in the test does not exceed $2\cdot10^5$.
-----Output-----
For each test case, output $n + 1$ integer — $i$-th number is equal to the minimum number of operations for which you can make the array $\mathrm{MEX}$ equal to $i$ ($0 \le i \le n$), or -1 if this cannot be done.
-----Examples-----
Input
5
3
0 1 3
7
0 1 2 3 4 3 2
4
3 0 0 0
7
4 6 2 3 5 0 5
5
4 0 1 0 4
Output
1 1 0 -1
1 1 2 2 1 0 2 6
3 0 1 4 3
1 0 -1 -1 -1 -1 -1 -1
2 1 0 2 -1 -1
-----Note-----
In the first set of example inputs, $n=3$:
to get $\mathrm{MEX}=0$, it is enough to perform one increment: $a_1$++;
to get $\mathrm{MEX}=1$, it is enough to perform one increment: $a_2$++;
$\mathrm{MEX}=2$ for a given array, so there is no need to perform increments;
it is impossible to get $\mathrm{MEX}=3$ by performing increments. | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
ls = [0] * (n + 1)
cnt = 0
for i in a:
ls[i] += 1
for i in range(n + 1):
if ls[i] > 1:
cnt += ls[i]
cnt -= 1
elif ls[i] == 0 and cnt > 0:
cnt -= 1
elif ls[i] == 0 and cnt == 0:
ls[i] = -1
break
ind = n + 1
for i in range(n + 1):
if ls[i] == -1:
ind = i
for j in range(i + 1, n + 1):
ls[j] = -1
break
xx = []
xxx = {}
j = 0
for i in range(ind):
if ls[i] > 1:
xx.append([ls[i] - 1, i])
j += 1
elif ls[i] == 0:
xxx[i] = xx[j - 1][1]
xx[j - 1][0] -= 1
if xx[j - 1][0] == 0:
xx.pop()
j -= 1
ls[ind] = 0
ans = 0
for i in range(n + 1):
if ls[i] == 0:
print(ans, end=" ")
if i != ind:
ans += i - xxx[i]
j += 1
elif ls[i] != -1:
print(ans + ls[i], end=" ")
else:
print(ls[i], end=" ")
print() | 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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING IF VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR |
Dmitry has an array of $n$ non-negative integers $a_1, a_2, \dots, a_n$.
In one operation, Dmitry can choose any index $j$ ($1 \le j \le n$) and increase the value of the element $a_j$ by $1$. He can choose the same index $j$ multiple times.
For each $i$ from $0$ to $n$, determine whether Dmitry can make the $\mathrm{MEX}$ of the array equal to exactly $i$. If it is possible, then determine the minimum number of operations to do it.
The $\mathrm{MEX}$ of the array is equal to the minimum non-negative integer that is not in the array. For example, the $\mathrm{MEX}$ of the array $[3, 1, 0]$ is equal to $2$, and the array $[3, 3, 1, 4]$ is equal to $0$.
-----Input-----
The first line of input data contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input.
The descriptions of the test cases follow.
The first line of the description of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the array $a$.
The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le n$) — elements of the array $a$.
It is guaranteed that the sum of the values $n$ over all test cases in the test does not exceed $2\cdot10^5$.
-----Output-----
For each test case, output $n + 1$ integer — $i$-th number is equal to the minimum number of operations for which you can make the array $\mathrm{MEX}$ equal to $i$ ($0 \le i \le n$), or -1 if this cannot be done.
-----Examples-----
Input
5
3
0 1 3
7
0 1 2 3 4 3 2
4
3 0 0 0
7
4 6 2 3 5 0 5
5
4 0 1 0 4
Output
1 1 0 -1
1 1 2 2 1 0 2 6
3 0 1 4 3
1 0 -1 -1 -1 -1 -1 -1
2 1 0 2 -1 -1
-----Note-----
In the first set of example inputs, $n=3$:
to get $\mathrm{MEX}=0$, it is enough to perform one increment: $a_1$++;
to get $\mathrm{MEX}=1$, it is enough to perform one increment: $a_2$++;
$\mathrm{MEX}=2$ for a given array, so there is no need to perform increments;
it is impossible to get $\mathrm{MEX}=3$ by performing increments. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
fr = [(0) for i in range(n + 1)]
for i in a:
fr[i] += 1
if fr[0] == 0:
print(*([0] + [(-1) for i in range(n)]))
continue
else:
ans = [fr[0]]
st = [(0) for i in range(fr[0] - 1)]
work = 0
for i in range(1, n + 1):
ans.append(fr[i] + work)
if fr[i] == 0:
if not st:
ans += [-1] * (n - i)
break
pot = st.pop(-1)
fr[i] = 1
work += i - pot
else:
for j in range(fr[i] - 1):
st.append(i)
print(*ans) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR BIN_OP LIST NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Dmitry has an array of $n$ non-negative integers $a_1, a_2, \dots, a_n$.
In one operation, Dmitry can choose any index $j$ ($1 \le j \le n$) and increase the value of the element $a_j$ by $1$. He can choose the same index $j$ multiple times.
For each $i$ from $0$ to $n$, determine whether Dmitry can make the $\mathrm{MEX}$ of the array equal to exactly $i$. If it is possible, then determine the minimum number of operations to do it.
The $\mathrm{MEX}$ of the array is equal to the minimum non-negative integer that is not in the array. For example, the $\mathrm{MEX}$ of the array $[3, 1, 0]$ is equal to $2$, and the array $[3, 3, 1, 4]$ is equal to $0$.
-----Input-----
The first line of input data contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input.
The descriptions of the test cases follow.
The first line of the description of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the array $a$.
The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le n$) — elements of the array $a$.
It is guaranteed that the sum of the values $n$ over all test cases in the test does not exceed $2\cdot10^5$.
-----Output-----
For each test case, output $n + 1$ integer — $i$-th number is equal to the minimum number of operations for which you can make the array $\mathrm{MEX}$ equal to $i$ ($0 \le i \le n$), or -1 if this cannot be done.
-----Examples-----
Input
5
3
0 1 3
7
0 1 2 3 4 3 2
4
3 0 0 0
7
4 6 2 3 5 0 5
5
4 0 1 0 4
Output
1 1 0 -1
1 1 2 2 1 0 2 6
3 0 1 4 3
1 0 -1 -1 -1 -1 -1 -1
2 1 0 2 -1 -1
-----Note-----
In the first set of example inputs, $n=3$:
to get $\mathrm{MEX}=0$, it is enough to perform one increment: $a_1$++;
to get $\mathrm{MEX}=1$, it is enough to perform one increment: $a_2$++;
$\mathrm{MEX}=2$ for a given array, so there is no need to perform increments;
it is impossible to get $\mathrm{MEX}=3$ by performing increments. | def solve(arr, n):
count = [(0) for _ in range(n + 1)]
for i in arr:
count[i] += 1
ans = [(-1) for _ in range(n + 1)]
ans[0] = count[0]
if not ans[0]:
return ans
steps = 0
last = []
if ans[0] > 1:
last.append(0)
for i in range(1, n + 1):
if arr[i - 1] >= i:
continue
ans[i] = count[i] + steps
if count[i] > 1:
last.append(i)
if not count[i]:
if not last:
return ans
steps += i - last[-1]
count[last[-1]] -= 1
if count[last[-1]] == 1:
last.pop()
return ans
t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int, input().split()))
arr.sort()
print(*solve(arr, n)) | FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR LIST IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR IF VAR RETURN VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR RETURN VAR 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 EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Dmitry has an array of $n$ non-negative integers $a_1, a_2, \dots, a_n$.
In one operation, Dmitry can choose any index $j$ ($1 \le j \le n$) and increase the value of the element $a_j$ by $1$. He can choose the same index $j$ multiple times.
For each $i$ from $0$ to $n$, determine whether Dmitry can make the $\mathrm{MEX}$ of the array equal to exactly $i$. If it is possible, then determine the minimum number of operations to do it.
The $\mathrm{MEX}$ of the array is equal to the minimum non-negative integer that is not in the array. For example, the $\mathrm{MEX}$ of the array $[3, 1, 0]$ is equal to $2$, and the array $[3, 3, 1, 4]$ is equal to $0$.
-----Input-----
The first line of input data contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input.
The descriptions of the test cases follow.
The first line of the description of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the array $a$.
The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le n$) — elements of the array $a$.
It is guaranteed that the sum of the values $n$ over all test cases in the test does not exceed $2\cdot10^5$.
-----Output-----
For each test case, output $n + 1$ integer — $i$-th number is equal to the minimum number of operations for which you can make the array $\mathrm{MEX}$ equal to $i$ ($0 \le i \le n$), or -1 if this cannot be done.
-----Examples-----
Input
5
3
0 1 3
7
0 1 2 3 4 3 2
4
3 0 0 0
7
4 6 2 3 5 0 5
5
4 0 1 0 4
Output
1 1 0 -1
1 1 2 2 1 0 2 6
3 0 1 4 3
1 0 -1 -1 -1 -1 -1 -1
2 1 0 2 -1 -1
-----Note-----
In the first set of example inputs, $n=3$:
to get $\mathrm{MEX}=0$, it is enough to perform one increment: $a_1$++;
to get $\mathrm{MEX}=1$, it is enough to perform one increment: $a_2$++;
$\mathrm{MEX}=2$ for a given array, so there is no need to perform increments;
it is impossible to get $\mathrm{MEX}=3$ by performing increments. | import sys
I = lambda: [*map(int, sys.stdin.readline().split())]
(t,) = I()
for _ in range(t):
(n,) = I()
a = I()
count = [0] * (n + 1)
for guy in a:
count[guy] += 1
cum = [count[0]]
for i in range(n):
cum.append(cum[-1] + count[i + 1])
tot = [0]
for i in range(n):
tot.append(tot[-1] + count[i + 1] * (i + 1))
counts = [count[0]]
point = -1 if count[0] <= 1 else 0
cover = [-1 if count[0] == 0 else 0]
for i in range(n):
if cover[i] == -1:
cover.append(-1)
elif count[i + 1] > 1:
cover.append(cover[-1])
point = i + 1
counts.append(count[i + 1])
elif count[i + 1] == 1:
cover.append(cover[-1])
counts.append(count[i + 1])
elif point < 0:
cover.append(-1)
else:
cover.append(cover[-1] + i + 1 - point)
counts.append(1)
counts[point] -= 1
while counts[point] <= 1 and point >= 0:
point -= 1
out = [count[0]]
for i in range(n):
if cover[i] == -1:
out.append(-1)
else:
out.append(cover[i] + count[i + 1])
print(" ".join(str(guy) for guy in out)) | IMPORT ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
Dmitry has an array of $n$ non-negative integers $a_1, a_2, \dots, a_n$.
In one operation, Dmitry can choose any index $j$ ($1 \le j \le n$) and increase the value of the element $a_j$ by $1$. He can choose the same index $j$ multiple times.
For each $i$ from $0$ to $n$, determine whether Dmitry can make the $\mathrm{MEX}$ of the array equal to exactly $i$. If it is possible, then determine the minimum number of operations to do it.
The $\mathrm{MEX}$ of the array is equal to the minimum non-negative integer that is not in the array. For example, the $\mathrm{MEX}$ of the array $[3, 1, 0]$ is equal to $2$, and the array $[3, 3, 1, 4]$ is equal to $0$.
-----Input-----
The first line of input data contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input.
The descriptions of the test cases follow.
The first line of the description of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the array $a$.
The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le n$) — elements of the array $a$.
It is guaranteed that the sum of the values $n$ over all test cases in the test does not exceed $2\cdot10^5$.
-----Output-----
For each test case, output $n + 1$ integer — $i$-th number is equal to the minimum number of operations for which you can make the array $\mathrm{MEX}$ equal to $i$ ($0 \le i \le n$), or -1 if this cannot be done.
-----Examples-----
Input
5
3
0 1 3
7
0 1 2 3 4 3 2
4
3 0 0 0
7
4 6 2 3 5 0 5
5
4 0 1 0 4
Output
1 1 0 -1
1 1 2 2 1 0 2 6
3 0 1 4 3
1 0 -1 -1 -1 -1 -1 -1
2 1 0 2 -1 -1
-----Note-----
In the first set of example inputs, $n=3$:
to get $\mathrm{MEX}=0$, it is enough to perform one increment: $a_1$++;
to get $\mathrm{MEX}=1$, it is enough to perform one increment: $a_2$++;
$\mathrm{MEX}=2$ for a given array, so there is no need to perform increments;
it is impossible to get $\mathrm{MEX}=3$ by performing increments. | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
rem = []
out = []
have = []
s = 0
ans = 0
skip = False
f = [0] * (n + 1)
for i in a:
f[i] += 1
for i in range(n + 1):
if skip:
out.append(-1)
continue
ans = s + f[i]
if len(rem) > 0:
ans = -1
if ans == -1:
skip = True
out.append(ans)
if f[i] == 0:
rem.append(i)
else:
while f[i] > 1:
have.append(i)
f[i] -= 1
while len(rem) > 0 and len(have) > 0:
while len(have) > 0 and have[-1] > rem[-1]:
have.pop()
if len(have) == 0:
break
x = rem[-1]
rem.pop()
y = have[-1]
have.pop()
s += x - y
print(*out) | 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 LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Dmitry has an array of $n$ non-negative integers $a_1, a_2, \dots, a_n$.
In one operation, Dmitry can choose any index $j$ ($1 \le j \le n$) and increase the value of the element $a_j$ by $1$. He can choose the same index $j$ multiple times.
For each $i$ from $0$ to $n$, determine whether Dmitry can make the $\mathrm{MEX}$ of the array equal to exactly $i$. If it is possible, then determine the minimum number of operations to do it.
The $\mathrm{MEX}$ of the array is equal to the minimum non-negative integer that is not in the array. For example, the $\mathrm{MEX}$ of the array $[3, 1, 0]$ is equal to $2$, and the array $[3, 3, 1, 4]$ is equal to $0$.
-----Input-----
The first line of input data contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases in the input.
The descriptions of the test cases follow.
The first line of the description of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the array $a$.
The second line of the description of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le n$) — elements of the array $a$.
It is guaranteed that the sum of the values $n$ over all test cases in the test does not exceed $2\cdot10^5$.
-----Output-----
For each test case, output $n + 1$ integer — $i$-th number is equal to the minimum number of operations for which you can make the array $\mathrm{MEX}$ equal to $i$ ($0 \le i \le n$), or -1 if this cannot be done.
-----Examples-----
Input
5
3
0 1 3
7
0 1 2 3 4 3 2
4
3 0 0 0
7
4 6 2 3 5 0 5
5
4 0 1 0 4
Output
1 1 0 -1
1 1 2 2 1 0 2 6
3 0 1 4 3
1 0 -1 -1 -1 -1 -1 -1
2 1 0 2 -1 -1
-----Note-----
In the first set of example inputs, $n=3$:
to get $\mathrm{MEX}=0$, it is enough to perform one increment: $a_1$++;
to get $\mathrm{MEX}=1$, it is enough to perform one increment: $a_2$++;
$\mathrm{MEX}=2$ for a given array, so there is no need to perform increments;
it is impossible to get $\mathrm{MEX}=3$ by performing increments. | import sys
input = sys.stdin.readline
def solve(n, x):
ans = [-1] * (n + 1)
tkrarat = [(0) for _ in range(max(*x, n + 1) + 1)]
for i in x:
tkrarat[i] += 1
temp = 0
availbleTkrarat = []
for i in range(n + 1):
if tkrarat[i] >= 1:
ans[i] = temp + tkrarat[i]
availbleTkrarat.extend([i] * (tkrarat[i] - 1))
else:
ans[i] = temp
if availbleTkrarat:
temp += i - availbleTkrarat.pop()
else:
break
return ans
for _ in range(int(input())):
n = int(input())
x = tuple(map(int, input().split()))
print(*solve(n, x)) | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR BIN_OP VAR FUNC_CALL VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.
Consider n distinct points on a line. Let the i-th point have the coordinate x_{i} and weight w_{i}. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |x_{i} - x_{j}| ≥ w_{i} + w_{j}.
Find the size of the maximum clique in such graph.
-----Input-----
The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.
Each of the next n lines contains two numbers x_{i}, w_{i} (0 ≤ x_{i} ≤ 10^9, 1 ≤ w_{i} ≤ 10^9) — the coordinate and the weight of a point. All x_{i} are different.
-----Output-----
Print a single number — the number of vertexes in the maximum clique of the given graph.
-----Examples-----
Input
4
2 3
3 1
6 1
0 2
Output
3
-----Note-----
If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!
The picture for the sample test. [Image] | def read_data():
n = int(input())
ranges = []
buffer = [input() for i in range(n)]
for xw in buffer:
x, w = map(int, xw.split())
ranges.append((x + w, x - w))
return n, ranges
def solve(n, ranges):
ranges.sort()
n = 0
end = -float("inf")
for e, b in ranges:
if b >= end:
n += 1
end = e
return n
n, ranges = read_data()
print(solve(n, ranges)) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.
Consider n distinct points on a line. Let the i-th point have the coordinate x_{i} and weight w_{i}. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |x_{i} - x_{j}| ≥ w_{i} + w_{j}.
Find the size of the maximum clique in such graph.
-----Input-----
The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.
Each of the next n lines contains two numbers x_{i}, w_{i} (0 ≤ x_{i} ≤ 10^9, 1 ≤ w_{i} ≤ 10^9) — the coordinate and the weight of a point. All x_{i} are different.
-----Output-----
Print a single number — the number of vertexes in the maximum clique of the given graph.
-----Examples-----
Input
4
2 3
3 1
6 1
0 2
Output
3
-----Note-----
If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!
The picture for the sample test. [Image] | R = lambda: list(map(int, input().split()))
n = int(input())
a = []
for _ in range(n):
a.append(R())
a.sort(key=lambda x: x[0] + x[1])
j = 0
ans = 1
for i in range(1, n):
if a[j][0] + a[j][1] <= a[i][0] - a[i][1]:
ans += 1
j = i
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.
Consider n distinct points on a line. Let the i-th point have the coordinate x_{i} and weight w_{i}. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |x_{i} - x_{j}| ≥ w_{i} + w_{j}.
Find the size of the maximum clique in such graph.
-----Input-----
The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.
Each of the next n lines contains two numbers x_{i}, w_{i} (0 ≤ x_{i} ≤ 10^9, 1 ≤ w_{i} ≤ 10^9) — the coordinate and the weight of a point. All x_{i} are different.
-----Output-----
Print a single number — the number of vertexes in the maximum clique of the given graph.
-----Examples-----
Input
4
2 3
3 1
6 1
0 2
Output
3
-----Note-----
If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!
The picture for the sample test. [Image] | def on(l1, l2):
return abs(l1[0] - l2[0]) >= l1[1] + l2[1]
n = int(input())
inf = []
for i in range(n):
a, b = list(map(int, input().split()))
inf.append([a + b, a - b])
inf.sort()
res = 1
last = 0
for i in range(1, n):
if inf[i][1] >= inf[last][0]:
res += 1
last = i
print(res) | FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.
Consider n distinct points on a line. Let the i-th point have the coordinate x_{i} and weight w_{i}. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |x_{i} - x_{j}| ≥ w_{i} + w_{j}.
Find the size of the maximum clique in such graph.
-----Input-----
The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.
Each of the next n lines contains two numbers x_{i}, w_{i} (0 ≤ x_{i} ≤ 10^9, 1 ≤ w_{i} ≤ 10^9) — the coordinate and the weight of a point. All x_{i} are different.
-----Output-----
Print a single number — the number of vertexes in the maximum clique of the given graph.
-----Examples-----
Input
4
2 3
3 1
6 1
0 2
Output
3
-----Note-----
If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!
The picture for the sample test. [Image] | n = int(input())
S = []
for _ in range(n):
x, w = list(map(int, input().split()))
S.append((x, w))
S = sorted(S, key=lambda kv: kv[0] + kv[1])
m = 1
last = S[0]
for kv in S[1:]:
if abs(kv[0] - last[0]) >= kv[1] + last[1]:
m += 1
last = kv
print(m) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.
Consider n distinct points on a line. Let the i-th point have the coordinate x_{i} and weight w_{i}. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |x_{i} - x_{j}| ≥ w_{i} + w_{j}.
Find the size of the maximum clique in such graph.
-----Input-----
The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.
Each of the next n lines contains two numbers x_{i}, w_{i} (0 ≤ x_{i} ≤ 10^9, 1 ≤ w_{i} ≤ 10^9) — the coordinate and the weight of a point. All x_{i} are different.
-----Output-----
Print a single number — the number of vertexes in the maximum clique of the given graph.
-----Examples-----
Input
4
2 3
3 1
6 1
0 2
Output
3
-----Note-----
If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!
The picture for the sample test. [Image] | N = int(input())
pairs = []
for i in range(N):
x, w = list(map(int, input().split()))
pairs.append((x, w))
sorted_pairs = sorted(pairs, key=lambda x: x[0])
stack = []
stack.append(sorted_pairs[0])
for x, w in sorted_pairs[1:N]:
right_x, right_w = stack[-1]
if right_x + right_w <= x - w:
stack.append((x, w))
elif x + w < right_x + right_w:
stack[-1] = x, w
print(len(stack)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.
Consider n distinct points on a line. Let the i-th point have the coordinate x_{i} and weight w_{i}. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |x_{i} - x_{j}| ≥ w_{i} + w_{j}.
Find the size of the maximum clique in such graph.
-----Input-----
The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.
Each of the next n lines contains two numbers x_{i}, w_{i} (0 ≤ x_{i} ≤ 10^9, 1 ≤ w_{i} ≤ 10^9) — the coordinate and the weight of a point. All x_{i} are different.
-----Output-----
Print a single number — the number of vertexes in the maximum clique of the given graph.
-----Examples-----
Input
4
2 3
3 1
6 1
0 2
Output
3
-----Note-----
If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!
The picture for the sample test. [Image] | n = int(input())
l = []
for i in range(n):
x, y = list(map(int, input().split()))
l += [(x + y, x - y)]
l.sort()
r = -2000000000
a = 0
for u in l:
if u[1] >= r:
a += 1
r = u[0]
print(a) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR LIST BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.
Consider n distinct points on a line. Let the i-th point have the coordinate x_{i} and weight w_{i}. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |x_{i} - x_{j}| ≥ w_{i} + w_{j}.
Find the size of the maximum clique in such graph.
-----Input-----
The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.
Each of the next n lines contains two numbers x_{i}, w_{i} (0 ≤ x_{i} ≤ 10^9, 1 ≤ w_{i} ≤ 10^9) — the coordinate and the weight of a point. All x_{i} are different.
-----Output-----
Print a single number — the number of vertexes in the maximum clique of the given graph.
-----Examples-----
Input
4
2 3
3 1
6 1
0 2
Output
3
-----Note-----
If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!
The picture for the sample test. [Image] | n = int(input())
l = sorted(
[
(x[0] - x[1], x[0] + x[1])
for x in [list(map(int, input().split())) for _ in range(n)]
],
key=lambda x: x[1],
)
ans = 1
cur = 0
for i in range(1, n):
if l[cur][1] <= l[i][0]:
cur = i
ans += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
The clique problem is one of the most well-known NP-complete problems. Under some simplification it can be formulated as follows. Consider an undirected graph G. It is required to find a subset of vertices C of the maximum size such that any two of them are connected by an edge in graph G. Sounds simple, doesn't it? Nobody yet knows an algorithm that finds a solution to this problem in polynomial time of the size of the graph. However, as with many other NP-complete problems, the clique problem is easier if you consider a specific type of a graph.
Consider n distinct points on a line. Let the i-th point have the coordinate x_{i} and weight w_{i}. Let's form graph G, whose vertices are these points and edges connect exactly the pairs of points (i, j), such that the distance between them is not less than the sum of their weights, or more formally: |x_{i} - x_{j}| ≥ w_{i} + w_{j}.
Find the size of the maximum clique in such graph.
-----Input-----
The first line contains the integer n (1 ≤ n ≤ 200 000) — the number of points.
Each of the next n lines contains two numbers x_{i}, w_{i} (0 ≤ x_{i} ≤ 10^9, 1 ≤ w_{i} ≤ 10^9) — the coordinate and the weight of a point. All x_{i} are different.
-----Output-----
Print a single number — the number of vertexes in the maximum clique of the given graph.
-----Examples-----
Input
4
2 3
3 1
6 1
0 2
Output
3
-----Note-----
If you happen to know how to solve this problem without using the specific properties of the graph formulated in the problem statement, then you are able to get a prize of one million dollars!
The picture for the sample test. [Image] | import sys
n = int(input())
ranges = []
for xw in sys.stdin:
x, w = map(int, xw.split())
ranges.append((x + w, x - w))
ranges.sort()
result = 0
end = -float("inf")
for e, b in ranges:
if b >= end:
result += 1
end = e
print(result) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
satisfaction.sort()
max_satisfaction = 0
for i in range(0, len(satisfaction)):
curr_satisfaction = 0
timestamp = 1
for j in range(i, len(satisfaction)):
curr_satisfaction += satisfaction[j] * timestamp
timestamp += 1
max_satisfaction = max(max_satisfaction, curr_satisfaction)
return max_satisfaction | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, sat: List[int]) -> int:
if max(sat) < 0:
return 0
arr = []
sat.sort(reverse=True)
for i in range(len(sat)):
sun = 0
for j in range(len(sat) - i):
sun += sat[j] * (len(sat) - i - j)
arr.append(sun)
print(arr)
return max(arr) | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
satisfaction.sort()
if satisfaction[0] >= 0:
return calc_satisfaction(satisfaction)
min_val = -sys.maxsize
min_index = None
for i, v in enumerate(satisfaction):
if 0 > v > min_val:
min_index = i
min_val = v
curr_satisfaction = calc_satisfaction(satisfaction[min_index + 1 :])
while True:
min_index -= 1
temp_satisfaction = calc_satisfaction(satisfaction[min_index + 1 :])
if temp_satisfaction > curr_satisfaction:
curr_satisfaction = temp_satisfaction
else:
break
return curr_satisfaction
def calc_satisfaction(dishes):
total = 0
for i, s in enumerate(dishes):
total += (i + 1) * s
return total | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR VAR IF NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | def merge(arr1, arr2):
index1, index2 = 0, 0
res = []
while index1 < len(arr1) or index2 < len(arr2):
if index1 >= len(arr1) or index2 < len(arr2) and arr1[index1] > arr2[index2]:
res.append(arr2[index2])
index2 += 1
else:
res.append(arr1[index1])
index1 += 1
return res
def mergeSort(arr):
if len(arr) <= 1:
return arr
m = int(len(arr) / 2)
return merge(mergeSort(arr[:m]), mergeSort(arr[m:]))
def splitAtZero(sortedArr):
for i in range(len(sortedArr)):
if sortedArr[i] >= 0:
return sortedArr[:i], sortedArr[i:]
return sortedArr, []
def coefficient(arr):
res = 0
for i in range(len(arr)):
res += (i + 1) * arr[i]
return res
class Solution:
def maxSatisfaction(self, unsortedSatisfaction: List[int]) -> int:
satisfaction = mergeSort(unsortedSatisfaction)
negative, nonnegative = splitAtZero(satisfaction)
res = nonnegative[:]
for dish in reversed(negative):
attempt = [dish] + res[:]
if coefficient(attempt) > coefficient(res):
res = attempt[:]
else:
return coefficient(res)
return coefficient(res) | FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER RETURN VAR VAR VAR VAR RETURN VAR LIST FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR RETURN VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, sat: List[int]) -> int:
sat.sort()
split = 0
while split < len(sat) and sat[split] < 0:
split += 1
if split == len(sat):
return 0
cur_sum, i, unit = 0, 1, 0
for x in sat[split:]:
cur_sum += i * x
unit += x
i += 1
self.max = cur_sum
for i in range(split - 1, -1, -1):
r, diff, psum = 1, 0, cur_sum
for j in range(i, split):
diff += r * sat[j]
r += 1
psum += unit
psum += diff
if psum > self.max:
self.max = psum
return self.max | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
satisfaction.sort(reverse=True)
ans = cur_sum = 0
for ele in satisfaction:
cur_sum += ele
if cur_sum >= 0:
ans += cur_sum
return ans | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR IF VAR NUMBER VAR VAR RETURN VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
satisfaction.sort(reverse=True)
max_satisfaction = 0
satis = 0
sum_ = 0
for s in satisfaction:
satis += sum_ + s
sum_ += s
max_satisfaction = max(max_satisfaction, satis)
if sum_ < 0:
break
return max_satisfaction | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
if max(satisfaction) <= 0:
return 0
satisfaction.sort()
if satisfaction[0] > 0:
output = 0
for i, v in enumerate(satisfaction):
output += (i + 1) * v
return output
min_val = min(i for i in satisfaction if i >= 0)
min_index = satisfaction.index(min_val)
temp_satisfaction = satisfaction[min_index:]
prev = -1
current_sum = 0
while current_sum > prev and len(temp_satisfaction) <= len(satisfaction):
prev = current_sum
current_sum = 0
for i, v in enumerate(temp_satisfaction):
current_sum += (i + 1) * v
min_index -= 1
temp_satisfaction.insert(0, satisfaction[min_index])
return prev if prev > current_sum else current_sum | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR RETURN VAR VAR VAR VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, s: List[int]) -> int:
s.sort(reverse=True)
maxi = 0
for i in range(len(s)):
t = 0
for j in range(i + 1):
t += s[j] * (i + 1 - j)
maxi = max(maxi, t)
return maxi | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
sortlist = sorted(satisfaction, reverse=True)
a = 0
b = 0
for i in range(len(sortlist)):
a += sortlist[i]
if a <= 0:
break
else:
b += 1
return sum(sortlist[j] * (b - j) for j in range(0, b)) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, sat: List[int]) -> int:
sat = sorted(sat)
out = [0]
while len(sat) > 0:
value = 0
for i, v in enumerate(sat):
value += v * (i + 1)
out.append(value)
print(sat)
if sat[0] > 0:
break
sat = sat[1:]
return max(out) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
satisfaction.sort()
s = 0
counter = 1
for i in satisfaction:
s += i * counter
counter += 1
check = s
longest = s
while True:
s = 0
counter = 1
for i in satisfaction:
s += i * counter
counter += 1
if s < check:
return check
if s > check:
longest = s
check = s
if len(satisfaction) == 0:
return longest
del satisfaction[0] | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR VAR NUMBER VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
sat_sort = sorted(satisfaction)
max_sat = 0
for i in range(len(sat_sort)):
sat = sum(t * s for t, s in enumerate(sat_sort[i:], start=1))
max_sat = max(max_sat, sat, 0)
return max_sat | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
satisfaction.sort()
dp = [([0] * len(satisfaction)) for i in range(len(satisfaction))]
maxVal = 0
for i in range(len(satisfaction)):
count = 1
tempM = 0
for j in range(i, len(satisfaction)):
dp[i][j] = satisfaction[j] * count
tempM += dp[i][j]
count += 1
maxVal = max(maxVal, tempM)
return maxVal | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
satisfaction.sort()
if satisfaction[-1] <= 0:
return 0
last_sum = 0
temp = [0] * len(satisfaction)
for i in range(len(satisfaction), 0, -1):
temp = [
(a + b) for a, b in zip(temp, [0] * (i - 1) + satisfaction[i - 1 :])
]
score = sum(temp)
if last_sum > score:
break
last_sum = score
return last_sum | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
satisfaction.sort()
total, res = 0, 0
while satisfaction and satisfaction[-1] + total > 0:
total += satisfaction.pop()
res += total
return res | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, s: List[int]) -> int:
s.sort()
l = len(s)
dp = [[(0) for _ in range(l)] for _ in range(l)]
for i in range(l):
for j in range(l):
if j <= i:
if j == 0:
dp[i][j] = (j + 1) * s[i]
else:
dp[i][j] = dp[i - 1][j - 1] + (j + 1) * s[i]
print(max(dp[-1]))
return max(max(dp[-1]), 0) | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def mergeSort(self, lst: List[int]) -> int:
n = len(lst)
if n <= 1:
return lst
mid = n // 2
L = lst[:mid]
R = lst[mid:]
self.mergeSort(L)
self.mergeSort(R)
i, j, k = 0, 0, 0
while i < len(L) and j < len(R):
if L[i] < R[j]:
lst[k] = L[i]
i += 1
else:
lst[k] = R[j]
j += 1
k += 1
while i < len(L):
lst[k] = L[i]
i += 1
k += 1
while j < len(R):
lst[k] = R[j]
j += 1
k += 1
return lst
def maxSatisfaction(self, satisfaction: List[int]) -> int:
n = len(satisfaction)
possible_sums = [None for i in range(n)]
print(".......")
self.mergeSort(satisfaction)
print(satisfaction)
for i in range(n):
arr = satisfaction[i:]
like_times = [((j + 1) * arr[j]) for j in range(len(arr))]
possible_sums[i] = sum(like_times)
return max(max(possible_sums), 0) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
satisfaction.sort()
totalDishes = len(satisfaction)
dp = {}
def cookOne(i0, n0):
if i0 == totalDishes:
return 0
if (i0, n0) in dp:
return dp[i0, n0]
if satisfaction[i0] >= 0:
dp[i0, n0] = (n0 + 1) * satisfaction[i0] + cookOne(i0 + 1, n0 + 1)
else:
dp[i0, n0] = max(
(n0 + 1) * satisfaction[i0] + cookOne(i0 + 1, n0 + 1),
cookOne(i0 + 1, n0),
)
return dp[i0, n0]
return cookOne(0, 0) | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
satisfaction.sort()
n = len(satisfaction)
dp = [[(0) for j in range(n + 1)] for i in range(n + 1)]
for j in range(1, n + 1):
dp[1][j] = satisfaction[j - 1]
ans = 0
for i in range(2, n + 1):
for j in range(i, n + 1):
dp[i][j] = dp[i - 1][j - 1] + satisfaction[j - 1] * i
ans = max(ans, dp[i][j])
return ans | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
sat = sorted(satisfaction)
dp = [([-float("inf")] * (len(sat) + 1)) for i in range(len(sat) + 1)]
for i in range(len(sat) + 1):
dp[0][i] = 0
ans = 0
for i in range(1, len(sat) + 1):
for j in range(i, len(sat) + 1):
dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + sat[j - 1] * i)
ans = max(ans, dp[i][j])
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
satisfaction.sort()
best = 0
total = 0
for i in range(len(satisfaction) - 1, -1, -1):
for j in range(len(satisfaction) - 1, i - 1, -1):
total += satisfaction[j]
best = max(best, total)
return best | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
satisfaction.sort()
positive_sum, positive_step, positive_count = 0, 0, 0
for i in satisfaction:
if i < 0:
continue
positive_count += 1
positive_sum += positive_count * i
positive_step += i
negative_count = len(satisfaction) - positive_count
result = [positive_sum]
for i in range(negative_count):
negative_sum = 0
for j in range(i + 1):
negative_sum += satisfaction[negative_count - j - 1] * (i - j + 1)
result.append(negative_sum + positive_sum + positive_step * (i + 1))
return max(result) | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
s = sorted(satisfaction, reverse=True)
total = 0
i = 0
while i < len(satisfaction):
total = max(
sum([(x * (i + 1)) for i, x in enumerate(s[0 : i + 1][::-1])]), total
)
i += 1
return total | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER RETURN VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
ordered_satis = sorted(satisfaction)
nums = len_num = len(ordered_satis)
max_satis = 0
while len_num > 0:
tem_max = 0
for i, j in enumerate(ordered_satis):
if nums - len_num > i:
continue
tem_max += (i + 1 + len_num - nums) * j
max_satis = max(max_satis, tem_max)
len_num -= 1
return max_satis | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
satisfaction.sort()
m = 0
while len(satisfaction) > 0:
s = sum(satisfaction[i] * (i + 1) for i in range(len(satisfaction)))
m = max(s, m)
satisfaction = satisfaction[1:]
return m | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
satisfaction.sort(reverse=True)
length, res = len(satisfaction), 0
l = [0] * length
for i in range(length):
summ = 0
for j in range(i + 1):
l[j] += satisfaction[j]
summ += l[j]
if summ > res:
res = summ
return res | CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR |
A chef has collected data on the satisfaction level of his n dishes. Chef can cook any dish in 1 unit of time.
Like-time coefficient of a dish is defined as the time taken to cook that dish including previous dishes multiplied by its satisfaction level i.e. time[i]*satisfaction[i]
Return the maximum sum of Like-time coefficient that the chef can obtain after dishes preparation.
Dishes can be prepared in any order and the chef can discard some dishes to get this maximum value.
Example 1:
Input: satisfaction = [-1,-8,0,5,-9]
Output: 14
Explanation: After Removing the second and last dish, the maximum total Like-time coefficient will be equal to (-1*1 + 0*2 + 5*3 = 14). Each dish is prepared in one unit of time.
Example 2:
Input: satisfaction = [4,3,2]
Output: 20
Explanation: Dishes can be prepared in any order, (2*1 + 3*2 + 4*3 = 20)
Example 3:
Input: satisfaction = [-1,-4,-5]
Output: 0
Explanation: People don't like the dishes. No dish is prepared.
Example 4:
Input: satisfaction = [-2,5,-1,0,3,-3]
Output: 35
Constraints:
n == satisfaction.length
1 <= n <= 500
-10^3 <= satisfaction[i] <= 10^3 | class Solution:
def maxSatisfaction(self, satisfaction: List[int]) -> int:
highScore = 0
satisfaction.sort(reverse=True)
for n in range(len(satisfaction)):
sortedSatisfaction = satisfaction[: len(satisfaction) - n]
sumSatisfaction = 0
for i in range(len(sortedSatisfaction)):
multiplier = len(sortedSatisfaction) - i
sumSatisfaction += sortedSatisfaction[i] * multiplier
if sumSatisfaction > highScore:
highScore = sumSatisfaction
return highScore | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.