description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
def arr_in(): return list(map(int, input().split())) def mapIn(): return map(int, input().split()) for ii in range(int(input())): n = int(input()) x = int(input()) a = [1] print(1) for i in range(n - 1): x = int(input()) if x == a[-1] + 1: a[-1] = x elif x == 1: a.append(x) elif x != 1 and x != a[-1] + 1: while len(a) != 0 and x != a[-1] + 1: a.pop() a[-1] = x s = "" for y in a: s += str(y) + "." print(s[: len(s) - 1])
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR 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 ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR ASSIGN VAR STRING FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
def solve(): N = int(input()) A = [int(input()) for _ in range(N)] st = [0] for a in A: if a == st[-1] + 1: st[-1] = a elif a == 1: st.append(1) else: while a != st[-1] + 1: st.pop() st[-1] = a for i in range(len(st) - 1): print(st[i], end=".") print(st[-1]) T = int(input()) for _ 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 VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
for _ in range(int(input())): n = int(input()) a = [] for i in range(n): a.append(int(input())) ans = [a[0]] print(ans[0]) for i in range(1, n): if a[i] == 1: ans.append(1) else: for j in range(len(ans) - 1, -1, -1): if ans[j] == a[i] - 1: ans[j] = a[i] break else: ans.pop() print(ans[0], end="") for j in range(1, len(ans)): print(".", end="") print(ans[j], end="") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR 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 FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
t = int(input()) ans = [] for _ in range(t): n = int(input()) lst = [1] levels = [1] level = 0 num = int(input()) for i in range(n - 1): num = int(input()) if num == 1: level += 1 if len(levels) <= level: levels.append(1) else: levels[level] = 1 else: while levels[level] != num - 1: level -= 1 levels[level] = num lst.append(".".join([str(k) for k in levels][: level + 1])) ans.extend(lst) print(*ans, sep="\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
def p(f): n = len(f) for i in range(n - 1): print(str(f[i]) + ".", end="") print(f[n - 1]) q = int(input()) for qe in range(q): n = int(input()) l = [] for i in range(n): l = l + [int(input())] f = [l[0]] j = 0 z = [] for i in range(1, n): k = l[i] z += [f] if k == 1: f = f[:] + [1] j += 1 else: m = len(f) while f[j] + 1 != k: j -= 1 f = f[:j] + [k] z += [f] for i in range(len(z)): p(z[i])
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR STRING STRING EXPR FUNC_CALL VAR VAR BIN_OP 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 LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR LIST FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR LIST VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR LIST NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR LIST VAR VAR LIST VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
t = int(input()) for test in range(t): n = int(input()) s = [] tog = 0 while tog < n: a = int(input()) if len(s) == 0 or a == 1: s.append(a) else: while True: if a - s[-1] == 1: s[-1] += 1 break s.pop(-1) if len(s) == 0: s.append(a) break for i in range(len(s) - 1): print(str(s[i]) + ".", end="") print(str(s[-1])) tog += 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE NUMBER IF BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR STRING STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
from sys import stdin, stdout intn = lambda: int(stdin.readline()) strs = lambda: stdin.readline()[:-1] lstr = lambda: list(stdin.readline()[:-1]) mint = lambda: map(int, stdin.readline().split()) lint = lambda: list(map(int, stdin.readline().split())) out = lambda x: stdout.write(str(x) + "\n") out_ = lambda x: stdout.write(str(x) + " ") def main(): for _ in range(intn()): n = intn() a = intn() l = [str(a)] for i in range(1, n): a = intn() x = l[i - 1] if a == 1: l.append(x + ".1") continue e = len(x) for j in range(len(x) - 1, -1, -1): if x[j] == ".": if a == int(x[j + 1 : e]) + 1: l.append(x[: j + 1] + str(a)) break e = j else: l.append(str(a)) print(*l, sep="\n") main()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING IF VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
t = int(input()) for _ in range(t): n = int(input()) ans = [] for _ in range(n): a = int(input()) if not ans: ans.append(a) elif a == 1: ans.append(a) else: while not ans[-1] + 1 == a: ans.pop() ans[-1] += 1 print(".".join(map(str, ans)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
for _ in range(int(input())): stack = [] n = int(input()) a = [int(input()) for _ in range(n)] stack.append(a[0]) print(stack[-1]) for x in a[1:]: if x == stack[-1]: if x == 1: stack.append(1) print(*stack, sep=".") else: while len(stack) > 0: if stack.pop() == x - 1: stack.append(x) print(*stack, sep=".") break elif x > stack[-1]: if x == stack[-1] + 1: stack[-1] = x print(*stack, sep=".") else: while len(stack) > 0: if stack.pop() == x - 1: stack.append(x) print(*stack, sep=".") break elif x == 1: stack.append(1) print(*stack, sep=".") else: while len(stack) > 0: if stack.pop() == x - 1: stack.append(x) print(*stack, sep=".") break
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR STRING WHILE FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING IF VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR STRING WHILE FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR STRING WHILE FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
for _ in range(int(input())): s = "" lst = [] for _ in range(int(input())): n = input() if n == "1": lst.insert(0, n) else: f = lst.index(str(int(n) - 1)) lst = lst[f:] lst[0] = n print(".".join(lst[::-1]))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
t = int(input()) for _ in range(t): stack = [] n = int(input()) for i in range(n): a = int(input()) if a == 1: stack.append(a) print(".".join(map(str, stack))) else: last = stack.pop() while a != last + 1: last = stack.pop() stack.append(a) print(".".join(map(str, stack)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
t = int(input()) for i in range(t): n = int(input()) arr = [] for i in range(n): arr.append(int(input())) stack = [] for i in range(n): if arr[i] == 1: stack.append(arr[i]) print(".".join(map(str, stack))) continue while stack and stack[-1] != arr[i] - 1: stack.pop() try: stack.pop() except: pass stack.append(arr[i]) print(".".join(map(str, stack)))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR 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 FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR WHILE VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
for _ in range(int(input())): n = int(input()) s = [] i = 0 while i < n: ref = input() i += 1 if len(s) == 0: s.append(ref) print(ref) continue elif ref == "1": s.append("1") else: while len(s) > 0: if int(s[-1]) == int(ref) - 1: s.pop(-1) break s.pop(-1) s.append(ref) j = 1 l = len(s) print(s[0], end="") while j < l: print("." + s[j], end="") j += 1 print("")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR STRING WHILE FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER STRING WHILE VAR VAR EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
tests = int(input()) for _ in range(tests): lines = int(input()) res = [] for x in range(lines): num = input() if int(num) == 1: res.append(num) else: while int(res[-1]) != int(num) - 1: del res[-1] res[-1] = num print(".".join(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 LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
t = int(input()) def solve(): n = int(input()) l = [] for i in range(n): l.append(int(input())) s = [1] print(1) for i in range(1, n): if s[-1] == l[i] - 1: s[-1] = l[i] elif l[i] == 1: s.append(1) else: while s[-1] != l[i] - 1: s.pop() s[-1] = l[i] print(".".join(map(str, s))) for i in range(t): solve()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
from sys import stdin T = int(stdin.readline()) for t in range(T): n = int(stdin.readline()) S, R = [], [] for i in range(n): a = int(stdin.readline()) if a == 1: S.append(a) else: while S[-1] != a - 1: S.pop() S.pop() S.append(a) R.append(".".join(map(str, S))) print("\n".join(R))
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 LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
t = int(input()) for i in range(t): n = int(input()) a = int(input()) last = [a] print(1) for i in range(1, n): a = int(input()) flag = False if a == 1: last.append(1) flag = True while not flag: if a - 1 == last[-1]: last[-1] += 1 flag = True else: last = last[:-1] print(".".join(list(map(str, last))))
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 ASSIGN VAR LIST VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
t = int(input()) for q in range(t): n = int(input()) a = [] i = 0 while i < n: temp = int(input()) if temp == 1: a.append(1) elif temp == a[-1] + 1: a[-1] += 1 else: while True: a.pop() if temp == a[-1] + 1: a[-1] += 1 break print(*a, sep=".") i += 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER WHILE NUMBER EXPR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING VAR NUMBER
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
t = int(input()) for _ in range(0, t): n = int(input()) a = [] a.append(int(input())) print(*a) l = 1 for i in range(1, n): x = int(input()) if x == 1: a.append(x) l = l + 1 print(*a, sep=".") else: while x - 1 != a[l - 1]: a.pop() l = l - 1 if x - 1 == a[l - 1]: a.pop() a.append(x) print(*a, sep=".")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR STRING WHILE BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
def printNum(v): for j in range(len(v)): if j != 0: print(".", end="") print(v[j], end="") print() def main(): for tc in range(int(input())): n = int(input()) sz = 0 v = [] for i in range(n): x = int(input()) if x == 1: if sz == 0: v.append([1]) else: cur = v[sz - 1].copy() cur.append(1) v.append(cur) sz += 1 else: while sz > 0 and v[sz - 1][len(v[sz - 1]) - 1] != x - 1: v.pop() sz -= 1 v[sz - 1][len(v[sz - 1]) - 1] += 1 printNum(v[sz - 1]) main()
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
def read_int(): return int(input()) def read_ints(): return map(int, input().split(" ")) t = read_int() for case_num in range(t): n = read_int() stk = [] for _ in range(n): trailing = read_int() if trailing == 1: stk.append(1) print(".".join(map(str, stk))) else: while trailing != stk[-1] + 1: stk.pop() stk.pop() stk.append(trailing) print(".".join(map(str, stk)))
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR WHILE VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
import sys input = iter(sys.stdin.read().splitlines()).__next__ def solve(): n = int(input()) input() a = [int(input()) for i in range(n - 1)] l = ["1"] stack = [1] stack_str = ["1"] for e in a: if e == 1: stack.append(1) stack_str.append("1") else: while e != stack[-1] + 1: stack.pop() stack_str.pop() stack[-1] = e stack_str[-1] = str(e) l.append(".".join(stack_str)) return l t = int(input()) output = [] for _ in range(t): output.extend(solve()) print(*output, sep="\n")
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST STRING ASSIGN VAR LIST NUMBER ASSIGN VAR LIST STRING FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING WHILE VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR RETURN 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 VAR STRING
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
t = int(input()) while t > 0: t -= 1 n = int(input()) s = [] while n > 0: n -= 1 a = int(input()) if a == 1: s += [1] else: while s[-1] != a - 1: s.pop() s[-1] += 1 print(*s, sep=".")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER VAR LIST NUMBER WHILE VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
from sys import stdin, stdout input = stdin.readline for _ in range(int(input())): n = int(input()) stack = [] for i in range(n): k = int(input()) if k == 1: stack.append(1) else: while stack and stack[-1] + 1 != k: stack.pop() else: stack[-1] += 1 stdout.write(".".join(map(str, stack)) + "\n")
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR STRING
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
t = int(input()) for i in range(t): a = [] n = int(input()) for j in range(n): e = int(input()) if e == 1: a.append(e) print(*a, sep=".") else: for k in range(len(a)): if a[k] == e - 1: m = k a = a[:m] a.append(e) print(*a, sep=".")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
t = int(input()) for i in range(t): n = int(input()) a = input() s = [1] print(1) for k in range(n - 1): a = input() if a == "1": s.append(1) else: while int(a) != int(s[-1]) + 1: s.pop() s[-1] = int(a) for rpo in range(len(s) - 1): print(s[rpo], end="") print(".", end="") print(s[-1])
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 ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR NUMBER WHILE FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR VAR NUMBER
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
t = int(input()) for i in range(t): n = int(input()) a = [] for i in range(n): a.append(int(input())) string = [1] print(*string) for i in range(1, n): if a[i] == 1: string = string + [1] print(".".join([str(i) for i in string])) else: for j in range(len(string) - 1, -1, -1): if string[j] == a[i] - 1: string = string[:j] + [a[i]] print(".".join([str(i) for i in string])) break
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR 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 FUNC_CALL VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR LIST NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
def list_print(lst): print(".".join(str(x) for x in lst)) def solve(): n = int(input()) last_pos = [] for _ in range(n): last_pos.append(int(input())) lst = [1] * n index = 0 assert last_pos[0] == 1 list_print(lst[: index + 1]) for i in range(1, n): if last_pos[i] == 1: index += 1 lst[index] = 1 list_print(lst[: index + 1]) continue for last in range(index, -1, -1): if lst[last] + 1 == last_pos[i]: index = last lst[index] += 1 list_print(lst[: index + 1]) break for _ in range(int(input().strip())): solve()
FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
import sys from sys import stdin tt = int(stdin.readline()) for loop in range(tt): n = int(stdin.readline()) a = [int(stdin.readline()) for i in range(n)] ans = [] for i in range(n): if a[i] == 1: ans.append(1) else: while ans[-1] != a[i] - 1: del ans[-1] del ans[-1] ans.append(a[i]) print(".".join(map(str, ans)))
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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
import sys input = sys.stdin.buffer.readline for _ in range(int(input())): n = int(input()) usable = [[] for i in range(n + 1)] usable[0].append([1, 0]) deepest = [1, 0] for i in range(n): b = int(input()) if len(usable[b - 1]) == 0: assert b == 1 new = deepest.copy() new[0] += 1 new.append(1) usable[1].append(new) deepest = new else: new = usable[b - 1][-1].copy() new[-1] += 1 for k in range(n + 1): j = usable[k] while j and j[-1][0] >= new[0]: j.pop() usable[b].append(new) deepest = new print(".".join(map(str, new[1:])))
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 LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
def prS(): print(".".join(map(str, a))) for _ in range(int(input())): n = int(input()) l = [] for i in range(n): l.append(int(input())) a = [l[0]] print(a[0]) for i in range(1, n): if l[i] == 1: a.append(1) else: while l[i] != a[-1] + 1: del a[-1] a[-1] = l[i] prS()
FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR 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 FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
for _ in range(int(input())): n = int(input()) arr = [int(input()) for i in range(n)] ans = [["1"]] st = [["1"]] for i in arr[1:]: if i == 1: p = st[-1][:] st.append(p + [".", "1"]) ans.append(p + [".", "1"]) else: v = 10**9 while st: if int(st[-1][-1]) == i: v = len(st[-1]) st.pop() elif i - int(st[-1][-1]) == 1: if len(st[-1]) < v: break else: st.pop() else: st.pop() if st: p = st[-1][:] p[-1] = str(i) st.append(p) ans.append(p) else: ans.append(str(i)) st.append(str(i)) for i in ans: print("".join(i))
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 VAR VAR ASSIGN VAR LIST LIST STRING ASSIGN VAR LIST LIST STRING FOR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR LIST STRING STRING EXPR FUNC_CALL VAR BIN_OP VAR LIST STRING STRING ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR IF FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR IF BIN_OP VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
for _ in range(int(input())): n = int(input()) arr = [] while n: n -= 1 a = int(input()) if a == 1: arr.append(1) else: while arr and arr[-1] != a - 1: arr.pop() arr[-1] += 1 print(*arr, sep=".")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
from sys import stdin, stdout input = stdin.readline def output(ans): for i in ans[:-1]: stdout.write(str(i) + ".") stdout.write(str(ans[-1]) + "\n") for _ in range(int(input())): n = int(input()) stack = [] for i in range(n): element = int(input()) if element == 1: stack.append(1) output(stack) else: while len(stack) > 0: ele = stack.pop() if ele + 1 == element: break stack.append(element) output(stack)
ASSIGN VAR VAR FUNC_DEF FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
from sys import stderr, stdin input = stdin.readline def dbp(*args, **kwargs): print(*args, file=stderr, **kwargs) def get_int_list(): return [int(x) for x in input().strip().split()] def do_thing(): n = int(input()) last = [] for i in range(n): end = int(input()) if not last: print(end) last = [end] continue if end == 1: last.append(1) print(".".join(str(x) for x in last)) else: ok = False for idx in range(len(last) - 1, -1, -1): if last[idx] + 1 != end: continue last[idx] = end last = last[: idx + 1] ok = True break if not ok: raise Exception("couldna do it") print(".".join(str(x) for x in last)) def multicase(): maxcc = int(input().strip()) for cc in range(maxcc): do_thing() multicase()
ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
ans = [] t = int(input()) while t: t -= 1 n = int(input()) l = ["1"] ans.append("1") for i in range(n): a = int(input()) if i == 0: continue if a == 1: l.append(".") l.append(str(a)) ans.append("".join(l)) continue while int(l[-1]) != a - 1: l.pop() l.pop() l.pop() l.append(str(a)) ans.append("".join(l)) print("\n".join(ans))
ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR WHILE FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
for _ in range(int(input())): n = int(input()) stack = [] ans = [] for i in range(n): pooh = int(input()) if pooh == 1: stack.append(pooh) else: while stack: if stack[len(stack) - 1] + 1 == pooh: stack[len(stack) - 1] = pooh break else: stack.pop() if not stack: stack.append(pooh) b = stack.copy() ans.append(b) for i in ans: print(*i, sep=".")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR IF BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
t = int(input()) for _ in range(t): n = int(input()) stack = [] for i in range(n): ai = int(input()) if ai == 1: stack.append(ai) else: while ai - stack[-1] != 1: stack.pop() stack.pop() stack.append(ai) for j in range(len(stack)): if j > 0: print(".", end="") print(stack[j], end="") print()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
for _ in range(int(input())): n = int(input()) l = [] for i in range(n): l.append(int(input())) ans = ["1"] temp = ["1"] for i in range(1, n): if l[i] == 1: x = ans[-1] + "." + str(1) ans.append(x) temp.append(x) elif l[i] == l[i - 1] + 1: tt = ans[-1].split(".") tt.pop(-1) tt.append(str(l[i])) x = ".".join(tt) ans.append(x) temp.append(x) else: while True: if int(temp[-1].split(".")[-1]) == l[i] - 1: break else: x = len(temp[-1].split(".")) while len(temp[-1].split(".")) == x: temp.pop(-1) tt = temp[-1].split(".") tt.pop(-1) tt.append(str(l[i])) x = ".".join(tt) ans.append(x) temp.append(x) for i in ans: print(i)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR 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 FUNC_CALL VAR ASSIGN VAR LIST STRING ASSIGN VAR LIST STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER STRING FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE NUMBER IF FUNC_CALL VAR FUNC_CALL VAR NUMBER STRING NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER STRING WHILE FUNC_CALL VAR FUNC_CALL VAR NUMBER STRING VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
import sys input = sys.stdin.readline T = int(input()) for _ in range(T): N = int(input()) prev = [0] for i in range(N): a = int(input()) if a != 1: while prev[-1] != a - 1: prev.pop() prev.pop() prev.append(a) print(".".join(map(str, prev[1:])))
IMPORT ASSIGN VAR 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 LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER WHILE VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
import sys input = sys.stdin.readline def solve(): n = int(input()) s = [] for i in range(n): v = int(input()) if v == 1: s.append(1) else: while s[-1] + 1 != v: s.pop() s[-1] = v print(".".join(map(str, s))) for i in range(int(input())): solve()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
def solve(n): prev = 1 s = [] for i in range(n): curr = int(input()) if i == 0: print(curr) s.append(str(curr)) continue elif curr == prev + 1: s[-1] = str(curr) print(".".join(s)) prev = curr continue elif curr == 1: s.append(str(curr)) print(".".join(s)) prev = curr continue else: for i in range(len(s)): if int(s[len(s) - 1 - i]) + 1 == curr: del s[len(s) - 1 - i : len(s)] s.append(str(curr)) prev = curr print(".".join(s)) break t = int(input()) for i in range(t): n = int(input()) solve(n)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
for ii in range(int(input())): n = int(input()) x = [] for jj in range(n): s = input() if "1" == s: x.append(s) print(".".join(x)) elif int(x[-1]) == int(s) - 1: x.pop() x.append(s) print(".".join(x)) else: while int(x[-1]) != int(s) - 1: x.pop() x.pop() x.append(str(s)) print(".".join(x))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF STRING VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR WHILE FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
import sys input = sys.stdin.readline def main(): n = int(input()) alst = [int(input()) for _ in range(n)] ans = [] for a in alst: if a == 1: ans.append("1") print(".".join(ans)) continue while ans and int(ans[-1]) != a - 1: ans.pop() if not ans: ans.append(str(a)) else: ans.pop() ans.append(str(a)) print(".".join(ans)) for _ in range(int(input())): main()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR WHILE VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
def next(st, prev, cur): arr = st.split(".") if len(arr) == 1: if prev == cur - 1: return str(cur) else: return str(prev) + "." + str(cur) elif prev == cur - 1: arr[len(arr) - 1] = cur ss = "" for a in arr: ss = ss + str(a) + "." return ss[0 : len(ss) - 1] elif cur == 1: arr.append(1) ss = "" for a in arr: ss = ss + str(a) + "." return ss[0 : len(ss) - 1] else: counter = 0 mark = -1 for a in arr: if int(a) == cur - 1: mark = counter counter += 1 if mark == -1: return st + "." + str(cur) else: ans = "" for i in range(mark): ans += arr[i] + "." ans += str(cur) return ans t = int(input()) while t > 0: n = int(input()) a = [0] * n for i in range(n): a[i] = int(input()) ans = [""] * n ans[0] = str(a[0]) for i in range(1, n): ans[i] = next(ans[i - 1], a[i - 1], a[i]) for s in ans: print(s) t -= 1
FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR STRING RETURN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR STRING RETURN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER RETURN BIN_OP BIN_OP VAR STRING FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR STRING VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
for _ in range(int(input())): ondszgv = 52 n = int(input()) adsd = 52645 input() ddf = 21 a = ["1"] z52 = 56 print("1") fv = 85 for __ in range(n - 1): yh = 215 t = int(input()) th = 85 if t == 1: ff = 98 a.append("1") else: while int(a[-1]) + 1 != t: jk = 527 a.pop() a[-1] = str(int(a[-1]) + 1) hh = 87 print(".".join(a))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR STRING WHILE BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
def solve(a, n): results = [] prevResult = [] for i, x in enumerate(a): if x == 1: currResult = prevResult + [1] else: for i in range(len(prevResult) - 1, -1, -1): if prevResult[i] == x - 1: currResult = prevResult[: i + 1] currResult[i] = x break prevResult = currResult results.append(".".join([str(x) for x in currResult])) return results t = int(input()) results = [] for _ in range(t): n = int(input()) a = [int(input()) for _ in range(n)] result = solve(a, n) results.append(result) for result in results: for line in result: print(line)
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST 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 VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
for _ in range(int(input())): q = [] for p in range(int(input())): q.append(int(input())) temp = [1] for i in range(len(q)): if i == 0: print(1) else: found = False for n in range(len(temp)): if temp[len(temp) - n - 1] + 1 == q[i]: temp[len(temp) - n - 1] += 1 temp = temp[: len(temp) - n] print(*temp, sep=".") found = True break if found == False: temp.append(1) print(*temp, sep=".")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
def solve(): v = [] for _ in range(int(input())): x = int(input()) if x == 1: v.append(x) else: while v[-1] != x - 1: v.pop() v[-1] += 1 for i in range(len(v)): if i < len(v) - 1: print(v[i], end=".") else: print(v[i]) for _ in range(int(input())): solve()
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
for _ in range(int(input())): l1 = ["1"] for __ in range(int(input())): n = int(input()) if __ == 0: continue if n == 1: l1.append(l1[-1] + ".1") else: shit = l1[-1].split(".") while n - int(shit[-1]) != 1: shit.pop() shit.pop() shit.append(n) l1.append(".".join([str(___) for ___ in shit])) for i in l1: print(i)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR NUMBER STRING WHILE BIN_OP VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
for tc in range(int(input())): n = int(input()) lisT = [] ans = [] leN = 0 for i in range(n): a = int(input()) while 1: if a == 1: lisT.append(1) ans.append(".".join(str(k) for k in lisT)) break if a == lisT[-1] + 1: lisT[-1] += 1 ans.append(".".join(str(k) for k in lisT)) break lisT.pop() for i in ans: print(i)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
William is a huge fan of planning ahead. That is why he starts his morning routine by creating a nested list of upcoming errands. A valid nested list is any list which can be created from a list with one item "1" by applying some operations. Each operation inserts a new item into the list, on a new line, just after one of existing items $a_1 \,.\, a_2 \,.\, a_3 \,.\, \,\cdots\, \,.\,a_k$ and can be one of two types: Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, a_k \,.\, 1$ (starting a list of a deeper level), or Add an item $a_1 \,.\, a_2 \,.\, a_3 \,.\, \cdots \,.\, (a_k + 1)$ (continuing the current level). Operation can only be applied if the list does not contain two identical items afterwards. And also, if we consider every item as a sequence of numbers, then the sequence of items should always remain increasing in lexicographical order. Examples of valid and invalid lists that are shown in the picture can found in the "Notes" section. When William decided to save a Word document with the list of his errands he accidentally hit a completely different keyboard shortcut from the "Ctrl-S" he wanted to hit. It's not known exactly what shortcut he pressed but after triggering it all items in the list were replaced by a single number: the last number originally written in the item number. William wants you to help him restore a fitting original nested list. -----Input----- Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10$). Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^3$), which is the number of lines in the list. Each of the next $n$ lines contains a single integer $a_i$ ($1 \le a_i \le n$), which is what remains of William's nested list. It is guaranteed that in each test case at least one fitting list exists. It is guaranteed that the sum of values $n$ across all test cases does not exceed $10^3$. -----Output----- For each test case output $n$ lines which represent a valid nested list, which could become the data provided to you by William. If there are multiple answers, print any. -----Examples----- Input 2 4 1 1 2 3 9 1 1 1 2 2 1 2 1 2 Output 1 1.1 1.2 1.3 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 -----Note----- In the second example test case one example of a fitting list is: 1 1.1 1.1.1 1.1.2 1.2 1.2.1 2 2.1 2.2 This list can be produced by using the sequence of operations shown below: Original list with a single item $1$. Insert item $2$ by using the insertion operation of the second type after item $1$. Insert item $1.1$ by using the insertion operation of the first type after item $1$. Insert item $1.2$ by using the insertion operation of the second type after item $1.1$. Insert item $1.1.1$ by using the insertion operation of the first type after item $1.1$. Insert item $1.1.2$ by using the insertion operation of the second type after item $1.1.1$. Insert item $1.2.1$ by using the insertion operation of the first type after item $1.2$. Insert item $2.1$ by using the insertion operation of the first type after item $2$. Insert item $2.2$ by using the insertion operation of the second type after item $2.1$.
def index_finder1(result): for i in range(len(result) - 1, -1, -1): if result[i] == ".": break return i + 1 def index_finder3(target, result): temp = 0 for i in range(len(result)): j = result.find(target, i) if j > temp: temp = j return temp def index_finder2(item, result): for i in reversed(result.split(".")): if item - int(i) == 1: result = result[: index_finder3(i, result)] + str(item) break return result t = int(input()) for i in range(t): n = int(input()) shopping = [] for i in range(n): j = int(input()) shopping.append(j) result = "" for i in range(n): if i == 0 and shopping[i] == 1: result = str(shopping[i]) elif shopping[i] == 1: result = result + ".1" elif shopping[i] - shopping[i - 1] == 1: if "." not in result: result = str(shopping[i]) else: result = result[: index_finder1(result)] + str(shopping[i]) else: result = index_finder2(shopping[i], result) print(result)
FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR STRING IF BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR 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 LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR STRING IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF STRING VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given $n$ integers. You need to choose a subset and put the chosen numbers in a beautiful rectangle (rectangular matrix). Each chosen number should occupy one of its rectangle cells, each cell must be filled with exactly one chosen number. Some of the $n$ numbers may not be chosen. A rectangle (rectangular matrix) is called beautiful if in each row and in each column all values are different. What is the largest (by the total number of cells) beautiful rectangle you can construct? Print the rectangle itself. -----Input----- The first line contains $n$ ($1 \le n \le 4\cdot10^5$). The second line contains $n$ integers ($1 \le a_i \le 10^9$). -----Output----- In the first line print $x$ ($1 \le x \le n$) — the total number of cells of the required maximum beautiful rectangle. In the second line print $p$ and $q$ ($p \cdot q=x$): its sizes. In the next $p$ lines print the required rectangle itself. If there are several answers, print any. -----Examples----- Input 12 3 1 4 1 5 9 2 6 5 3 5 8 Output 12 3 4 1 2 3 5 3 1 5 4 5 6 8 9 Input 5 1 1 1 1 1 Output 1 1 1 1
from sys import stdin, stdout def getmaxrectangle(n, a): dic = {} dicCntVals = {} for va in a: if va not in dic: dic[va] = 0 dic[va] += 1 for val in dic.keys(): cnt = dic[val] if cnt not in dicCntVals: dicCntVals[cnt] = [] dicCntVals[cnt].append(val) geq = [0] * (n + 1) if n in dicCntVals: geq[n] = len(dicCntVals[n]) for cnt in range(n - 1, 0, -1): geq[cnt] = geq[cnt + 1] if cnt in dicCntVals: geq[cnt] += len(dicCntVals[cnt]) b_pq = 0 b_p = 0 b_q = 0 ttl = 0 for p in range(1, n + 1): ttl += geq[p] q = int(ttl / p) if q >= p and q * p > b_pq: b_pq = q * p b_p = p b_q = q x = 0 y = 0 r = [[(0) for j in range(b_q)] for i in range(b_p)] for i in range(n, 0, -1): if i not in dicCntVals: continue for j in dicCntVals[i]: for k in range(min(b_p, i)): if r[x][y] != 0: x = (x + 1) % b_p if r[x][y] == 0: r[x][y] = j x = (x + 1) % b_p y = (y + 1) % b_q return r n = int(stdin.readline()) a = list(map(int, stdin.readline().split())) res = getmaxrectangle(n, a) stdout.write(str(len(res) * len(res[0]))) stdout.write("\n") stdout.write(str(len(res)) + " " + str(len(res[0]))) stdout.write("\n") for i in range(len(res)): for j in range(len(res[i])): stdout.write(str(res[i][j])) stdout.write(" ") stdout.write("\n")
FUNC_DEF ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR FOR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR STRING FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Alice got a new doll these days. It can even walk! Alice has built a maze for the doll and wants to test it. The maze is a grid with n rows and m columns. There are k obstacles, the i-th of them is on the cell (x_i, y_i), which means the cell in the intersection of the x_i-th row and the y_i-th column. However, the doll is clumsy in some ways. It can only walk straight or turn right at most once in the same cell (including the start cell). It cannot get into a cell with an obstacle or get out of the maze. More formally, there exist 4 directions, in which the doll can look: 1. The doll looks in the direction along the row from the first cell to the last. While moving looking in this direction the doll will move from the cell (x, y) into the cell (x, y + 1); 2. The doll looks in the direction along the column from the first cell to the last. While moving looking in this direction the doll will move from the cell (x, y) into the cell (x + 1, y); 3. The doll looks in the direction along the row from the last cell to first. While moving looking in this direction the doll will move from the cell (x, y) into the cell (x, y - 1); 4. The doll looks in the direction along the column from the last cell to the first. While moving looking in this direction the doll will move from the cell (x, y) into the cell (x - 1, y). . Standing in some cell the doll can move into the cell in the direction it looks or it can turn right once. Turning right once, the doll switches it's direction by the following rules: 1 → 2, 2 → 3, 3 → 4, 4 → 1. Standing in one cell, the doll can make at most one turn right. Now Alice is controlling the doll's moves. She puts the doll in of the cell (1, 1) (the upper-left cell of the maze). Initially, the doll looks to the direction 1, so along the row from the first cell to the last. She wants to let the doll walk across all the cells without obstacles exactly once and end in any place. Can it be achieved? Input The first line contains three integers n, m and k, separated by spaces (1 ≤ n,m ≤ 10^5, 0 ≤ k ≤ 10^5) — the size of the maze and the number of obstacles. Next k lines describes the obstacles, the i-th line contains two integer numbers x_i and y_i, separated by spaces (1 ≤ x_i ≤ n,1 ≤ y_i ≤ m), which describes the position of the i-th obstacle. It is guaranteed that no two obstacles are in the same cell and no obstacle is in cell (1, 1). Output Print 'Yes' (without quotes) if the doll can walk across all the cells without obstacles exactly once by the rules, described in the statement. If it is impossible to walk across the maze by these rules print 'No' (without quotes). Examples Input 3 3 2 2 2 2 1 Output Yes Input 3 3 2 3 1 2 2 Output No Input 3 3 8 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 Output Yes Note Here is the picture of maze described in the first example: <image> In the first example, the doll can walk in this way: * The doll is in the cell (1, 1), looks to the direction 1. Move straight; * The doll is in the cell (1, 2), looks to the direction 1. Move straight; * The doll is in the cell (1, 3), looks to the direction 1. Turn right; * The doll is in the cell (1, 3), looks to the direction 2. Move straight; * The doll is in the cell (2, 3), looks to the direction 2. Move straight; * The doll is in the cell (3, 3), looks to the direction 2. Turn right; * The doll is in the cell (3, 3), looks to the direction 3. Move straight; * The doll is in the cell (3, 2), looks to the direction 3. Move straight; * The doll is in the cell (3, 1), looks to the direction 3. The goal is achieved, all cells of the maze without obstacles passed exactly once.
c, r, k = list(map(int, input().split())) rows = [[] for i in range(r)] columns = [[] for i in range(c)] for i in range(k): x, y = list(map(int, input().split())) x -= 1 y -= 1 rows[y].append(x) columns[x].append(y) for v in rows: v.sort() for v in columns: v.sort() def firstGreaterThan(listlist, num, low, high): if low > high: return -1 if low == high: if listlist[low] > num: return listlist[low] else: return -1 mid = (low + high) // 2 if listlist[mid] > num: high = mid else: low = mid + 1 return firstGreaterThan(listlist, num, low, high) def lastLessThan(listlist, num, low, high): if low > high: return -1 if low == high: if listlist[low] < num: return listlist[low] else: return -1 mid = (low + high + 1) // 2 if listlist[mid] < num: low = mid else: high = mid - 1 return lastLessThan(listlist, num, low, high) xpos = 0 ypos = 0 cellsCovered = 1 dir = 1 firstStep = True xmin = 0 xmax = c - 1 ymin = 0 ymax = r - 1 while xmin <= xmax and ymin <= ymax: targetSquare = -1 if dir == 1: targetSquare = firstGreaterThan(columns[xpos], ypos, 0, len(columns[xpos]) - 1) elif dir == 2: targetSquare = firstGreaterThan(rows[ypos], xpos, 0, len(rows[ypos]) - 1) elif dir == 3: targetSquare = lastLessThan(columns[xpos], ypos, 0, len(columns[xpos]) - 1) else: targetSquare = lastLessThan(rows[ypos], xpos, 0, len(rows[ypos]) - 1) if targetSquare == -1: if dir == 1: targetSquare = ymax elif dir == 2: targetSquare = xmax elif dir == 3: targetSquare = ymin else: targetSquare = xmin elif dir == 1: targetSquare -= 1 if targetSquare > ymax: targetSquare = ymax elif dir == 2: targetSquare -= 1 if targetSquare > xmax: targetSquare = xmax elif dir == 3: targetSquare += 1 if targetSquare < ymin: targetSquare = ymin else: targetSquare += 1 if targetSquare < xmin: targetSquare = xmin if dir % 2 == 0 and targetSquare == xpos or dir % 2 == 1 and targetSquare == ypos: if firstStep: firstStep = False dir += 1 if dir == 5: dir = 1 continue break firstStep = False if dir == 1: cellsCovered += targetSquare - ypos ypos = targetSquare ymax = ypos xmin += 1 elif dir == 2: cellsCovered += targetSquare - xpos xpos = targetSquare xmax = xpos ymax -= 1 elif dir == 3: cellsCovered += ypos - targetSquare ypos = targetSquare ymin = ypos xmax -= 1 else: cellsCovered += xpos - targetSquare xpos = targetSquare xmin = xpos ymin += 1 dir += 1 if dir == 5: dir = 1 if cellsCovered == r * c - k: print("Yes") else: print("No")
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR IF VAR VAR VAR RETURN VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR IF VAR VAR VAR RETURN VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR IF VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Alice got a new doll these days. It can even walk! Alice has built a maze for the doll and wants to test it. The maze is a grid with n rows and m columns. There are k obstacles, the i-th of them is on the cell (x_i, y_i), which means the cell in the intersection of the x_i-th row and the y_i-th column. However, the doll is clumsy in some ways. It can only walk straight or turn right at most once in the same cell (including the start cell). It cannot get into a cell with an obstacle or get out of the maze. More formally, there exist 4 directions, in which the doll can look: 1. The doll looks in the direction along the row from the first cell to the last. While moving looking in this direction the doll will move from the cell (x, y) into the cell (x, y + 1); 2. The doll looks in the direction along the column from the first cell to the last. While moving looking in this direction the doll will move from the cell (x, y) into the cell (x + 1, y); 3. The doll looks in the direction along the row from the last cell to first. While moving looking in this direction the doll will move from the cell (x, y) into the cell (x, y - 1); 4. The doll looks in the direction along the column from the last cell to the first. While moving looking in this direction the doll will move from the cell (x, y) into the cell (x - 1, y). . Standing in some cell the doll can move into the cell in the direction it looks or it can turn right once. Turning right once, the doll switches it's direction by the following rules: 1 → 2, 2 → 3, 3 → 4, 4 → 1. Standing in one cell, the doll can make at most one turn right. Now Alice is controlling the doll's moves. She puts the doll in of the cell (1, 1) (the upper-left cell of the maze). Initially, the doll looks to the direction 1, so along the row from the first cell to the last. She wants to let the doll walk across all the cells without obstacles exactly once and end in any place. Can it be achieved? Input The first line contains three integers n, m and k, separated by spaces (1 ≤ n,m ≤ 10^5, 0 ≤ k ≤ 10^5) — the size of the maze and the number of obstacles. Next k lines describes the obstacles, the i-th line contains two integer numbers x_i and y_i, separated by spaces (1 ≤ x_i ≤ n,1 ≤ y_i ≤ m), which describes the position of the i-th obstacle. It is guaranteed that no two obstacles are in the same cell and no obstacle is in cell (1, 1). Output Print 'Yes' (without quotes) if the doll can walk across all the cells without obstacles exactly once by the rules, described in the statement. If it is impossible to walk across the maze by these rules print 'No' (without quotes). Examples Input 3 3 2 2 2 2 1 Output Yes Input 3 3 2 3 1 2 2 Output No Input 3 3 8 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 Output Yes Note Here is the picture of maze described in the first example: <image> In the first example, the doll can walk in this way: * The doll is in the cell (1, 1), looks to the direction 1. Move straight; * The doll is in the cell (1, 2), looks to the direction 1. Move straight; * The doll is in the cell (1, 3), looks to the direction 1. Turn right; * The doll is in the cell (1, 3), looks to the direction 2. Move straight; * The doll is in the cell (2, 3), looks to the direction 2. Move straight; * The doll is in the cell (3, 3), looks to the direction 2. Turn right; * The doll is in the cell (3, 3), looks to the direction 3. Move straight; * The doll is in the cell (3, 2), looks to the direction 3. Move straight; * The doll is in the cell (3, 1), looks to the direction 3. The goal is achieved, all cells of the maze without obstacles passed exactly once.
def tr(n, m, inp): xa = n xi = yi = 1 ya = m while True: while (xi, ya) in inp: for x in range(xi, xa + 1): inp.remove((x, ya)) ya -= 1 if ya < yi: return xi += 1 if xa < xi: return while (xa, ya) in inp: for y in range(yi, ya + 1): inp.remove((xa, y)) xa -= 1 if xa < xi: return ya -= 1 if ya < yi: return while (xa, yi) in inp: for x in range(xi, xa + 1): inp.remove((x, yi)) yi += 1 if ya < yi: return xa -= 1 if xa < xi: return while (xi, yi) in inp: for y in range(yi, ya + 1): inp.remove((xi, y)) xi += 1 if xa < xi: return yi += 1 if ya < yi: return n, m, k = map(int, input().split()) inp = {tuple(map(int, input().split())) for _ in range(k)} try: tr(n, m, inp) assert not inp print("Yes") except (AssertionError, KeyError): print("No")
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR WHILE NUMBER WHILE VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR NUMBER IF VAR VAR RETURN WHILE VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR NUMBER IF VAR VAR RETURN WHILE VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR NUMBER IF VAR VAR RETURN WHILE VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR NUMBER IF VAR VAR RETURN ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING VAR VAR EXPR FUNC_CALL VAR STRING
Alice got a new doll these days. It can even walk! Alice has built a maze for the doll and wants to test it. The maze is a grid with n rows and m columns. There are k obstacles, the i-th of them is on the cell (x_i, y_i), which means the cell in the intersection of the x_i-th row and the y_i-th column. However, the doll is clumsy in some ways. It can only walk straight or turn right at most once in the same cell (including the start cell). It cannot get into a cell with an obstacle or get out of the maze. More formally, there exist 4 directions, in which the doll can look: 1. The doll looks in the direction along the row from the first cell to the last. While moving looking in this direction the doll will move from the cell (x, y) into the cell (x, y + 1); 2. The doll looks in the direction along the column from the first cell to the last. While moving looking in this direction the doll will move from the cell (x, y) into the cell (x + 1, y); 3. The doll looks in the direction along the row from the last cell to first. While moving looking in this direction the doll will move from the cell (x, y) into the cell (x, y - 1); 4. The doll looks in the direction along the column from the last cell to the first. While moving looking in this direction the doll will move from the cell (x, y) into the cell (x - 1, y). . Standing in some cell the doll can move into the cell in the direction it looks or it can turn right once. Turning right once, the doll switches it's direction by the following rules: 1 → 2, 2 → 3, 3 → 4, 4 → 1. Standing in one cell, the doll can make at most one turn right. Now Alice is controlling the doll's moves. She puts the doll in of the cell (1, 1) (the upper-left cell of the maze). Initially, the doll looks to the direction 1, so along the row from the first cell to the last. She wants to let the doll walk across all the cells without obstacles exactly once and end in any place. Can it be achieved? Input The first line contains three integers n, m and k, separated by spaces (1 ≤ n,m ≤ 10^5, 0 ≤ k ≤ 10^5) — the size of the maze and the number of obstacles. Next k lines describes the obstacles, the i-th line contains two integer numbers x_i and y_i, separated by spaces (1 ≤ x_i ≤ n,1 ≤ y_i ≤ m), which describes the position of the i-th obstacle. It is guaranteed that no two obstacles are in the same cell and no obstacle is in cell (1, 1). Output Print 'Yes' (without quotes) if the doll can walk across all the cells without obstacles exactly once by the rules, described in the statement. If it is impossible to walk across the maze by these rules print 'No' (without quotes). Examples Input 3 3 2 2 2 2 1 Output Yes Input 3 3 2 3 1 2 2 Output No Input 3 3 8 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 Output Yes Note Here is the picture of maze described in the first example: <image> In the first example, the doll can walk in this way: * The doll is in the cell (1, 1), looks to the direction 1. Move straight; * The doll is in the cell (1, 2), looks to the direction 1. Move straight; * The doll is in the cell (1, 3), looks to the direction 1. Turn right; * The doll is in the cell (1, 3), looks to the direction 2. Move straight; * The doll is in the cell (2, 3), looks to the direction 2. Move straight; * The doll is in the cell (3, 3), looks to the direction 2. Turn right; * The doll is in the cell (3, 3), looks to the direction 3. Move straight; * The doll is in the cell (3, 2), looks to the direction 3. Move straight; * The doll is in the cell (3, 1), looks to the direction 3. The goal is achieved, all cells of the maze without obstacles passed exactly once.
from sys import stdout printn = lambda x: stdout.write(x) inn = lambda: int(input()) inl = lambda: list(map(int, input().split())) inm = lambda: map(int, input().split()) DBG = True and False BIG = 999999999 R = 10**9 + 7 def ddprint(x): if DBG: print(x) n, m, k = inm() hx = {} hy = {} cnt = 0 for i in range(k): x, y = inm() x -= 1 y -= 1 cnt += 1 if x in hx: hx[x].append(y) else: hx[x] = [y] if y in hy: hy[y].append(x) else: hy[y] = [x] ddprint(hx) ddprint(hy) x, y = 0, 0 dir = 1 if m == 1 or m > 1 and 0 in hx and 1 in hx[0]: dir = 2 minx = miny = -1 maxx = n maxy = m while True: nx = x ny = y if dir == 1: a = [z for z in hx[x] if y < z and z < maxy] if x in hx else [] if len(a) == 0: ny = maxy - 1 else: a.sort() ny = a[0] - 1 if ny == y: break cnt += ny - y y = ny minx = x dir = 2 elif dir == 2: a = [z for z in hy[y] if x < z and z < maxx] if y in hy else [] if len(a) == 0: nx = maxx - 1 else: a.sort() nx = a[0] - 1 if nx == x: break cnt += nx - x x = nx maxy = y dir = 3 elif dir == 3: a = [z for z in hx[x] if miny < z and z < y] if x in hx else [] if len(a) == 0: ny = miny + 1 else: a.sort(reverse=True) ny = a[0] + 1 if ny == y: break cnt += y - ny y = ny maxx = x dir = 4 elif dir == 4: a = [z for z in hy[y] if minx < z and z < x] if y in hy else [] if len(a) == 0: nx = minx + 1 else: a.sort(reverse=True) nx = a[0] + 1 if nx == x: break cnt += x - nx x = nx miny = y dir = 1 print("Yes" if cnt == n * m - 1 else "No")
ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR LIST IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR LIST IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR LIST IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR LIST IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER STRING STRING
Alice got a new doll these days. It can even walk! Alice has built a maze for the doll and wants to test it. The maze is a grid with n rows and m columns. There are k obstacles, the i-th of them is on the cell (x_i, y_i), which means the cell in the intersection of the x_i-th row and the y_i-th column. However, the doll is clumsy in some ways. It can only walk straight or turn right at most once in the same cell (including the start cell). It cannot get into a cell with an obstacle or get out of the maze. More formally, there exist 4 directions, in which the doll can look: 1. The doll looks in the direction along the row from the first cell to the last. While moving looking in this direction the doll will move from the cell (x, y) into the cell (x, y + 1); 2. The doll looks in the direction along the column from the first cell to the last. While moving looking in this direction the doll will move from the cell (x, y) into the cell (x + 1, y); 3. The doll looks in the direction along the row from the last cell to first. While moving looking in this direction the doll will move from the cell (x, y) into the cell (x, y - 1); 4. The doll looks in the direction along the column from the last cell to the first. While moving looking in this direction the doll will move from the cell (x, y) into the cell (x - 1, y). . Standing in some cell the doll can move into the cell in the direction it looks or it can turn right once. Turning right once, the doll switches it's direction by the following rules: 1 → 2, 2 → 3, 3 → 4, 4 → 1. Standing in one cell, the doll can make at most one turn right. Now Alice is controlling the doll's moves. She puts the doll in of the cell (1, 1) (the upper-left cell of the maze). Initially, the doll looks to the direction 1, so along the row from the first cell to the last. She wants to let the doll walk across all the cells without obstacles exactly once and end in any place. Can it be achieved? Input The first line contains three integers n, m and k, separated by spaces (1 ≤ n,m ≤ 10^5, 0 ≤ k ≤ 10^5) — the size of the maze and the number of obstacles. Next k lines describes the obstacles, the i-th line contains two integer numbers x_i and y_i, separated by spaces (1 ≤ x_i ≤ n,1 ≤ y_i ≤ m), which describes the position of the i-th obstacle. It is guaranteed that no two obstacles are in the same cell and no obstacle is in cell (1, 1). Output Print 'Yes' (without quotes) if the doll can walk across all the cells without obstacles exactly once by the rules, described in the statement. If it is impossible to walk across the maze by these rules print 'No' (without quotes). Examples Input 3 3 2 2 2 2 1 Output Yes Input 3 3 2 3 1 2 2 Output No Input 3 3 8 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 Output Yes Note Here is the picture of maze described in the first example: <image> In the first example, the doll can walk in this way: * The doll is in the cell (1, 1), looks to the direction 1. Move straight; * The doll is in the cell (1, 2), looks to the direction 1. Move straight; * The doll is in the cell (1, 3), looks to the direction 1. Turn right; * The doll is in the cell (1, 3), looks to the direction 2. Move straight; * The doll is in the cell (2, 3), looks to the direction 2. Move straight; * The doll is in the cell (3, 3), looks to the direction 2. Turn right; * The doll is in the cell (3, 3), looks to the direction 3. Move straight; * The doll is in the cell (3, 2), looks to the direction 3. Move straight; * The doll is in the cell (3, 1), looks to the direction 3. The goal is achieved, all cells of the maze without obstacles passed exactly once.
import sys n, m, k = map(int, input().split()) dr = {} dc = {} for i in range(n): dr[i] = [] for i in range(m): dc[i] = [] for i in range(k): r, c = map(int, input().split()) dr[r - 1].append(c - 1) dc[c - 1].append(r - 1) for i in range(n): dr[i].sort() for i in range(m): dc[i].sort() def findDown(r, c, mx): global dc res = mx lf, rg = 0, len(dc[c]) - 1 while lf <= rg: mid = (lf + rg) // 2 if dc[c][mid] < r: lf = mid + 1 else: res = dc[c][mid] - 1 rg = mid - 1 return min(res, mx) def findUp(r, c, mx): global dc res = mx lf, rg = 0, len(dc[c]) - 1 while lf <= rg: mid = (lf + rg) // 2 if dc[c][mid] > r: rg = mid - 1 else: res = dc[c][mid] + 1 lf = mid + 1 return max(res, mx) def findRight(r, c, mx): global dr res = mx lf, rg = 0, len(dr[r]) - 1 while lf <= rg: mid = (lf + rg) // 2 if dr[r][mid] < c: lf = mid + 1 else: res = dr[r][mid] - 1 rg = mid - 1 return min(res, mx) def findLeft(r, c, mx): global dr res = mx lf, rg = 0, len(dr[r]) - 1 while lf <= rg: mid = (lf + rg) // 2 if dr[r][mid] > c: rg = mid - 1 else: res = dr[r][mid] + 1 lf = mid + 1 return max(res, mx) lb, rb = 0, m - 1 ub, db = 1, n - 1 direct = 0 visited = 1 r, c = 0, 0 while True: nr, nc = r, c if direct == 0: nc = findRight(r, c, rb) rb = nc - 1 elif direct == 1: nr = findDown(r, c, db) db = nr - 1 elif direct == 2: nc = findLeft(r, c, lb) lb = nc + 1 elif direct == 3: nr = findUp(r, c, ub) ub = nr + 1 steps = abs(r - nr) + abs(c - nc) if steps == 0 and (r != 0 or c != 0 or direct != 0): break direct = (direct + 1) % 4 visited += steps r, c = nr, nc print("Yes" if n * m - k == visited else "No")
IMPORT ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR STRING STRING
Alice got a new doll these days. It can even walk! Alice has built a maze for the doll and wants to test it. The maze is a grid with n rows and m columns. There are k obstacles, the i-th of them is on the cell (x_i, y_i), which means the cell in the intersection of the x_i-th row and the y_i-th column. However, the doll is clumsy in some ways. It can only walk straight or turn right at most once in the same cell (including the start cell). It cannot get into a cell with an obstacle or get out of the maze. More formally, there exist 4 directions, in which the doll can look: 1. The doll looks in the direction along the row from the first cell to the last. While moving looking in this direction the doll will move from the cell (x, y) into the cell (x, y + 1); 2. The doll looks in the direction along the column from the first cell to the last. While moving looking in this direction the doll will move from the cell (x, y) into the cell (x + 1, y); 3. The doll looks in the direction along the row from the last cell to first. While moving looking in this direction the doll will move from the cell (x, y) into the cell (x, y - 1); 4. The doll looks in the direction along the column from the last cell to the first. While moving looking in this direction the doll will move from the cell (x, y) into the cell (x - 1, y). . Standing in some cell the doll can move into the cell in the direction it looks or it can turn right once. Turning right once, the doll switches it's direction by the following rules: 1 → 2, 2 → 3, 3 → 4, 4 → 1. Standing in one cell, the doll can make at most one turn right. Now Alice is controlling the doll's moves. She puts the doll in of the cell (1, 1) (the upper-left cell of the maze). Initially, the doll looks to the direction 1, so along the row from the first cell to the last. She wants to let the doll walk across all the cells without obstacles exactly once and end in any place. Can it be achieved? Input The first line contains three integers n, m and k, separated by spaces (1 ≤ n,m ≤ 10^5, 0 ≤ k ≤ 10^5) — the size of the maze and the number of obstacles. Next k lines describes the obstacles, the i-th line contains two integer numbers x_i and y_i, separated by spaces (1 ≤ x_i ≤ n,1 ≤ y_i ≤ m), which describes the position of the i-th obstacle. It is guaranteed that no two obstacles are in the same cell and no obstacle is in cell (1, 1). Output Print 'Yes' (without quotes) if the doll can walk across all the cells without obstacles exactly once by the rules, described in the statement. If it is impossible to walk across the maze by these rules print 'No' (without quotes). Examples Input 3 3 2 2 2 2 1 Output Yes Input 3 3 2 3 1 2 2 Output No Input 3 3 8 1 2 1 3 2 1 2 2 2 3 3 1 3 2 3 3 Output Yes Note Here is the picture of maze described in the first example: <image> In the first example, the doll can walk in this way: * The doll is in the cell (1, 1), looks to the direction 1. Move straight; * The doll is in the cell (1, 2), looks to the direction 1. Move straight; * The doll is in the cell (1, 3), looks to the direction 1. Turn right; * The doll is in the cell (1, 3), looks to the direction 2. Move straight; * The doll is in the cell (2, 3), looks to the direction 2. Move straight; * The doll is in the cell (3, 3), looks to the direction 2. Turn right; * The doll is in the cell (3, 3), looks to the direction 3. Move straight; * The doll is in the cell (3, 2), looks to the direction 3. Move straight; * The doll is in the cell (3, 1), looks to the direction 3. The goal is achieved, all cells of the maze without obstacles passed exactly once.
import sys def I(): return sys.stdin.readline().rstrip() n, m, k = map(int, I().split()) r, c = dict(), dict() for _ in range(k): x, y = map(int, I().split()) if x not in r: r[x] = [] r[x].append(y) if y not in c: c[y] = [] c[y].append(x) for v in r.values(): v.sort() for v in c.values(): v.sort() def bin(a, x): i = -1 j = 1 while j <= len(a): j *= 2 j //= 2 while j: if i + j < len(a) and a[i + j] <= x: i += j j //= 2 return i dx = [m + 1, n + 1, 0, 0] def next(x, y, d): if d == 0: if x in r: a = r[x] i = bin(a, y) + 1 if i < len(a): dx[d] = min(dx[d], a[i]) y = dx[d] - 1 dx[d - 1] = x elif d == 1: if y in c: a = c[y] i = bin(a, x) + 1 if i < len(a): dx[d] = min(dx[d], a[i]) x = dx[d] - 1 dx[d - 1] = y elif d == 2: if x in r: a = r[x] i = bin(a, y) if i >= 0: dx[d] = max(dx[d], a[i]) y = dx[d] + 1 dx[d - 1] = x else: if y in c: a = c[y] i = bin(a, x) if i >= 0: dx[d] = max(dx[d], a[i]) x = dx[d] + 1 dx[d - 1] = y return x, y, (d + 1) % 4 x, y, d = 1, 0, 0 while True: xx, yy, d = next(x, y, d) if x == xx and y == yy: break k += abs(xx - x) + abs(yy - y) x, y = xx, yy if k == n * m: print("Yes") else: print("No")
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER WHILE VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
A tree is an undirected connected graph in which there are no cycles. This problem is about non-rooted trees. A leaf of a tree is a vertex that is connected to at most one vertex. The gardener Vitaly grew a tree from $n$ vertices. He decided to trim the tree. To do this, he performs a number of operations. In one operation, he removes all leaves of the tree. Example of a tree. For example, consider the tree shown in the figure above. The figure below shows the result of applying exactly one operation to the tree. The result of applying the operation "remove all leaves" to the tree. Note the special cases of the operation: applying an operation to an empty tree (of $0$ vertices) does not change it; applying an operation to a tree of one vertex removes this vertex (this vertex is treated as a leaf); applying an operation to a tree of two vertices removes both vertices (both vertices are treated as leaves). Vitaly applied $k$ operations sequentially to the tree. How many vertices remain? -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. Each test case is preceded by an empty line. Each test case consists of several lines. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 4 \cdot 10^5$, $1 \le k \le 2 \cdot 10^5$) — the number of vertices in the tree and the number of operations, respectively. Then $n - 1$ lines follow, each of them contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \neq v$) which describe a pair of vertices connected by an edge. It is guaranteed that the given graph is a tree and has no loops or multiple edges. It is guaranteed that the sum of $n$ from all test cases does not exceed $4 \cdot 10^5$. -----Output----- For each test case output on a separate line a single integer — the number of vertices that remain in the tree after applying $k$ operations. -----Examples----- Input 6 14 1 1 2 2 3 2 4 4 5 4 6 2 7 7 8 8 9 8 10 3 11 3 12 1 13 13 14 2 200000 1 2 3 2 1 2 2 3 5 1 5 1 3 2 2 1 5 4 6 2 5 1 2 5 5 6 4 2 3 4 7 1 4 3 5 1 1 3 6 1 1 7 2 1 Output 7 0 0 3 1 2 -----Note----- The first test case is considered in the statement. The second test case contains a tree of two vertices. $200000$ operations are applied to it. The first one removes all two vertices, the other operations do not change the tree. In the third test case, a tree of three vertices is given. As a result of the first operation, only $1$ vertex remains in it (with the index $2$), the second operation makes the tree empty.
T = int(input()) for t in range(T): input() V, K = map(int, input().split()) adj = [set() for i in range(V)] for i in range(V - 1): u, v = map(int, input().split()) adj[u - 1].add(v - 1) adj[v - 1].add(u - 1) leaves = [] for i in range(V): if len(adj[i]) == 1: leaves.append(i) rem = V while rem > 2 and K > 0: K -= 1 rem -= len(leaves) temp = [] while leaves: leaf = leaves.pop() succ = adj[leaf].pop() adj[succ].remove(leaf) if len(adj[succ]) == 1: temp.append(succ) leaves = temp if K == 0: print(rem) else: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
A tree is an undirected connected graph in which there are no cycles. This problem is about non-rooted trees. A leaf of a tree is a vertex that is connected to at most one vertex. The gardener Vitaly grew a tree from $n$ vertices. He decided to trim the tree. To do this, he performs a number of operations. In one operation, he removes all leaves of the tree. Example of a tree. For example, consider the tree shown in the figure above. The figure below shows the result of applying exactly one operation to the tree. The result of applying the operation "remove all leaves" to the tree. Note the special cases of the operation: applying an operation to an empty tree (of $0$ vertices) does not change it; applying an operation to a tree of one vertex removes this vertex (this vertex is treated as a leaf); applying an operation to a tree of two vertices removes both vertices (both vertices are treated as leaves). Vitaly applied $k$ operations sequentially to the tree. How many vertices remain? -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. Each test case is preceded by an empty line. Each test case consists of several lines. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 4 \cdot 10^5$, $1 \le k \le 2 \cdot 10^5$) — the number of vertices in the tree and the number of operations, respectively. Then $n - 1$ lines follow, each of them contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \neq v$) which describe a pair of vertices connected by an edge. It is guaranteed that the given graph is a tree and has no loops or multiple edges. It is guaranteed that the sum of $n$ from all test cases does not exceed $4 \cdot 10^5$. -----Output----- For each test case output on a separate line a single integer — the number of vertices that remain in the tree after applying $k$ operations. -----Examples----- Input 6 14 1 1 2 2 3 2 4 4 5 4 6 2 7 7 8 8 9 8 10 3 11 3 12 1 13 13 14 2 200000 1 2 3 2 1 2 2 3 5 1 5 1 3 2 2 1 5 4 6 2 5 1 2 5 5 6 4 2 3 4 7 1 4 3 5 1 1 3 6 1 1 7 2 1 Output 7 0 0 3 1 2 -----Note----- The first test case is considered in the statement. The second test case contains a tree of two vertices. $200000$ operations are applied to it. The first one removes all two vertices, the other operations do not change the tree. In the third test case, a tree of three vertices is given. As a result of the first operation, only $1$ vertex remains in it (with the index $2$), the second operation makes the tree empty.
def f(): input() n, k = map(int, input().split()) if n == 1: print(0 if k else 1) return g = [set() for i in range(n)] q = set() for i in range(n - 1): a, b = map(int, input().split()) a -= 1 b -= 1 g[a].add(b) g[b].add(a) for i in range(n): if len(g[i]) == 1: q.add(i) ans = set() while k and q: k -= 1 p = set() for i in q: ans.add(i) if len(g[i]) == 0: continue t = list(g[i])[0] g[t].discard(i) if len(g[t]) == 1: p.add(t) q = p - q print(n - len(ans)) for _ in range(int(input())): f()
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER RETURN ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
A tree is an undirected connected graph in which there are no cycles. This problem is about non-rooted trees. A leaf of a tree is a vertex that is connected to at most one vertex. The gardener Vitaly grew a tree from $n$ vertices. He decided to trim the tree. To do this, he performs a number of operations. In one operation, he removes all leaves of the tree. Example of a tree. For example, consider the tree shown in the figure above. The figure below shows the result of applying exactly one operation to the tree. The result of applying the operation "remove all leaves" to the tree. Note the special cases of the operation: applying an operation to an empty tree (of $0$ vertices) does not change it; applying an operation to a tree of one vertex removes this vertex (this vertex is treated as a leaf); applying an operation to a tree of two vertices removes both vertices (both vertices are treated as leaves). Vitaly applied $k$ operations sequentially to the tree. How many vertices remain? -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. Each test case is preceded by an empty line. Each test case consists of several lines. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 4 \cdot 10^5$, $1 \le k \le 2 \cdot 10^5$) — the number of vertices in the tree and the number of operations, respectively. Then $n - 1$ lines follow, each of them contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \neq v$) which describe a pair of vertices connected by an edge. It is guaranteed that the given graph is a tree and has no loops or multiple edges. It is guaranteed that the sum of $n$ from all test cases does not exceed $4 \cdot 10^5$. -----Output----- For each test case output on a separate line a single integer — the number of vertices that remain in the tree after applying $k$ operations. -----Examples----- Input 6 14 1 1 2 2 3 2 4 4 5 4 6 2 7 7 8 8 9 8 10 3 11 3 12 1 13 13 14 2 200000 1 2 3 2 1 2 2 3 5 1 5 1 3 2 2 1 5 4 6 2 5 1 2 5 5 6 4 2 3 4 7 1 4 3 5 1 1 3 6 1 1 7 2 1 Output 7 0 0 3 1 2 -----Note----- The first test case is considered in the statement. The second test case contains a tree of two vertices. $200000$ operations are applied to it. The first one removes all two vertices, the other operations do not change the tree. In the third test case, a tree of three vertices is given. As a result of the first operation, only $1$ vertex remains in it (with the index $2$), the second operation makes the tree empty.
for _ in range(int(input())): __ = input() n, k = map(int, input().split()) tt = k d = {i: [] for i in range(n)} ideg = [(0) for _ in range(n)] v = [(0) for _ in range(n)] for i in range(n - 1): a, b = map(int, input().split()) d[a - 1].append(b - 1) d[b - 1].append(a - 1) ideg[b - 1] += 1 ideg[a - 1] += 1 q = [] if k: for i in range(n): if ideg[i] == 1: q.append(i) v[i] = 1 k -= 1 while q and k: q1 = [] for node in q: for child in d[node]: if v[child] == 0: ideg[child] -= 1 if ideg[child] == 1: v[child] = 1 q1.append(child) q = q1 k -= 1 if n == 1: if tt: print(0) else: print(1) else: print(v.count(0))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR LIST 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 BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST IF VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER
A tree is an undirected connected graph in which there are no cycles. This problem is about non-rooted trees. A leaf of a tree is a vertex that is connected to at most one vertex. The gardener Vitaly grew a tree from $n$ vertices. He decided to trim the tree. To do this, he performs a number of operations. In one operation, he removes all leaves of the tree. Example of a tree. For example, consider the tree shown in the figure above. The figure below shows the result of applying exactly one operation to the tree. The result of applying the operation "remove all leaves" to the tree. Note the special cases of the operation: applying an operation to an empty tree (of $0$ vertices) does not change it; applying an operation to a tree of one vertex removes this vertex (this vertex is treated as a leaf); applying an operation to a tree of two vertices removes both vertices (both vertices are treated as leaves). Vitaly applied $k$ operations sequentially to the tree. How many vertices remain? -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. Each test case is preceded by an empty line. Each test case consists of several lines. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 4 \cdot 10^5$, $1 \le k \le 2 \cdot 10^5$) — the number of vertices in the tree and the number of operations, respectively. Then $n - 1$ lines follow, each of them contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \neq v$) which describe a pair of vertices connected by an edge. It is guaranteed that the given graph is a tree and has no loops or multiple edges. It is guaranteed that the sum of $n$ from all test cases does not exceed $4 \cdot 10^5$. -----Output----- For each test case output on a separate line a single integer — the number of vertices that remain in the tree after applying $k$ operations. -----Examples----- Input 6 14 1 1 2 2 3 2 4 4 5 4 6 2 7 7 8 8 9 8 10 3 11 3 12 1 13 13 14 2 200000 1 2 3 2 1 2 2 3 5 1 5 1 3 2 2 1 5 4 6 2 5 1 2 5 5 6 4 2 3 4 7 1 4 3 5 1 1 3 6 1 1 7 2 1 Output 7 0 0 3 1 2 -----Note----- The first test case is considered in the statement. The second test case contains a tree of two vertices. $200000$ operations are applied to it. The first one removes all two vertices, the other operations do not change the tree. In the third test case, a tree of three vertices is given. As a result of the first operation, only $1$ vertex remains in it (with the index $2$), the second operation makes the tree empty.
for _ in range(int(input())): input() n, k = map(int, input().split()) graph = [[] for _ in range(n + 1)] remain = [0] * (n + 1) layer = [0] * (n + 1) for _ in range(n - 1): u, v = map(int, input().split()) graph[u].append(v) graph[v].append(u) remain[u] += 1 remain[v] += 1 leaves = [] for i in range(1, n + 1): if remain[i] == 1: leaves.append(i) layer[i] = 1 for i in leaves: for j in graph[i]: if remain[j] != 1: remain[j] -= 1 if remain[j] == 1: layer[j] = layer[i] + 1 leaves.append(j) ans = 0 for i in range(1, n + 1): if layer[i] > k: ans += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
A tree is an undirected connected graph in which there are no cycles. This problem is about non-rooted trees. A leaf of a tree is a vertex that is connected to at most one vertex. The gardener Vitaly grew a tree from $n$ vertices. He decided to trim the tree. To do this, he performs a number of operations. In one operation, he removes all leaves of the tree. Example of a tree. For example, consider the tree shown in the figure above. The figure below shows the result of applying exactly one operation to the tree. The result of applying the operation "remove all leaves" to the tree. Note the special cases of the operation: applying an operation to an empty tree (of $0$ vertices) does not change it; applying an operation to a tree of one vertex removes this vertex (this vertex is treated as a leaf); applying an operation to a tree of two vertices removes both vertices (both vertices are treated as leaves). Vitaly applied $k$ operations sequentially to the tree. How many vertices remain? -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. Each test case is preceded by an empty line. Each test case consists of several lines. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 4 \cdot 10^5$, $1 \le k \le 2 \cdot 10^5$) — the number of vertices in the tree and the number of operations, respectively. Then $n - 1$ lines follow, each of them contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \neq v$) which describe a pair of vertices connected by an edge. It is guaranteed that the given graph is a tree and has no loops or multiple edges. It is guaranteed that the sum of $n$ from all test cases does not exceed $4 \cdot 10^5$. -----Output----- For each test case output on a separate line a single integer — the number of vertices that remain in the tree after applying $k$ operations. -----Examples----- Input 6 14 1 1 2 2 3 2 4 4 5 4 6 2 7 7 8 8 9 8 10 3 11 3 12 1 13 13 14 2 200000 1 2 3 2 1 2 2 3 5 1 5 1 3 2 2 1 5 4 6 2 5 1 2 5 5 6 4 2 3 4 7 1 4 3 5 1 1 3 6 1 1 7 2 1 Output 7 0 0 3 1 2 -----Note----- The first test case is considered in the statement. The second test case contains a tree of two vertices. $200000$ operations are applied to it. The first one removes all two vertices, the other operations do not change the tree. In the third test case, a tree of three vertices is given. As a result of the first operation, only $1$ vertex remains in it (with the index $2$), the second operation makes the tree empty.
t = int(input()) for _ in range(t): empty = input() ni, k = map(int, input().split()) neighbors = {i: [] for i in range(ni)} for i in range(ni - 1): v, w = map(int, input().split()) neighbors[v - 1].append(w - 1) neighbors[w - 1].append(v - 1) time = {} layer = [] for i in range(ni): if len(neighbors[i]) <= 1: time[i] = 1 layer.append(i) dist = 2 while len(time) < ni and dist <= k: newlayer = set() for v in layer: for n in neighbors[v]: if n not in newlayer and n not in time: alive = 0 for nn in neighbors[n]: if nn not in time or time[nn] == dist: alive += 1 if alive == 2: break if alive < 2: newlayer.add(n) time[n] = dist dist += 1 layer = newlayer print(ni - len(time))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
A tree is an undirected connected graph in which there are no cycles. This problem is about non-rooted trees. A leaf of a tree is a vertex that is connected to at most one vertex. The gardener Vitaly grew a tree from $n$ vertices. He decided to trim the tree. To do this, he performs a number of operations. In one operation, he removes all leaves of the tree. Example of a tree. For example, consider the tree shown in the figure above. The figure below shows the result of applying exactly one operation to the tree. The result of applying the operation "remove all leaves" to the tree. Note the special cases of the operation: applying an operation to an empty tree (of $0$ vertices) does not change it; applying an operation to a tree of one vertex removes this vertex (this vertex is treated as a leaf); applying an operation to a tree of two vertices removes both vertices (both vertices are treated as leaves). Vitaly applied $k$ operations sequentially to the tree. How many vertices remain? -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. Each test case is preceded by an empty line. Each test case consists of several lines. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 4 \cdot 10^5$, $1 \le k \le 2 \cdot 10^5$) — the number of vertices in the tree and the number of operations, respectively. Then $n - 1$ lines follow, each of them contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \neq v$) which describe a pair of vertices connected by an edge. It is guaranteed that the given graph is a tree and has no loops or multiple edges. It is guaranteed that the sum of $n$ from all test cases does not exceed $4 \cdot 10^5$. -----Output----- For each test case output on a separate line a single integer — the number of vertices that remain in the tree after applying $k$ operations. -----Examples----- Input 6 14 1 1 2 2 3 2 4 4 5 4 6 2 7 7 8 8 9 8 10 3 11 3 12 1 13 13 14 2 200000 1 2 3 2 1 2 2 3 5 1 5 1 3 2 2 1 5 4 6 2 5 1 2 5 5 6 4 2 3 4 7 1 4 3 5 1 1 3 6 1 1 7 2 1 Output 7 0 0 3 1 2 -----Note----- The first test case is considered in the statement. The second test case contains a tree of two vertices. $200000$ operations are applied to it. The first one removes all two vertices, the other operations do not change the tree. In the third test case, a tree of three vertices is given. As a result of the first operation, only $1$ vertex remains in it (with the index $2$), the second operation makes the tree empty.
import sys input = sys.stdin.readline def solution(edges: list, n, k): if k > n / 2: return 0 adj_list = [set() for _ in range(n)] for u, v in edges: adj_list[u].add(v) adj_list[v].add(u) n_removed = 0 degree = [len(adj_list[v]) for v in range(n)] leaves = [v for v in range(n) if degree[v] == 1] for _ in range(k): if n_removed == n - 1: return 0 neighbors = set() for leaf in leaves: neighbors.update(adj_list[leaf]) degree[leaf] = 0 n_removed += 1 for neighbor in adj_list[leaf]: degree[neighbor] -= 1 leaves = [v for v in neighbors if degree[v] == 1] return n - n_removed def main(): for _ in range(int(input())): input() n, k = map(int, input().split()) edges = [ tuple(map(lambda x: int(x) - 1, input().split())) for _ in range(n - 1) ] print(solution(edges, n, k)) def test(): edges = [(1, 0), (2, 1)] print(solution(edges, 3, 2)) main()
IMPORT ASSIGN VAR VAR FUNC_DEF VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR
A tree is an undirected connected graph in which there are no cycles. This problem is about non-rooted trees. A leaf of a tree is a vertex that is connected to at most one vertex. The gardener Vitaly grew a tree from $n$ vertices. He decided to trim the tree. To do this, he performs a number of operations. In one operation, he removes all leaves of the tree. Example of a tree. For example, consider the tree shown in the figure above. The figure below shows the result of applying exactly one operation to the tree. The result of applying the operation "remove all leaves" to the tree. Note the special cases of the operation: applying an operation to an empty tree (of $0$ vertices) does not change it; applying an operation to a tree of one vertex removes this vertex (this vertex is treated as a leaf); applying an operation to a tree of two vertices removes both vertices (both vertices are treated as leaves). Vitaly applied $k$ operations sequentially to the tree. How many vertices remain? -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. Each test case is preceded by an empty line. Each test case consists of several lines. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 4 \cdot 10^5$, $1 \le k \le 2 \cdot 10^5$) — the number of vertices in the tree and the number of operations, respectively. Then $n - 1$ lines follow, each of them contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \neq v$) which describe a pair of vertices connected by an edge. It is guaranteed that the given graph is a tree and has no loops or multiple edges. It is guaranteed that the sum of $n$ from all test cases does not exceed $4 \cdot 10^5$. -----Output----- For each test case output on a separate line a single integer — the number of vertices that remain in the tree after applying $k$ operations. -----Examples----- Input 6 14 1 1 2 2 3 2 4 4 5 4 6 2 7 7 8 8 9 8 10 3 11 3 12 1 13 13 14 2 200000 1 2 3 2 1 2 2 3 5 1 5 1 3 2 2 1 5 4 6 2 5 1 2 5 5 6 4 2 3 4 7 1 4 3 5 1 1 3 6 1 1 7 2 1 Output 7 0 0 3 1 2 -----Note----- The first test case is considered in the statement. The second test case contains a tree of two vertices. $200000$ operations are applied to it. The first one removes all two vertices, the other operations do not change the tree. In the third test case, a tree of three vertices is given. As a result of the first operation, only $1$ vertex remains in it (with the index $2$), the second operation makes the tree empty.
def gardener_and_tree(edges, n, k): next_step = [] for vertex in edges: if len(edges[vertex]) <= 1: next_step += [vertex] for j in range(k): to_remove = next_step if len(to_remove) == 0: return 0 next_step = [] for vertex_to_remove in to_remove: try: connected = edges[vertex_to_remove].pop() edges[connected].remove(vertex_to_remove) if len(edges[connected]) == 1: next_step += [connected] except KeyError: pass del edges[vertex_to_remove] count = len(edges.keys()) return count t = int(input()) for _ in range(t): input() n, k = map(int, input().split()) edges = {} for _ in range(n - 1): a, b = map(int, input().split(" ")) if a in edges: edges[a].add(b) else: edges[a] = {b} if b in edges: edges[b].add(a) else: edges[b] = {a} print(gardener_and_tree(edges, n, k))
FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR LIST VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR LIST VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
A tree is an undirected connected graph in which there are no cycles. This problem is about non-rooted trees. A leaf of a tree is a vertex that is connected to at most one vertex. The gardener Vitaly grew a tree from $n$ vertices. He decided to trim the tree. To do this, he performs a number of operations. In one operation, he removes all leaves of the tree. Example of a tree. For example, consider the tree shown in the figure above. The figure below shows the result of applying exactly one operation to the tree. The result of applying the operation "remove all leaves" to the tree. Note the special cases of the operation: applying an operation to an empty tree (of $0$ vertices) does not change it; applying an operation to a tree of one vertex removes this vertex (this vertex is treated as a leaf); applying an operation to a tree of two vertices removes both vertices (both vertices are treated as leaves). Vitaly applied $k$ operations sequentially to the tree. How many vertices remain? -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. Each test case is preceded by an empty line. Each test case consists of several lines. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 4 \cdot 10^5$, $1 \le k \le 2 \cdot 10^5$) — the number of vertices in the tree and the number of operations, respectively. Then $n - 1$ lines follow, each of them contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \neq v$) which describe a pair of vertices connected by an edge. It is guaranteed that the given graph is a tree and has no loops or multiple edges. It is guaranteed that the sum of $n$ from all test cases does not exceed $4 \cdot 10^5$. -----Output----- For each test case output on a separate line a single integer — the number of vertices that remain in the tree after applying $k$ operations. -----Examples----- Input 6 14 1 1 2 2 3 2 4 4 5 4 6 2 7 7 8 8 9 8 10 3 11 3 12 1 13 13 14 2 200000 1 2 3 2 1 2 2 3 5 1 5 1 3 2 2 1 5 4 6 2 5 1 2 5 5 6 4 2 3 4 7 1 4 3 5 1 1 3 6 1 1 7 2 1 Output 7 0 0 3 1 2 -----Note----- The first test case is considered in the statement. The second test case contains a tree of two vertices. $200000$ operations are applied to it. The first one removes all two vertices, the other operations do not change the tree. In the third test case, a tree of three vertices is given. As a result of the first operation, only $1$ vertex remains in it (with the index $2$), the second operation makes the tree empty.
import sys N = int(1000000.0 + 5) sys.setrecursionlimit(N) n, k = int(), int() adj = list() def charming(): global n, k, adj input() n, k = map(int, input().split()) adj = list(list() for i in range(n + 1)) vis = set() du = [0] * (n + 1) for i in range(n - 1): u, v = map(int, input().split()) adj[u].append(v) adj[v].append(u) du[u] += 1 du[v] += 1 leaf = list() for i in range(1, n + 1): if du[i] <= 1: leaf.append(i) vis.add(i) nleaf = list() for i in range(k - 1): nleaf = list() for u in leaf: for v in adj[u]: if v in vis: continue du[v] -= 1 if du[v] <= 1: vis.add(v) nleaf.append(v) leaf = nleaf if len(leaf) <= 0: break print(n - len(vis)) for t in range(int(input())): charming()
IMPORT ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
A tree is an undirected connected graph in which there are no cycles. This problem is about non-rooted trees. A leaf of a tree is a vertex that is connected to at most one vertex. The gardener Vitaly grew a tree from $n$ vertices. He decided to trim the tree. To do this, he performs a number of operations. In one operation, he removes all leaves of the tree. Example of a tree. For example, consider the tree shown in the figure above. The figure below shows the result of applying exactly one operation to the tree. The result of applying the operation "remove all leaves" to the tree. Note the special cases of the operation: applying an operation to an empty tree (of $0$ vertices) does not change it; applying an operation to a tree of one vertex removes this vertex (this vertex is treated as a leaf); applying an operation to a tree of two vertices removes both vertices (both vertices are treated as leaves). Vitaly applied $k$ operations sequentially to the tree. How many vertices remain? -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. Each test case is preceded by an empty line. Each test case consists of several lines. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 4 \cdot 10^5$, $1 \le k \le 2 \cdot 10^5$) — the number of vertices in the tree and the number of operations, respectively. Then $n - 1$ lines follow, each of them contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \neq v$) which describe a pair of vertices connected by an edge. It is guaranteed that the given graph is a tree and has no loops or multiple edges. It is guaranteed that the sum of $n$ from all test cases does not exceed $4 \cdot 10^5$. -----Output----- For each test case output on a separate line a single integer — the number of vertices that remain in the tree after applying $k$ operations. -----Examples----- Input 6 14 1 1 2 2 3 2 4 4 5 4 6 2 7 7 8 8 9 8 10 3 11 3 12 1 13 13 14 2 200000 1 2 3 2 1 2 2 3 5 1 5 1 3 2 2 1 5 4 6 2 5 1 2 5 5 6 4 2 3 4 7 1 4 3 5 1 1 3 6 1 1 7 2 1 Output 7 0 0 3 1 2 -----Note----- The first test case is considered in the statement. The second test case contains a tree of two vertices. $200000$ operations are applied to it. The first one removes all two vertices, the other operations do not change the tree. In the third test case, a tree of three vertices is given. As a result of the first operation, only $1$ vertex remains in it (with the index $2$), the second operation makes the tree empty.
def remove_leaves( graph, leaves, removed_leaves, removed_neighbours_count, beginning_neighbours_count ): removed_leaves.update(leaves) new_leaves = set() for leave in leaves: for neighbour in graph[leave]: if neighbour not in removed_leaves: removed_neighbours_count[neighbour] += 1 if ( removed_neighbours_count[neighbour] >= beginning_neighbours_count[neighbour] - 1 ): new_leaves.add(neighbour) return list(new_leaves) tests = int(input()) for i in range(tests): empty_line = input() [vertex_count, operations] = [int(el) for el in input().split()] graph = [[] for t in range(vertex_count)] for j in range(vertex_count - 1): [u, w] = [int(el) for el in input().split()] graph[u - 1].append(w - 1) graph[w - 1].append(u - 1) visited = [False] * vertex_count leaves = [idx for idx, el in enumerate(graph) if len(el) in [0, 1]] beginning_neighbours_count = [len(el) for el in graph] removed_neighbours_count = [0] * vertex_count removed_leaves = set() for _ in range(operations): leaves = remove_leaves( graph, leaves, removed_leaves, removed_neighbours_count, beginning_neighbours_count, ) if len(graph) == len(removed_leaves): break print(len(graph) - len(removed_leaves))
FUNC_DEF EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR
A tree is an undirected connected graph in which there are no cycles. This problem is about non-rooted trees. A leaf of a tree is a vertex that is connected to at most one vertex. The gardener Vitaly grew a tree from $n$ vertices. He decided to trim the tree. To do this, he performs a number of operations. In one operation, he removes all leaves of the tree. Example of a tree. For example, consider the tree shown in the figure above. The figure below shows the result of applying exactly one operation to the tree. The result of applying the operation "remove all leaves" to the tree. Note the special cases of the operation: applying an operation to an empty tree (of $0$ vertices) does not change it; applying an operation to a tree of one vertex removes this vertex (this vertex is treated as a leaf); applying an operation to a tree of two vertices removes both vertices (both vertices are treated as leaves). Vitaly applied $k$ operations sequentially to the tree. How many vertices remain? -----Input----- The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Then $t$ test cases follow. Each test case is preceded by an empty line. Each test case consists of several lines. The first line of the test case contains two integers $n$ and $k$ ($1 \le n \le 4 \cdot 10^5$, $1 \le k \le 2 \cdot 10^5$) — the number of vertices in the tree and the number of operations, respectively. Then $n - 1$ lines follow, each of them contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \neq v$) which describe a pair of vertices connected by an edge. It is guaranteed that the given graph is a tree and has no loops or multiple edges. It is guaranteed that the sum of $n$ from all test cases does not exceed $4 \cdot 10^5$. -----Output----- For each test case output on a separate line a single integer — the number of vertices that remain in the tree after applying $k$ operations. -----Examples----- Input 6 14 1 1 2 2 3 2 4 4 5 4 6 2 7 7 8 8 9 8 10 3 11 3 12 1 13 13 14 2 200000 1 2 3 2 1 2 2 3 5 1 5 1 3 2 2 1 5 4 6 2 5 1 2 5 5 6 4 2 3 4 7 1 4 3 5 1 1 3 6 1 1 7 2 1 Output 7 0 0 3 1 2 -----Note----- The first test case is considered in the statement. The second test case contains a tree of two vertices. $200000$ operations are applied to it. The first one removes all two vertices, the other operations do not change the tree. In the third test case, a tree of three vertices is given. As a result of the first operation, only $1$ vertex remains in it (with the index $2$), the second operation makes the tree empty.
import sys input = sys.stdin.readline def solve(): input() N, K = map(int, input().split()) if N == 1: print(0) return G = [set() for _ in range(N)] for _ in range(N - 1): u, v = map(int, input().split()) G[u - 1].add(v - 1) G[v - 1].add(u - 1) ans = N rm = set(u for u in range(N) if len(G[u]) == 1) for _ in range(K): if not rm: break ans -= len(rm) rm2 = set() for u in rm: if len(G[u]) == 0: continue v = next(iter(G[u])) G[v].remove(u) if len(G[v]) == 1: rm2.add(v) rm = rm2 - rm print(ans) for _ in range(int(input())): solve()
IMPORT ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
n, m = list(map(int, input().split())) if n == 1: print(0) exit(0) t = list(map(int, input().split())) dp = [0] * 101 ans = [] for i in range(len(t)): s = 0 lim = m - t[i] f = False c = 0 for j in range(1, 101): if f: c += dp[j] continue if dp[j]: if s + dp[j] * j > lim: c += dp[j] - (lim - s) // j f = True s += dp[j] * j dp[t[i]] += 1 ans.append(c) print(*ans)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR VAR VAR IF VAR VAR IF BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
n, m = map(int, input().split()) a = list(map(int, input().split())) cnt = [0] * 101 curr = 0 for i in range(n): tm = m - a[i] ans = 0 for j in range(1, 101): tmp = int(tm / j) if tmp >= cnt[j]: tm -= cnt[j] * j ans += cnt[j] else: tm -= tmp * j ans += tmp print(i - ans, end=" ") cnt[a[i]] += 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR STRING VAR VAR VAR NUMBER
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
def mp(): return map(int, input().split()) n, m = mp() a = list(mp()) s = [0] * (n + 1) for i in range(n): s[i] = s[i - 1] + a[i] cnt = [0] * 101 for i in range(n): ans = 0 t = s[i] while t > m: for j in range(100, 0, -1): if cnt[j] != 0: k = max(0, (t - m + j - 1) // j) ans += min(k, cnt[j]) t -= min(k, cnt[j]) * j cnt[a[i]] += 1 print(ans, end=" ")
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
n, t = map(int, input().split()) arr = list(map(int, input().split())) v = 0 ans = [0] * 101 for i in range(n): cnt = 0 v += arr[i] extra = v - t k = 100 while extra > 0 and k > 0: if ans[k] * k >= extra: cnt += (extra + k - 1) // k break elif ans[k] * k < extra: extra -= ans[k] * k cnt += ans[k] k -= 1 ans[arr[i]] += 1 print(cnt, end=" ")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
n, m = map(int, input().strip().split()) a = [int(x) for x in input().strip().split()] t = [(0) for i in range(0, 101)] for i in range(0, n): c, j, s = a[i], 1, 0 flag = 0 while j < 101: cnt = min(t[j], (m - c) // j) s += cnt c += j * cnt if cnt < t[j]: flag = 1 break j += 1 if flag == 0: print(0, end=" ") else: print(i - s, end=" ") t[a[i]] += 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR VAR STRING VAR VAR VAR NUMBER
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
n, m = map(int, input().split()) s = list(map(int, input().split())) sas = s.copy() g = 0 ans = [] for i in range(1, len(s)): s[i] += s[i - 1] h = [(0) for i in range(101)] for i in range(len(s)): g = 0 if s[i] <= m: ans.append(0) else: posos = s[i] - m for j in range(100, -1, -1): if h[j] * j >= posos: ans.append((posos + j - 1) // j + g) break elif h[j] > 0: g += h[j] posos -= h[j] * j h[sas[i]] += 1 print(*ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
a, b = map(int, input().split()) z1 = [0] * 101 z = list(map(int, input().split())) for i in range(a): s = z[i] p = 0 for j in range(1, 101): k = j * z1[j] if s + k <= b: s += k else: f = b - s p = sum(z1[j + 1 :]) + z1[j] - f // j break z1[z[i]] += 1 print(p, end=" ")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
from sys import stdout n, m = map(int, input().split()) a = list(map(int, input().split())) dp = 0 size = max(a) + 1 b = [0] * size for i in range(n): e = a[i] k = 1 c = 0 s_c = m - e while k < size: if b[k] != 0: if s_c < b[k] * k: c += s_c // k break else: s_c -= b[k] * k c += b[k] k += 1 b[e] += 1 stdout.write(str(i - c) + " ")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR IF VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR STRING
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
import sys import time sys.setrecursionlimit(100000) input = sys.stdin.readline def read_int(): return int(input()) def read_int_n(): return list(map(int, input().strip().split())) def read_float(): return float(input()) def read_float_n(): return list(map(float, input().split())) def read_str(): return input().strip() def read_str_n(): return list(map(str, input().split())) def error_print(*args): print(*args, file=sys.stderr) def mt(f): import time def wrap(*args, **kwargs): s = time.time() ret = f(*args, **kwargs) e = time.time() error_print(e - s, "sec") return ret return wrap def slv(N, M, S): ans = [] m = 0 c = [0] * (max(S) + 1) for i, s in enumerate(S): sm = 0 sp = 0 m = M - s for j in range(1, len(c)): if m < sm + c[j] * j: sp += (m - sm) // j break else: sm += c[j] * j sp += c[j] ans.append(i - sp) c[s] += 1 return " ".join(map(str, ans)) def main(): N, M = read_int_n() S = read_int_n() print(slv(N, M, S)) main()
IMPORT IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR STRING RETURN VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
n, m = map(int, input().split()) a = list(map(int, input().split())) ans = [] ma = 0 tc = [0] * (max(a) + 1) for i, k in enumerate(a): tcm = 0 dop = 0 ma = m - k for j in range(1, len(tc)): if ma < tcm + tc[j] * j: dop += (ma - tcm) // j break else: tcm += tc[j] * j dop += tc[j] ans.append(i - dop) tc[k] += 1 print(*ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
import sys input = sys.stdin.readline def main(): N, M = map(int, input().split()) A = list(map(int, input().split())) T = [0] * 101 s = 0 ans = [] for a in A: s += a ss = max(s - M, 0) count = 0 for t in range(100, 0, -1): c = T[t] if ss - t * c > 0: count += c ss -= t * c else: count += ss // t if ss % t != 0: count += 1 break T[a] += 1 ans.append(count) for a in ans: print(a, end=" ") print() main()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
def main1(): buf = input() buflist = buf.split() n = int(buflist[0]) M = int(buflist[1]) buf = input() buflist = buf.split() t = list(map(int, buflist)) student = [] minimum = [] for i in range(n): count = 0 pointer = 0 while pointer < i: if count + student[pointer] <= M - t[i]: count += student[pointer] pointer += 1 else: break minimum.append(i - pointer) student.append(t[i]) student.sort() print(" ".join(list(map(str, minimum)))) def main2(): buf = input() buflist = buf.split() n = int(buflist[0]) M = int(buflist[1]) buf = input() buflist = buf.split() t = list(map(int, buflist)) bucket = [] for i in range(101): bucket.append(0) minimum = [] for i in range(n): target = M - t[i] count = 0 subtotal = 0 for j in range(1, 101): if subtotal + bucket[j] * j > target: count += (target - subtotal) // j break else: count += bucket[j] subtotal += bucket[j] * j minimum.append(i - count) bucket[t[i]] += 1 print(" ".join(list(map(str, minimum)))) def __starting_point(): main2() __starting_point()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
n, m = map(int, input().split()) array = list(map(int, input().split())) counts = [0] * 101 answer = [] s = 0 for i in range(n): s += array[i] ans = 0 s_in_iteration = s - m for j in range(100, 0, -1): if s_in_iteration > 0: count = (s_in_iteration + j - 1) // j if count > counts[j]: s_in_iteration -= counts[j] * j ans += counts[j] else: ans += count break else: break answer.append(ans) counts[array[i]] += 1 print(*answer)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
import sys def nline(): return sys.stdin.readline()[:-1] def ni(): return int(sys.stdin.readline()) def na(): return [int(v) for v in sys.stdin.readline().split()] n, m = na() arr = na() sm = 0 count = [0] * 101 for i in range(0, n): diff, k = sm + arr[i] - m, 0 if diff > 0: for j in range(100, -1, -1): tmp = count[j] * j if tmp >= diff: k += (diff + j - 1) // j break k += count[j] diff -= tmp print(k, end=" ") sm += arr[i] count[arr[i]] += 1
IMPORT FUNC_DEF RETURN FUNC_CALL VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR VAR VAR VAR VAR VAR NUMBER
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
n, m = map(int, input().split()) l = list(map(int, input().split())) a = [(0) for i in range(101)] ans = [] for i in range(n): if i == 0: a[l[i]] += 1 ans.append(0) else: s = 0 t = m - l[i] count = 0 for j in range(1, 101): if a[j] * j < t - s: s += a[j] * j count += a[j] else: u = (t - s) // j ans.append(i - count - u) break else: ans.append(0) a[l[i]] += 1 for i in ans: print(i, end=" ")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
n, m = map(int, input().strip().split()) arr = list(map(int, input().strip().split())) cnt = [0] * 101 curr = 0 for i in range(n): res = 0 val = curr + arr[i] - m t = 100 while t > 0 and val > 0: d = min(cnt[t], (val + t - 1) // t) res += d val -= d * t t -= 1 print(res, end=" ") curr += arr[i] cnt[arr[i]] += 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR VAR VAR VAR VAR VAR NUMBER
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
import sys input = sys.stdin.readline n, M = list(map(int, input().split())) t = list(map(int, input().split())) ans = [] tmp = [0] * 101 for i in range(n): num = 0 T = t[i] for j in range(1, 101): if T + j * tmp[j] <= M: num += tmp[j] T += j * tmp[j] else: m = M - T num += m // j break ans.append(i - num) tmp[t[i]] += 1 print(*ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
a = [int(i) for i in input().split()] n = a[0] M = a[1] x = [int(i) for i in input().split()] pref = [(0) for i in x] pref[0] = x[0] freq = [(0) for i in range(100)] for i in range(1, n): pref[i] = pref[i - 1] + x[i] for j in range(n): ans = pref[j] if ans <= M: print(0, end=" ") else: k = 0 while ans > M: for i in range(99, -1, -1): if freq[i] == 0: continue y = (ans - M) // (i + 1) if y == 0: ans = ans - (i + 1) k += 1 elif freq[i] <= y: ans = ans - freq[i] * (i + 1) k += freq[i] elif ans - y * (i + 1) <= M: ans = ans - y * (i + 1) k += y else: ans = ans - (y + 1) * (i + 1) k += y + 1 if ans <= M: break print(k, end=" ") freq[x[j] - 1] += 1
ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER STRING ASSIGN VAR NUMBER WHILE VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR BIN_OP VAR VAR NUMBER NUMBER
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
def ri(): return [int(i) for i in input().split()] def fl(a, b): return abs(a - b) def gt_mx(d, l): ans = 0 for i in reversed(range(1, 110)): if l < 0: break if d[i] > 0: do = min(l // i, d[i]) ans += do l -= do * i if l > 0 and do < d[i]: l -= i ans += 1 return ans def main(n, m, t): if m >= t[0]: print(0, end=" ") else: print(1, end=" ") d = {i: (0) for i in range(1, 110)} d[t[0]] += 1 sm = t[0] for i in range(1, len(t)): sm += t[i] if i == 5: asd = 123 if sm > m: delit = gt_mx(d, sm - m) print(delit, end=" ") else: print(0, end=" ") d[t[i]] += 1 n, m = ri() t = ri() main(n, m, t)
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER STRING ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR NUMBER STRING VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
R = lambda: map(int, input().split()) n, m = R() cnts = [0] * 105 tcnt = 0 for x in R(): cnt = acc = 0 for i, c in enumerate(cnts): if acc + i * c + x <= m: cnt, acc = cnt + c, acc + i * c else: cnt += (m - acc - x) // i break print(tcnt - cnt, end=" ") cnts[x] += 1 tcnt += 1
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR STRING VAR VAR NUMBER VAR NUMBER
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
arr = list(map(int, input().split())) n, m = arr[0], arr[1] s = str(input()) a = [int(i) for i in s.split()] answers = [] s = 0 count = 0 max_val = 0 k = 0 arr = [(0) for i in range(101)] for elem in a: if s + elem > m: i = max_val s2 = s c2 = count while s2 + elem > m: if s2 - arr[i] * i + elem <= m: ans = k - c2 + (arr[i] - (m - elem - (s2 - arr[i] * i)) // i) s2 -= i * (arr[i] - (m - elem - (s2 - arr[i] * i)) // i) else: s2 -= arr[i] * i c2 -= arr[i] i -= 1 if elem < max_val: arr[elem] += 1 arr[max_val] -= 1 s = s - max_val + elem j = max_val while arr[j] < 1: j -= 1 max_val = j else: arr[elem] += 1 s += elem ans = k - count count += 1 if elem > max_val: max_val = elem answers.append(str(ans)) k += 1 print(" ".join(answers))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
n, M = list(map(int, input().split())) nums = list(map(int, input().split())) count = [0] * 101 tm = 0 for i in range(n): ans = 0 tm += nums[i] if tm > M: extra = tm - M itr = 100 while extra > 0: extra -= itr * count[itr] ans += count[itr] itr -= 1 ans -= -extra // (itr + 1) print(ans) count[nums[i]] += 1
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
n, t = map(int, input().split()) arr = [0] * 101 students = [int(x) for x in input().split()] cur_time = 0 for i in students: cur_time += i ptr = 100 this = cur_time acc = 0 while ptr: if this - arr[ptr] * ptr <= t: acc += (this - t - 1) // ptr + 1 break else: acc += arr[ptr] this -= arr[ptr] * ptr ptr -= 1 print(max(0, acc), end=" ") arr[i] += 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR STRING VAR VAR NUMBER
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
import sys input = sys.stdin.readline n, m = map(int, input().split()) t = list(map(int, input().split())) cnt = [0] * 101 cs = 0 ans = [] for i in range(n): r = cs - m + t[i] res = 0 if r > 0: for j in range(1, 101)[::-1]: if r - cnt[j] * j > 0: r -= cnt[j] * j res += cnt[j] else: res += (r + j - 1) // j break ans.append(res) cs += t[i] cnt[t[i]] += 1 print(*ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
n, m = map(int, input().split()) tot_sum, curr_sum, d = 0, 0, {} arr = list(map(int, input().split())) for x in arr: tot_sum += x curr_sum += x curr_res = 0 for it in sorted(d.keys(), reverse=True): if curr_sum <= m: break take = min(d[it], (curr_sum - m + it - 1) // it) curr_sum -= take * it curr_res += take d[x] = 1 if x not in d else d[x] + 1 curr_sum = tot_sum print(curr_res, end=" ")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR STRING
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
n, M = map(int, input().split()) ls = list(map(int, input().split())) count = {} for i in range(1, 101): count[i] = 0 for i in range(n): Sum = ls[i] cnt = 1 flag = 0 for j in range(1, 101): if Sum + j * count[j] > M: break Sum += j * count[j] cnt += count[j] else: flag = 1 if flag == 0: cnt += (M - Sum) // j Sum += j * ((M - Sum) // j) count[ls[i]] += 1 print(i + 1 - cnt, end=" ") print()
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR STRING EXPR FUNC_CALL VAR
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
n, M = map(int, input().split()) a = list(map(int, input().split())) b = [0] * 110 for x in a: cc = 0 cur = 0 for j in range(1, 101): h = min((M - x - cur) // j, b[j]) cc += h cur += h * j b[x] += 1 print(sum(b) - cc - 1)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
from sys import * buckets = [0] * 101 n, M = map(int, stdin.readline().split()) ts = [int(t) for t in stdin.readline().split()] full_sum = 0 for i in range(n): cur_M = M cur_sum = full_sum bucket_idx = 100 ans = 0 while cur_sum > cur_M - ts[i]: if cur_sum - cur_M + ts[i] >= buckets[bucket_idx] * bucket_idx: cur_sum -= buckets[bucket_idx] * bucket_idx ans += buckets[bucket_idx] else: tmp = (cur_sum - cur_M + ts[i] - 1) // bucket_idx + 1 ans += tmp cur_sum -= bucket_idx * tmp bucket_idx -= 1 stdout.write(str(ans) + " ") full_sum += ts[i] buckets[ts[i]] += 1 stdout.write("\n")
ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
n, m = map(int, input().split()) c = [0] * 101 a = list(map(int, input().split())) for i in range(n): ans = 0 sm = a[i] for j in range(1, 101): k = min((m - sm) // j, c[j]) ans += k sm += k * j print(i - ans) c[a[i]] += 1
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
from sys import stdin input = lambda: stdin.readline().rstrip("\r\n") n, m = map(int, input().split()) t = list(map(int, input().split())) pre = [0] ans = [] d = {} for i in range(1, 101): d[i] = 0 for i in range(n): if pre[-1] + t[i] <= m: ans.append(0) else: x = pre[-1] + t[i] - m c = 0 for j in range(100, 0, -1): if x <= j * d[j]: c += (x + j - 1) // j ans.append(c) break else: x -= j * d[j] c += d[j] pre.append(pre[-1] + t[i]) d[t[i]] += 1 print(*ans)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
x = list(map(int, input().rstrip().split())) y = list(map(int, input().rstrip().split())) l = [0] * 101 out = [0] * x[0] su = 0 for i in range(x[0]): k = 0 d = su + y[i] - x[1] if d > 0: for j in range(100, 0, -1): h = j * l[j] if d <= h: k += (d + j - 1) // j break k += l[j] d -= h su += y[i] l[y[i]] += 1 print(k)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$) — the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$) — time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
R = lambda: map(int, input().split()) n, m = R() L = list(R()) f = [0] * 101 su = 0 for i in range(n): su += L[i] p = su - m c = 0 if p > 0: for j in reversed(range(1, 101)): if f[j] > 0: if p < f[j] * j: c += (p + j - 1) // j break else: c += f[j] p -= j * f[j] print(c, end=" ") f[L[i]] += 1
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING VAR VAR VAR NUMBER