description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$. A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$. Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence. -----Input----- The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) β€” the lengths of the strings $s$ and $t$. The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings. -----Output----- Output one integer β€” the maximum width of a beautiful sequence. -----Examples----- Input 5 3 abbbc abc Output 3 Input 5 2 aaaaa aa Output 4 Input 5 5 abcdf abcdf Output 1 Input 2 2 ab ab Output 1 -----Note----- In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$. In the second example the beautiful sequence with the maximum width is $\{1, 5\}$. In the third example there is exactly one beautiful sequence β€” it is $\{1, 2, 3, 4, 5\}$. In the fourth example there is exactly one beautiful sequence β€” it is $\{1, 2\}$.
from sys import stdin as f def find_good_chain(s, t): x = 0 y = 0 ind = [] while x < len(t): while s[y] != t[x]: y = y + 1 ind.append(y) x = x + 1 y = y + 1 return ind def find_reversed_chain(s, t): x = len(t) - 1 y = len(s) - 1 ind = [] while x >= 0: while s[y] != t[x]: y = y - 1 ind.append(y) x = x - 1 y = y - 1 return list(reversed(ind)) def get_max(ind1, ind2): curr_max = 0 for i in range(1, len(ind1)): if ind2[i] - ind1[i - 1] > curr_max: curr_max = ind2[i] - ind1[i - 1] return curr_max m, n = [int(i) for i in f.readline().strip().split()] s = f.readline().strip() t = f.readline().strip() ind1 = find_good_chain(s, t) ind2 = find_reversed_chain(s, t) curr_max = get_max(ind1, ind2) print(curr_max)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$. A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$. Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence. -----Input----- The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) β€” the lengths of the strings $s$ and $t$. The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings. -----Output----- Output one integer β€” the maximum width of a beautiful sequence. -----Examples----- Input 5 3 abbbc abc Output 3 Input 5 2 aaaaa aa Output 4 Input 5 5 abcdf abcdf Output 1 Input 2 2 ab ab Output 1 -----Note----- In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$. In the second example the beautiful sequence with the maximum width is $\{1, 5\}$. In the third example there is exactly one beautiful sequence β€” it is $\{1, 2, 3, 4, 5\}$. In the fourth example there is exactly one beautiful sequence β€” it is $\{1, 2\}$.
def findmax(s, t): l_indexies = [] start = 0 for c in t: idx = s.find(c, start) l_indexies.append(idx) start = idx + 1 r_indexies = [] end = len(s) for c in reversed(t): idx = s.rfind(c, 0, end) r_indexies.append(idx) end = idx r_indexies = r_indexies[::-1] diff1 = (r - l for l, r in zip(l_indexies[:-1], l_indexies[1:])) diff2 = (r - l for l, r in zip(l_indexies[:-1], r_indexies[1:])) maxwidth = max(*diff1, *diff2) return maxwidth n, m = list(map(int, input().split(" "))) s = input() t = input() print(findmax(s, t))
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$. A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$. Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence. -----Input----- The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) β€” the lengths of the strings $s$ and $t$. The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings. -----Output----- Output one integer β€” the maximum width of a beautiful sequence. -----Examples----- Input 5 3 abbbc abc Output 3 Input 5 2 aaaaa aa Output 4 Input 5 5 abcdf abcdf Output 1 Input 2 2 ab ab Output 1 -----Note----- In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$. In the second example the beautiful sequence with the maximum width is $\{1, 5\}$. In the third example there is exactly one beautiful sequence β€” it is $\{1, 2, 3, 4, 5\}$. In the fourth example there is exactly one beautiful sequence β€” it is $\{1, 2\}$.
n, m = map(int, input().split()) s = input() t = input() s1 = [] s2 = [] j = 0 for i in range(m): while t[i] != s[j]: j += 1 s1.append(j) j += 1 j = 0 s = s[::-1] t = t[::-1] for i in range(m): while t[i] != s[j]: j += 1 s2.append(n - j - 1) j += 1 ans = 0 s2 = s2[::-1] for i in range(m - 1): ans = max(ans, s2[i + 1] - s1[i]) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$. A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$. Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence. -----Input----- The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) β€” the lengths of the strings $s$ and $t$. The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings. -----Output----- Output one integer β€” the maximum width of a beautiful sequence. -----Examples----- Input 5 3 abbbc abc Output 3 Input 5 2 aaaaa aa Output 4 Input 5 5 abcdf abcdf Output 1 Input 2 2 ab ab Output 1 -----Note----- In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$. In the second example the beautiful sequence with the maximum width is $\{1, 5\}$. In the third example there is exactly one beautiful sequence β€” it is $\{1, 2, 3, 4, 5\}$. In the fourth example there is exactly one beautiful sequence β€” it is $\{1, 2\}$.
n, m = map(int, input().split()) s = input() t = input() i = 0 ls = [] for c in t: while s[i] != c: i = i + 1 ls.append(i) i = i + 1 rs = [] i = n - 1 for c in t[::-1]: while s[i] != c: i = i - 1 rs.append(i) i = i - 1 maxi = 0 rs = rs[::-1] for i in range(0, m): maxi = max(maxi, rs[i] - ls[i - 1]) print(maxi)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$. A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$. Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence. -----Input----- The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) β€” the lengths of the strings $s$ and $t$. The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings. -----Output----- Output one integer β€” the maximum width of a beautiful sequence. -----Examples----- Input 5 3 abbbc abc Output 3 Input 5 2 aaaaa aa Output 4 Input 5 5 abcdf abcdf Output 1 Input 2 2 ab ab Output 1 -----Note----- In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$. In the second example the beautiful sequence with the maximum width is $\{1, 5\}$. In the third example there is exactly one beautiful sequence β€” it is $\{1, 2, 3, 4, 5\}$. In the fourth example there is exactly one beautiful sequence β€” it is $\{1, 2\}$.
n, m = [int(i) for i in input().split()] s, t = [input() for i in range(2)] qw = {} ind = n for i in range(m - 1, -1, -1): for j in range(ind - 1, -1, -1): if s[j] == t[i]: qw[i] = j ind = j break raz = 0 ind = -1 for i in range(1, m): for j in range(ind + 1, n): if s[j] == t[i - 1]: ind = j break k = qw[i] - ind raz = max(raz, k) print(raz)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$. A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$. Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence. -----Input----- The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) β€” the lengths of the strings $s$ and $t$. The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings. -----Output----- Output one integer β€” the maximum width of a beautiful sequence. -----Examples----- Input 5 3 abbbc abc Output 3 Input 5 2 aaaaa aa Output 4 Input 5 5 abcdf abcdf Output 1 Input 2 2 ab ab Output 1 -----Note----- In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$. In the second example the beautiful sequence with the maximum width is $\{1, 5\}$. In the third example there is exactly one beautiful sequence β€” it is $\{1, 2, 3, 4, 5\}$. In the fourth example there is exactly one beautiful sequence β€” it is $\{1, 2\}$.
n, m = map(int, input().split()) val1 = input() val2 = input() left = [] right = [] num = m for i in range(n - 1, -1, -1): vari = val1[i] if val2[num - 1] == vari: right.append(i) num = num - 1 if num == 0: break num = 0 for i in range(n): vari = val1[i] if val2[num] == vari: left.append(i) num += 1 if num == m: break result = 0 right.reverse() for j in range(m - 1): result = max(right[j + 1] - left[j], result) print(result)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$. A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$. Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence. -----Input----- The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) β€” the lengths of the strings $s$ and $t$. The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings. -----Output----- Output one integer β€” the maximum width of a beautiful sequence. -----Examples----- Input 5 3 abbbc abc Output 3 Input 5 2 aaaaa aa Output 4 Input 5 5 abcdf abcdf Output 1 Input 2 2 ab ab Output 1 -----Note----- In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$. In the second example the beautiful sequence with the maximum width is $\{1, 5\}$. In the third example there is exactly one beautiful sequence β€” it is $\{1, 2, 3, 4, 5\}$. In the fourth example there is exactly one beautiful sequence β€” it is $\{1, 2\}$.
def solve(s, t): min_ids = [] i = 0 for ct in t: while True: if s[i] == ct: min_ids.append(i) i += 1 break i += 1 max_ids = [] i = len(s) - 1 for ct in reversed(t): while True: if s[i] == ct: max_ids.append(i) i -= 1 break i -= 1 max_ids.reverse() mx = 0 for i, min_id in enumerate(min_ids[:-1]): mx = max(max_ids[i + 1] - min_id, mx) print(mx) input() s = input().strip() t = input().strip() solve(s, t)
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR WHILE NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$. A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$. Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence. -----Input----- The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) β€” the lengths of the strings $s$ and $t$. The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings. -----Output----- Output one integer β€” the maximum width of a beautiful sequence. -----Examples----- Input 5 3 abbbc abc Output 3 Input 5 2 aaaaa aa Output 4 Input 5 5 abcdf abcdf Output 1 Input 2 2 ab ab Output 1 -----Note----- In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$. In the second example the beautiful sequence with the maximum width is $\{1, 5\}$. In the third example there is exactly one beautiful sequence β€” it is $\{1, 2, 3, 4, 5\}$. In the fourth example there is exactly one beautiful sequence β€” it is $\{1, 2\}$.
n = input() s = list(input()) t = list(input()) pref = [0] * (len(s) + 1) for i in range(1, len(s) + 1): if pref[i - 1] == len(t): pref[i] = len(t) elif t[pref[i - 1]] == s[i - 1]: pref[i] = pref[i - 1] + 1 else: pref[i] = pref[i - 1] ss = s[::-1] tt = t[::-1] pref2 = [0] * (len(ss) + 1) for i in range(1, len(ss) + 1): if pref2[i - 1] == len(tt): pref2[i] = len(tt) elif tt[pref2[i - 1]] == ss[i - 1]: pref2[i] = pref2[i - 1] + 1 else: pref2[i] = pref2[i - 1] i = 0 while pref[i] == 0: i += 1 j = len(s) ans = 0 while pref[i] < len(t): while pref2[j] >= len(t) - pref[i]: j -= 1 j += 1 ans = max(ans, len(s) - j - i + 1) x = pref[i] i += 1 while i < len(s) and pref[i] == x: i += 1 print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$. A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$. Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence. -----Input----- The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) β€” the lengths of the strings $s$ and $t$. The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings. -----Output----- Output one integer β€” the maximum width of a beautiful sequence. -----Examples----- Input 5 3 abbbc abc Output 3 Input 5 2 aaaaa aa Output 4 Input 5 5 abcdf abcdf Output 1 Input 2 2 ab ab Output 1 -----Note----- In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$. In the second example the beautiful sequence with the maximum width is $\{1, 5\}$. In the third example there is exactly one beautiful sequence β€” it is $\{1, 2, 3, 4, 5\}$. In the fourth example there is exactly one beautiful sequence β€” it is $\{1, 2\}$.
n, m = list(map(int, input().split())) a = list(input()) b = list(input()) poses = [] it = 0 for i in range(m): while a[it] != b[i]: it += 1 poses.append(it) it += 1 last = n - 1 ma = 0 for i in range(1, ma): m = max(poses[i] - poses[i - 1], ma) for i in range(m - 1, 0, -1): while a[last] != b[i]: last -= 1 ma = max(last - poses[i - 1], ma) last -= 1 print(ma)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$. A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$. Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence. -----Input----- The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) β€” the lengths of the strings $s$ and $t$. The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings. -----Output----- Output one integer β€” the maximum width of a beautiful sequence. -----Examples----- Input 5 3 abbbc abc Output 3 Input 5 2 aaaaa aa Output 4 Input 5 5 abcdf abcdf Output 1 Input 2 2 ab ab Output 1 -----Note----- In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$. In the second example the beautiful sequence with the maximum width is $\{1, 5\}$. In the third example there is exactly one beautiful sequence β€” it is $\{1, 2, 3, 4, 5\}$. In the fourth example there is exactly one beautiful sequence β€” it is $\{1, 2\}$.
N, M = map(int, input().split()) S = input() T = input() A = [] B = [] si = 0 for t in T: while S[si] != t: si += 1 A.append(si) si += 1 si = N - 1 for t in T[::-1]: while S[si] != t: si -= 1 B.append(si) si -= 1 B.reverse() ans = 0 for a, b in zip(A, B[1:]): ans = max(ans, b - a) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$. A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$. Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence. -----Input----- The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) β€” the lengths of the strings $s$ and $t$. The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings. -----Output----- Output one integer β€” the maximum width of a beautiful sequence. -----Examples----- Input 5 3 abbbc abc Output 3 Input 5 2 aaaaa aa Output 4 Input 5 5 abcdf abcdf Output 1 Input 2 2 ab ab Output 1 -----Note----- In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$. In the second example the beautiful sequence with the maximum width is $\{1, 5\}$. In the third example there is exactly one beautiful sequence β€” it is $\{1, 2, 3, 4, 5\}$. In the fourth example there is exactly one beautiful sequence β€” it is $\{1, 2\}$.
import sys input = sys.stdin.readline def solve(): n, m = map(int, input().split()) p = [[] for i in range(26)] p1 = [None] * 26 A = ord("a") j = 0 for i in input().strip(): p[ord(i) - A].append(j) j += 1 for i in range(26): p1[i] = p[i].copy() p1[i].reverse() t = input().strip() a = [] prev = -1 for i in range(0, len(t)): c = ord(t[i]) - A while True: v = p1[c].pop() if v > prev: break a.append(v) prev = v b = [] prev = int(1000000000.0) for i in range(len(t) - 1, -1, -1): c = ord(t[i]) - A while True: v = p[c].pop() if v < prev: break b.append(v) prev = v b.reverse() res = 0 for i in range(1, len(t)): res = max(res, b[i] - a[i - 1]) print(res) solve()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NONE NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$. A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$. Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence. -----Input----- The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) β€” the lengths of the strings $s$ and $t$. The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings. -----Output----- Output one integer β€” the maximum width of a beautiful sequence. -----Examples----- Input 5 3 abbbc abc Output 3 Input 5 2 aaaaa aa Output 4 Input 5 5 abcdf abcdf Output 1 Input 2 2 ab ab Output 1 -----Note----- In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$. In the second example the beautiful sequence with the maximum width is $\{1, 5\}$. In the third example there is exactly one beautiful sequence β€” it is $\{1, 2, 3, 4, 5\}$. In the fourth example there is exactly one beautiful sequence β€” it is $\{1, 2\}$.
import sys input = sys.stdin.readline def main(): N, M = [int(x) for x in input().split()] S = input().strip() T = input().strip() aa = [] current = 0 for i in range(M): for j in range(current, N): if T[i] == S[j]: aa.append(j) current = j + 1 break bb = [] current = N - 1 for i in range(M - 1, -1, -1): for j in range(current, -1, -1): if T[i] == S[j]: bb.append(j) current = j - 1 break bb = bb[::-1] ans = 0 for i in range(M - 1): ans = max(bb[i + 1] - aa[i], ans) print(ans) main()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$. A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$. Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence. -----Input----- The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) β€” the lengths of the strings $s$ and $t$. The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings. -----Output----- Output one integer β€” the maximum width of a beautiful sequence. -----Examples----- Input 5 3 abbbc abc Output 3 Input 5 2 aaaaa aa Output 4 Input 5 5 abcdf abcdf Output 1 Input 2 2 ab ab Output 1 -----Note----- In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$. In the second example the beautiful sequence with the maximum width is $\{1, 5\}$. In the third example there is exactly one beautiful sequence β€” it is $\{1, 2, 3, 4, 5\}$. In the fourth example there is exactly one beautiful sequence β€” it is $\{1, 2\}$.
n, m = [int(x) for x in input().split()] s = input() t = input() L = 1 R = n - m + 1 head = 0 indices1 = [] indices2 = [] for i in range(n): if s[i] == t[head]: head += 1 indices1.append(i) if head == m: break tail = m - 1 for i in range(n): if s[n - 1 - i] == t[tail]: tail -= 1 indices2.append(n - 1 - i) if tail == -1: break indices2.reverse() ans = max([(indices2[i + 1] - indices1[i]) for i in range(m - 1)]) print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Your classmate, whom you do not like because he is boring, but whom you respect for his intellect, has two strings: $s$ of length $n$ and $t$ of length $m$. A sequence $p_1, p_2, \ldots, p_m$, where $1 \leq p_1 < p_2 < \ldots < p_m \leq n$, is called beautiful, if $s_{p_i} = t_i$ for all $i$ from $1$ to $m$. The width of a sequence is defined as $\max\limits_{1 \le i < m} \left(p_{i + 1} - p_i\right)$. Please help your classmate to identify the beautiful sequence with the maximum width. Your classmate promised you that for the given strings $s$ and $t$ there is at least one beautiful sequence. -----Input----- The first input line contains two integers $n$ and $m$ ($2 \leq m \leq n \leq 2 \cdot 10^5$) β€” the lengths of the strings $s$ and $t$. The following line contains a single string $s$ of length $n$, consisting of lowercase letters of the Latin alphabet. The last line contains a single string $t$ of length $m$, consisting of lowercase letters of the Latin alphabet. It is guaranteed that there is at least one beautiful sequence for the given strings. -----Output----- Output one integer β€” the maximum width of a beautiful sequence. -----Examples----- Input 5 3 abbbc abc Output 3 Input 5 2 aaaaa aa Output 4 Input 5 5 abcdf abcdf Output 1 Input 2 2 ab ab Output 1 -----Note----- In the first example there are two beautiful sequences of width $3$: they are $\{1, 2, 5\}$ and $\{1, 4, 5\}$. In the second example the beautiful sequence with the maximum width is $\{1, 5\}$. In the third example there is exactly one beautiful sequence β€” it is $\{1, 2, 3, 4, 5\}$. In the fourth example there is exactly one beautiful sequence β€” it is $\{1, 2\}$.
def fill(s, t, f): j = 0 i = 0 l = [0] * len(t) while i < m: while t[i] != s[j]: j += 1 if not f: l[i] = n - 1 - j else: l[i] = j i += 1 j += 1 return l n, m = list(map(int, input().split())) s = input() t = input() forward = fill(s, t, True) backward = fill(s[::-1], t[::-1], False)[::-1] m = 0 for i in range(1, len(t)): m = max(m, backward[i] - forward[i - 1]) print(m)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR WHILE VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER IF VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
t = int(input()) for _ in range(t): n = int(input()) k = list(map(int, input().split())) h = list(map(int, input().split())) inter = [] for i in range(n): inter.append([k[i] - h[i], k[i]]) inter.sort() ans = 0 s = -1 e = -1 for i in inter: if i[0] >= e: ans += (e - s) * (e - s + 1) // 2 s = i[0] e = i[1] else: e = max(e, i[1]) ans += (e - s) * (e - s + 1) // 2 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
t = int(input()) while t: n = int(input()) lt = list(map(int, input().split())) lh = list(map(int, input().split())) li = [] for i in range(n): li.append([lt[i] - lh[i] + 1, lt[i]]) li.sort() i = 0 ele = li[0] while ele[-1] != li[-1][-1]: if li[i + 1][0] <= li[i][1]: li[i] = [min(li[i][0], li[i + 1][0]), max(li[i + 1][1], li[i][1])] li.pop(i + 1) else: i += 1 ele = li[i] s = 0 for ele in li: s += (ele[-1] - ele[-2] + 1) * (ele[-1] - ele[-2] + 2) // 2 print(int(s)) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR LIST FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
def sum_till(n): return n * (n + 1) // 2 for t in range(int(input())): n = int(input()) k = list(map(int, input().split())) h = list(map(int, input().split())) if True: start = [] end = [] for i in range(1, n + 1): st = k[-i] - h[-i] + 1 ed = k[-i] start.append(st) end.append(ed) mana = 0 temp_st = start[0] temp_ed = end[0] diff = [] for i in range(n - 1): temp_st = min(temp_st, start[i]) temp_ed = max(temp_ed, end[i]) if temp_st > end[i + 1]: diff.append(temp_ed - temp_st + 1) temp_st = start[i + 1] temp_ed = end[i + 1] temp_st = min(temp_st, start[n - 1]) temp_ed = max(temp_ed, end[n - 1]) diff.append(temp_ed - temp_st + 1) s = 0 for i in diff: s += sum_till(i) print(s)
FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
for _ in range(int(input())): n = int(input()) k = list(map(int, input().split())) h = list(map(int, input().split())) st = [] for i in range(n): st.append([k[i] - h[i] + 1, k[i]]) st.sort() l, r = -2, -1 ans = 0 for it in st: if it[0] > r: if l != -2 and r != -1: ans += (r - l + 1) * (r - l + 2) // 2 l, r = it else: r = max(r, it[1]) if l != -2 and r != -1: ans += (r - l + 1) * (r - l + 2) // 2 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
for i in range(int(input())): n = int(input()) k = list(map(int, input().split())) h = list(map(int, input().split())) l = [h[n - 1]] c = 0 r = k[n - 1] - h[n - 1] + 1 ks = k[n - 1] hs = h[n - 1] for j in range(n - 2, -1, -1): if r <= k[j] - h[j] + 1: continue elif r <= k[j]: r = k[j] - h[j] + 1 hs = ks - r + 1 l[c] = hs else: c += 1 l.append(h[j]) r = k[j] - h[j] + 1 ks = k[j] hs = h[j] s = 0 for j in range(c + 1): s += l[j] * (l[j] + 1) // 2 print(s)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
import sys def eprint(*args, **kwargs): print(*args, file=sys.stderr, **kwargs) testcases = int(input()) for _ in range(testcases): _ = int(input()) spawns = input().split() spawns = [int(x) for x in spawns] health = input().split() health = [int(x) for x in health] mana = 0 monsters = list(zip(spawns, health)) monsters.sort(key=lambda x: (x[0] - x[1], -x[1])) time = 0 charging_since = 0 for monster in monsters: start = monster[0] - monster[1] eprint("time is", time, "monster:", monster, "mana:", mana) if time > monster[0]: eprint("already killed that one") pass elif time > start: eprint("continuing to charge") mana += (monster[0] - charging_since) * ( monster[0] - charging_since + 1 ) // 2 - (time - charging_since) * (time - charging_since + 1) // 2 time = monster[0] else: eprint("starting to charge") mana += monster[1] * (monster[1] + 1) // 2 time = monster[0] charging_since = start eprint(monsters) print(mana)
IMPORT FUNC_DEF EXPR FUNC_CALL VAR VAR 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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING VAR STRING VAR STRING VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR STRING VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR STRING VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
testcases = int(input()) for i in range(testcases): n_monster = int(input()) input_1 = input().split() appear_list = list(map(int, input_1)) input_2 = input().split() health_list = list(map(int, input_2)) mana = int(0) for i in range(1, n_monster): for j in range(i): if appear_list[i] - health_list[i] < appear_list[j]: health_list[i] = max( [health_list[j] + (appear_list[i] - appear_list[j]), health_list[i]] ) health_list[j] = 0 for i in range(n_monster): mana = mana + health_list[i] * (health_list[i] + 1) // 2 print(mana)
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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
t = int(input()) for _ in range(t): n = int(input()) ks = [int(x) for x in input().split()] hs = [int(x) for x in input().split()] dct = {} for i in range(n): dct[ks[i] - hs[i]] = max(dct.get(ks[i] - hs[i], -1), ks[i]) ds = sorted(dct.keys()) i = 0 ans = 0 while 1: right = dct[ds[i]] left = ds[i] while i < len(ds) and ds[i] < right: right = max(right, dct[ds[i]]) i += 1 ans += (right - left) * (right - left + 1) // 2 if i == len(ds): break print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
for _ in range(int(input())): m = int(input()) monsters = list(map(int, input().split())) health = list(map(int, input().split())) intervals = [] for idx, monster_location in enumerate(monsters): intervals.append([monster_location - health[idx] + 1, monster_location]) intervals.sort(key=lambda x: (x[0], x[1])) overlap = [] start, end = intervals[0] for next_start, next_end in intervals[1:]: if next_start <= end: end = max(end, next_end) else: overlap.append([start, end]) start = next_start end = next_end if not overlap or overlap[-1] != [start, end]: overlap.append([start, end]) ans = 0 for start, end in overlap: ans += (end - start + 1) * (end - start + 2) // 2 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER LIST VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
def solve(n): return n * (n + 1) // 2 t = int(input()) for _ in range(t): n = int(input()) app = list(map(int, input().split())) hea = list(map(int, input().split())) ran = [] for i in range(n): r = app[i] l = app[i] - hea[i] + 1 ran.append([l, r]) ran.sort(key=lambda x: x[0]) l = 1 r = 0 ans = 0 for i in range(n): if ran[i][0] > r: ans += solve(r - l + 1) l = ran[i][0] r = ran[i][1] else: r = max(r, ran[i][1]) ans += solve(r - l + 1) print(ans)
FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
import sys input = sys.stdin.readline ans = [] for _ in range(int(input())): n = int(input()) k = list(map(int, input().split())) h = list(map(int, input().split())) a = 0 p = h[-1] q = k[-1] for i in range(n - 2, -1, -1): d = p - (q - k[i]) if d > 0: if d < h[i]: p += h[i] - d else: a += (1 + p) * p // 2 p = h[i] q = k[i] a += (1 + p) * p // 2 ans.append(a) for i in ans: print(i)
IMPORT ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
def processing(monster, power, n): ans = 0 span = power[-1] tail = monster[-1] for i in reversed(range(n - 1)): if monster[i] <= tail - span: ans += span * (span + 1) // 2 span = power[i] tail = monster[i] elif tail - span + 1 <= monster[i] - power[i]: continue else: span = power[i] + tail - monster[i] ans += span * (span + 1) // 2 return ans t = int(input()) while t > 0: n = int(input()) monster = [int(i) for i in input().split()] power = [int(i) for i in input().split()] print(processing(monster, power, n)) t -= 1
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
t = int(input()) out = "" for _ in range(t): n = int(input()) k = [int(x) for x in input().split()] h = [int(x) for x in input().split()] last = k[-1] value = h[-1] for i in range(n - 2, -1, -1): h[i] = max(h[i], value - (last - k[i])) last = k[i] value = h[i] last = 0 value = 0 total = 0 for i in range(n): if last >= k[i] - (h[i] - 1): total += (k[i] - last) * (value + 1 + value + k[i] - last) // 2 value += k[i] - last else: total += (h[i] + 1) * h[i] // 2 value = h[i] last = k[i] out += str(int(total)) + "\n" print(out)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
import sys input = sys.stdin.readline def helper(a): return a * (a + 1) // 2 def solve(n, arr): ans = helper(arr[0][1]) curr = arr[0][1] for i in range(1, n): d = arr[i][0] - arr[i - 1][0] if d >= arr[i][1]: ans += helper(arr[i][1]) curr = arr[i][1] else: ans += helper(curr + d) - helper(curr) curr += d return ans t = int(input()) for _ in range(t): n = int(input()) k = list(map(int, input().split())) h = list(map(int, input().split())) arr = list(zip(k, h)) arr.sort(key=lambda x: x[0] - x[1]) a = [] a.append(arr[0]) for i in range(1, n): if arr[i][0] > a[-1][0]: a.append(arr[i]) print(solve(len(a), a))
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
for iq in range(int(input())): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) k = -1 ans = 0 for i in range(n - 2, -1, -1): vz = b[k] + a[i] - a[k] if vz <= 0: ans += b[k] * (b[k] + 1) // 2 k = i elif vz < b[i]: b[k] += b[i] - vz ans += b[k] * (b[k] + 1) // 2 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
for _ in range(int(input())): n = int(input()) time = [int(x) for x in input().split()] damage = [int(x) for x in input().split()] biggesttime = n - 1 biggestdamage = n - 1 for j in range(n - 2, -1, -1): diffoftime = time[biggesttime] - time[j] if diffoftime >= damage[biggestdamage]: biggestdamage = j biggesttime = j elif diffoftime <= damage[biggestdamage] - damage[j]: damage[j] = 0 else: damage[biggestdamage] = damage[j] + diffoftime damage[j] = 0 s = 0 for x in damage: if x != 0: s += x * (x + 1) // 2 print(s)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
t = int(input()) while t: t -= 1 n = int(input()) k = list(map(int, input().split())) h = list(map(int, input().split())) l = -1 r = -1 res = 0 for i in range(n - 1, -1, -1): if l == -1: r = k[i] l = k[i] - h[i] + 1 elif k[i] < l: res += (r - l + 1) * (r - l + 2) // 2 r = k[i] l = k[i] - h[i] + 1 elif k[i] - h[i] + 1 < l: l = k[i] - h[i] + 1 res += (r - l + 1) * (r - l + 2) // 2 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
for _ in range(int(input())): n = int(input()) k = list(map(int, input().split())) h = list(map(int, input().split())) a = [] for i in range(n - 1, -1, -1): a.append((k[i] - h[i] + 1, k[i])) a.sort() stack = [] stack.append(a[0]) for i in range(1, n): if stack[-1][1] >= a[i][0]: temp = stack.pop() stack.append((temp[0], max(a[i][1], temp[1]))) else: stack.append(a[i]) ans = 0 for i in range(len(stack)): t = stack[i][1] - stack[i][0] + 1 ans += t * (t + 1) // 2 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
def solve(s, h): ramps = [] i = len(h) - 1 end_ramp = s[i] start_ramp = s[i] - h[i] i -= 1 while 1: if i < 0: break if s[i] > start_ramp: if h[i] <= s[i] - start_ramp: pass else: start_ramp = s[i] - h[i] else: ramps.append([start_ramp, end_ramp]) end_ramp = s[i] start_ramp = s[i] - h[i] i -= 1 ramps.append([start_ramp, end_ramp]) sum = 0 for i in ramps: dur = i[1] - i[0] sum += dur * (dur + 1) // 2 return sum n = int(input()) for i in range(n): input() (*s,) = map(int, input().split()) (*h,) = map(int, input().split()) print(solve(s, h))
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR NUMBER WHILE NUMBER IF VAR NUMBER IF VAR VAR VAR IF VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
for _ in range(int(input())): n = int(input()) time = list(map(int, input().split())) health = list(map(int, input().split())) ans = [] for i in range(n): l = time[i] r = time[i] - health[i] + 1 ans.append([r, l]) def fun(x): return x * (x + 1) // 2 arr = sorted(ans) ans = [] val = 0 for ele in arr: if not ans or ans[-1][-1] < ele[0]: if ans: l = ans[-1][0] r = ans[-1][1] val += fun(r - l + 1) ans.append(ele) else: ans[-1][-1] = max(ans[-1][-1], ele[1]) l, r = ans[-1][0], ans[-1][1] val += fun(r - l + 1) print(val)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
def cal(a): return a * (a + 1) // 2 t = int(input()) for _ in range(t): n = int(input()) k = [int(i) for i in input().split(" ")] h = [int(i) for i in input().split(" ")] stack = [] ans = 0 for i in range(n): origin = k[i] - h[i] + 1 while stack: if stack[-1][0] <= origin and stack[-1][1] >= origin: origin = stack[-1][0] stack.pop() break elif stack[-1][1] < origin: break stack.pop() stack.append([origin, k[i]]) for i in stack: ans += cal(i[1] - i[0] + 1) print(ans)
FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER WHILE VAR IF VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR FOR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
import sys inp = sys.stdin.readline def getCost(a: int): return a * (a + 1) // 2 for _ in range(int(inp())): n = int(inp()) k = list(map(int, inp().split())) h = list(map(int, inp().split())) ans = 0 segEnd = len(k) - 1 segMax = len(k) - 1 for i in range(len(k) - 2, -1, -1): if h[segMax] <= k[segMax] - k[i]: ans += getCost(h[segMax] + (k[segEnd] - k[segMax])) segEnd = i segMax = i elif h[i] + k[segMax] - k[i] > h[segMax]: segMax = i ans += getCost(h[segMax] + (k[segEnd] - k[segMax])) print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF VAR RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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 FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
T = int(input()) while T > 0: T -= 1 n = int(input()) k = input().split() h = input().split() for i in range(n): k[i] = int(k[i]) for i in range(n): h[i] = int(h[i]) for i in range(n - 1, 0, -1): for j in range(i - 1, -1, -1): if h[i] - (k[i] - k[j]) > 0 and h[j] > h[i] - (k[i] - k[j]): h[i] = h[j] + (k[i] - k[j]) sum = 0 last = k[-1] + 10 for i in range(n - 1, -1, -1): if k[i] <= last: a = int(h[i]) b = int(h[i] + 1) if a % 2 == 0: a = int(a / 2) else: b = int(b / 2) sum += int(a * b) last = k[i] - h[i] print(sum)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
for j in range(int(input())): n = int(input()) ks = list(map(int, input().split())) hs = list(map(int, input().split())) ks.insert(0, 0) hs.insert(0, 0) ind = n curr = hs[ind] su = 0 for i in range(n - 1, -1, -1): if ks[ind] - ks[i] < curr: curr += max(0, hs[i] - (curr - (ks[ind] - ks[i]))) else: su += curr * (curr + 1) // 2 curr = hs[i] ind = i print(su)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
for t in range(int(input())): n = int(input()) k = [int(i) for i in input().split()] h = [int(i) for i in input().split()] atleast = h[:] for i in range(n - 2, -1, -1): if k[i + 1] - k[i] < atleast[i + 1]: if atleast[i] < atleast[i + 1] - (k[i + 1] - k[i]): atleast[i] = atleast[i + 1] - (k[i + 1] - k[i]) ans = atleast[0] * (atleast[0] + 1) // 2 for i in range(1, n): ans += atleast[i] * (atleast[i] + 1) // 2 if atleast[i] > k[i] - k[i - 1]: ans -= atleast[i] * (atleast[i] + 1) // 2 ans -= atleast[i - 1] * (atleast[i - 1] + 1) // 2 atleast[i] = atleast[i - 1] + k[i] - k[i - 1] ans += atleast[i] * (atleast[i] + 1) // 2 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
def tr(n): return n * (n + 1) // 2 tests = int(input()) for test in range(tests): n = int(input()) k = list(map(int, input().split())) h = list(map(int, input().split())) res = 0 curh = 0 curk = 2 * 10**9 for i in range(n - 1, -1, -1): if curk - k[i] >= curh: res += tr(curh) curk = k[i] curh = h[i] else: curh = max(curh, h[i] + curk - k[i]) res += tr(curh) print(res)
FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
for _ in range(int(input())): n = int(input()) s = input() ks = s.split() s = input() hs = s.split() arr = [] for i in range(n): arr.append([int(ks[i]) - int(hs[i]) + 1, int(ks[i])]) arr.sort() a = [arr[0][0], arr[0][1]] ans = 0 for i in range(1, n): if arr[i][0] <= a[1]: a[1] = max(a[1], arr[i][1]) else: x = a[1] - a[0] + 1 ans += x * (x + 1) // 2 a = [arr[i][0], arr[i][1]] x = a[1] - a[0] + 1 ans += x * (x + 1) // 2 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
t = int(input()) for _ in range(t): n = int(input()) tm = list(map(int, input().split())) hm = list(map(int, input().split())) lstS = [tm[0] - hm[0] + 1] lstE = [tm[0]] for i in range(1, n): e = tm[i] s = tm[i] - hm[i] + 1 if len(lstS) == 0: lstS.append(s) lstE.append(e) if len(lstS) != 0 and s <= lstE[-1]: while len(lstS) > 0 and s <= lstE[-1]: os = lstS.pop() lstE.pop() s = min(os, s) lstS.append(s) lstE.append(e) else: lstS.append(s) lstE.append(e) tot = 0 for i in range(len(lstS)): rng = lstE[i] - lstS[i] + 1 tot += rng * (rng + 1) // 2 print(tot)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
def sum_till(n): return n * (n + 1) // 2 for _ in range(int(input())): n = int(input()) arrivals = list(map(int, input().split())) healths = list(map(int, input().split())) intervals = [] for i, arrival in enumerate(arrivals): this_r = arrival this_l = arrival - healths[i] + 1 intervals.append((this_l, this_r)) intervals.sort() merged_intervals = [] curr_l, curr_r = 1, 0 ans = 0 for i, (l, r) in enumerate(intervals): if l <= curr_r: curr_r = max(curr_r, r) else: merged_intervals.append((curr_l, curr_r)) curr_l = l curr_r = r merged_intervals.append((curr_l, curr_r)) for l, r in merged_intervals: ans += sum_till(r - l + 1) print(ans)
FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
def summ(n): return n * (n + 1) // 2 for _ in range(int(input())): n = int(input()) k = list(map(int, input().split())) h = list(map(int, input().split())) s = [] s.append([k[0], h[0]]) for i in range(1, n): while len(s) > 0: prevtime = s[-1][0] prevh = s[-1][1] diff = k[i] - prevtime if diff >= h[i]: s.append([k[i], h[i]]) break else: h[i] = max(h[i], diff + prevh) s.pop() if len(s) == 0: s.append([k[i], h[i]]) ans = 0 for i in range(len(s)): ans += summ(s[i][-1]) print(ans)
FUNC_DEF RETURN BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
for t in range(int(input())): n = int(input()) ls = list(map(int, input().strip().split())) lh = list(map(int, input().strip().split())) lis = [] lis.append((ls[0] - lh[0] + 1, ls[0])) for i in range(1, n): a = ls[i] - lh[i] + 1 if a <= lis[-1][1] and lis[-1][0] <= a: lis[-1] = lis[-1][0], ls[i] elif lis[-1][0] > a: del lis[-1] while True: if len(lis) == 0: lis.append((a, ls[i])) break elif a <= lis[-1][1] and lis[-1][0] <= a: lis[-1] = lis[-1][0], ls[i] break elif lis[-1][0] > a: del lis[-1] else: lis.append((a, ls[i])) break else: lis.append((a, ls[i])) co = 0 for i in range(len(lis)): k = lis[i][1] - lis[i][0] + 1 co += k * (k + 1) // 2 print(co)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER VAR VAR IF VAR NUMBER NUMBER VAR VAR NUMBER WHILE NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER VAR VAR IF VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
t = int(input()) ans = [] while t > 0: t -= 1 n = int(input()) otv = 0 time = list(map(int, input().split())) x = list(map(int, input().split())) st = [] for i in range(n): st.append([time[i] - x[i], time[i]]) st.sort() l, r = -1, -1 for it in st: if it[0] >= r: otv += (r - l) * (r - l + 1) // 2 l, r = it else: r = max(r, it[1]) otv += (r - l) * (r - l + 1) // 2 ans.append(otv) print("\n".join([str(i) for i in ans]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN 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 FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
answer = [] for _ in range(int(input())): n = int(input()) k = list(map(int, input().split())) h = list(map(int, input().split())) count = 0 k_r = k[-1] k_l = k[-1] - h[-1] + 1 for i in range(n - 2, -1, -1): if k_l <= k[i]: pass else: count += (k_r - k_l + 2) * (k_r - k_l + 1) // 2 k_r = k[i] k_l = min(k_l, k[i] - h[i] + 1) count += (k_r - k_l + 2) * (k_r - k_l + 1) // 2 answer.append(count) print("\n".join(map(str, answer)))
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
t = int(input()) for i in range(t): n = int(input()) seconds = [int(el) for el in input().split(" ")] points = [int(el) for el in input().split(" ")] current = points[-1] for z in range(n - 2, -1, -1): h = points[z] diff = seconds[z + 1] - seconds[z] points[z] = max([h, current - diff]) current = points[z] total = points[0] * (points[0] + 1) // 2 current = points[0] for j in range(1, n): diff = seconds[j] - seconds[j - 1] h = points[j] if h <= diff: current = h total += h * (h + 1) // 2 else: total += current * diff + diff * (diff + 1) // 2 current += diff print(total)
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 VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
for _ in range(int(input())): n = int(input()) k = list(map(int, input().split())) h = list(map(int, input().split())) s = [] for i in range(n): s.append((k[i] - h[i] + 1, k[i])) ans = [] i = n - 1 while i != 0: a = s[i] b = s[i - 1] if a[0] <= b[1]: new = min(a[0], b[0]), a[1] s.pop(-1) s.pop(-1) i -= 1 s.append(new) else: ans.append(s[i]) s.pop(-1) i -= 1 ans.append(s[0]) res = 0 for i in range(len(ans)): cnt = ans[i][1] - ans[i][0] res += (cnt + 2) * (cnt + 1) // 2 print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
def problemC(n, seconds, health): intervals = [] for i in range(n): intervals.append((seconds[i] - health[i] + 1, seconds[i])) intervals.sort() l, r = -1, -2 ans = 0 for left, right in intervals: if left > r: ans += (r - l + 1) * (r - l + 2) // 2 l, r = left, right else: r = max(r, right) ans += (r - l + 1) * (r - l + 2) // 2 return ans for _ in range(int(input())): n = int(input()) seconds = list(map(int, input().split(" "))) health = list(map(int, input().split(" "))) print(problemC(n, seconds, health))
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
import sys def solve(): inp = sys.stdin.readline inp() k = list(map(int, inp().split())) h = list(map(int, inp().split())) i = 0 while i < len(h): j = i + 1 while j < len(h): s = k[j] - h[j] + 1 if s <= k[i]: if h[j] < h[i] + (k[j] - k[i]): h[j] = h[i] + (k[j] - k[i]) h.pop(i) k.pop(i) i -= 1 break else: j += 1 i += 1 ans = 0 for i in h: ans += i * (i + 1) // 2 print(ans) def main(): for i in range(int(sys.stdin.readline())): solve() main()
IMPORT FUNC_DEF ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR IF VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Monocarp is playing a computer game once again. He is a wizard apprentice, who only knows a single spell. Luckily, this spell can damage the monsters. The level he's currently on contains $n$ monsters. The $i$-th of them appears $k_i$ seconds after the start of the level and has $h_i$ health points. As an additional constraint, $h_i \le k_i$ for all $1 \le i \le n$. All $k_i$ are different. Monocarp can cast the spell at moments which are positive integer amounts of second after the start of the level: $1, 2, 3, \dots$ The damage of the spell is calculated as follows. If he didn't cast the spell at the previous second, the damage is $1$. Otherwise, let the damage at the previous second be $x$. Then he can choose the damage to be either $x + 1$ or $1$. A spell uses mana: casting a spell with damage $x$ uses $x$ mana. Mana doesn't regenerate. To kill the $i$-th monster, Monocarp has to cast a spell with damage at least $h_i$ at the exact moment the monster appears, which is $k_i$. Note that Monocarp can cast the spell even when there is no monster at the current second. The mana amount required to cast the spells is the sum of mana usages for all cast spells. Calculate the least amount of mana required for Monocarp to kill all monsters. It can be shown that it's always possible to kill all monsters under the constraints of the problem. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of the testcase contains a single integer $n$ ($1 \le n \le 100$) β€” the number of monsters in the level. The second line of the testcase contains $n$ integers $k_1 < k_2 < \dots < k_n$ ($1 \le k_i \le 10^9$) β€” the number of second from the start the $i$-th monster appears at. All $k_i$ are different, $k_i$ are provided in the increasing order. The third line of the testcase contains $n$ integers $h_1, h_2, \dots, h_n$ ($1 \le h_i \le k_i \le 10^9$) β€” the health of the $i$-th monster. The sum of $n$ over all testcases doesn't exceed $10^4$. -----Output----- For each testcase, print a single integer β€” the least amount of mana required for Monocarp to kill all monsters. -----Examples----- Input 3 1 6 4 2 4 5 2 2 3 5 7 9 2 1 2 Output 10 6 7 -----Note----- In the first testcase of the example, Monocarp can cast spells $3, 4, 5$ and $6$ seconds from the start with damages $1, 2, 3$ and $4$, respectively. The damage dealt at $6$ seconds is $4$, which is indeed greater than or equal to the health of the monster that appears. In the second testcase of the example, Monocarp can cast spells $3, 4$ and $5$ seconds from the start with damages $1, 2$ and $3$, respectively. In the third testcase of the example, Monocarp can cast spells $4, 5, 7, 8$ and $9$ seconds from the start with damages $1, 2, 1, 1$ and $2$, respectively.
T = int(input()) def calc(n): return (n + 1) * n // 2 def solve(): n = int(input()) k = list(map(int, input().split())) h = list(map(int, input().split())) ranges = [] for i in range(n): ranges += [(k[i] - (h[i] - 1), k[i])] ranges = sorted(ranges) cur_l = ranges[0][0] cur_r = ranges[0][1] new_ranges = [(cur_l, cur_r)] for i in range(1, n): l = ranges[i][0] r = ranges[i][1] if l <= new_ranges[-1][1]: new_ranges[-1] = new_ranges[-1][0], max(r, new_ranges[-1][1]) else: new_ranges += [(l, r)] answer = 0 for i in range(len(new_ranges)): l = new_ranges[i][0] r = new_ranges[i][1] answer += calc(r - l + 1) return answer for t in range(T): print(solve())
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR LIST BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR LIST VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence a_1, a_2, ..., a_{n} of n integer numbers β€” saturation of the color of each pencil. Now Mishka wants to put all the mess in the pack in order. He has an infinite number of empty boxes to do this. He would like to fill some boxes in such a way that: Each pencil belongs to exactly one box; Each non-empty box has at least k pencils in it; If pencils i and j belong to the same box, then |a_{i} - a_{j}| ≀ d, where |x| means absolute value of x. Note that the opposite is optional, there can be pencils i and j such that |a_{i} - a_{j}| ≀ d and they belong to different boxes. Help Mishka to determine if it's possible to distribute all the pencils into boxes. Print "YES" if there exists such a distribution. Otherwise print "NO". -----Input----- The first line contains three integer numbers n, k and d (1 ≀ k ≀ n ≀ 5Β·10^5, 0 ≀ d ≀ 10^9) β€” the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively. The second line contains n integer numbers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” saturation of color of each pencil. -----Output----- Print "YES" if it's possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print "NO". -----Examples----- Input 6 3 10 7 2 7 7 4 2 Output YES Input 6 2 3 4 5 3 13 4 10 Output YES Input 3 2 5 10 16 22 Output NO -----Note----- In the first example it is possible to distribute pencils into 2 boxes with 3 pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won't exceed 10. In the second example you can split pencils of saturations [4, 5, 3, 4] into 2 boxes of size 2 and put the remaining ones into another box.
n, k, d = list(map(int, input().split())) a = sorted(list(map(int, input().split()))) b = [0] * n i = j = 0 for i in range(n): while a[i] - a[j] > d: j += 1 b[i] = j c = [0] * n for i in range(k - 1, n): c[i] = c[i - 1] + int( i - b[i] + 1 >= k and (b[i] == 0 or c[i - k] > c[b[i] - 2] or b[i] == 1 and c[i - k] > c[0]) ) print("YES" if n < 2 or c[n - 1] > c[n - 2] else "NO")
ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING STRING
Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence a_1, a_2, ..., a_{n} of n integer numbers β€” saturation of the color of each pencil. Now Mishka wants to put all the mess in the pack in order. He has an infinite number of empty boxes to do this. He would like to fill some boxes in such a way that: Each pencil belongs to exactly one box; Each non-empty box has at least k pencils in it; If pencils i and j belong to the same box, then |a_{i} - a_{j}| ≀ d, where |x| means absolute value of x. Note that the opposite is optional, there can be pencils i and j such that |a_{i} - a_{j}| ≀ d and they belong to different boxes. Help Mishka to determine if it's possible to distribute all the pencils into boxes. Print "YES" if there exists such a distribution. Otherwise print "NO". -----Input----- The first line contains three integer numbers n, k and d (1 ≀ k ≀ n ≀ 5Β·10^5, 0 ≀ d ≀ 10^9) β€” the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively. The second line contains n integer numbers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” saturation of color of each pencil. -----Output----- Print "YES" if it's possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print "NO". -----Examples----- Input 6 3 10 7 2 7 7 4 2 Output YES Input 6 2 3 4 5 3 13 4 10 Output YES Input 3 2 5 10 16 22 Output NO -----Note----- In the first example it is possible to distribute pencils into 2 boxes with 3 pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won't exceed 10. In the second example you can split pencils of saturations [4, 5, 3, 4] into 2 boxes of size 2 and put the remaining ones into another box.
def getIntList(): return list(map(int, input().split())) def getTransIntList(n): first = getIntList() m = len(first) result = [([0] * n) for _ in range(m)] for i in range(m): result[i][0] = first[i] for j in range(1, n): curr = getIntList() for i in range(m): result[i][j] = curr[i] return result n, k, d = getIntList() a = getIntList() a.sort() cuttable = [False] * len(a) cuttable[0] = True def search(a): curr = 0 upLim = 0 upLimPrev = 0 while True: if cuttable[curr]: lowLim = curr + k upLimPrev = upLim while upLim < len(a) and a[upLim] - a[curr] <= d: upLim += 1 if upLim == len(a): return True lowLim = max(lowLim, upLimPrev + 1) for i in range(lowLim, upLim + 1): cuttable[i] = True curr += 1 if curr > len(a) - k: break return False if k == 1: print("YES") elif search(a): print("YES") else: print("NO")
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR RETURN NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence a_1, a_2, ..., a_{n} of n integer numbers β€” saturation of the color of each pencil. Now Mishka wants to put all the mess in the pack in order. He has an infinite number of empty boxes to do this. He would like to fill some boxes in such a way that: Each pencil belongs to exactly one box; Each non-empty box has at least k pencils in it; If pencils i and j belong to the same box, then |a_{i} - a_{j}| ≀ d, where |x| means absolute value of x. Note that the opposite is optional, there can be pencils i and j such that |a_{i} - a_{j}| ≀ d and they belong to different boxes. Help Mishka to determine if it's possible to distribute all the pencils into boxes. Print "YES" if there exists such a distribution. Otherwise print "NO". -----Input----- The first line contains three integer numbers n, k and d (1 ≀ k ≀ n ≀ 5Β·10^5, 0 ≀ d ≀ 10^9) β€” the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively. The second line contains n integer numbers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” saturation of color of each pencil. -----Output----- Print "YES" if it's possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print "NO". -----Examples----- Input 6 3 10 7 2 7 7 4 2 Output YES Input 6 2 3 4 5 3 13 4 10 Output YES Input 3 2 5 10 16 22 Output NO -----Note----- In the first example it is possible to distribute pencils into 2 boxes with 3 pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won't exceed 10. In the second example you can split pencils of saturations [4, 5, 3, 4] into 2 boxes of size 2 and put the remaining ones into another box.
def bi_search(a, x): n = len(a) u, l = n, -1 while u - l > 1: md = (u + l) // 2 if x > a[md]: l = md else: u = md return u class Bit: def __init__(self, n): self.bit = [0] * (n + 1) self.n = n def sum_prefix(self, i): s = 0 while i > 0: s += self.bit[i] i -= i & -i return s def sum_(self, l, r): if l == 1: return self.sum_prefix(r) return self.sum_prefix(r) - self.sum_prefix(l - 1) def add(self, i, x): while i <= self.n: self.bit[i] += x i += i & -i n, k, d = map(int, input().split()) a = [-1] + list(map(int, input().split())) a = sorted(a) B = Bit(n) ans = [-1] * n for i in range(n + 1): if i == 0: continue pos = bi_search(a, max(0, a[i] - d)) l, r = pos - 1, i - k if l <= r: s = 0 if l >= 1: s += B.sum_(l, r) if l == 0 or s > 0: B.add(i, 1) ans[i - 1] = 1 if ans[-1] == 1: print("YES") else: print("NO")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence a_1, a_2, ..., a_{n} of n integer numbers β€” saturation of the color of each pencil. Now Mishka wants to put all the mess in the pack in order. He has an infinite number of empty boxes to do this. He would like to fill some boxes in such a way that: Each pencil belongs to exactly one box; Each non-empty box has at least k pencils in it; If pencils i and j belong to the same box, then |a_{i} - a_{j}| ≀ d, where |x| means absolute value of x. Note that the opposite is optional, there can be pencils i and j such that |a_{i} - a_{j}| ≀ d and they belong to different boxes. Help Mishka to determine if it's possible to distribute all the pencils into boxes. Print "YES" if there exists such a distribution. Otherwise print "NO". -----Input----- The first line contains three integer numbers n, k and d (1 ≀ k ≀ n ≀ 5Β·10^5, 0 ≀ d ≀ 10^9) β€” the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively. The second line contains n integer numbers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” saturation of color of each pencil. -----Output----- Print "YES" if it's possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print "NO". -----Examples----- Input 6 3 10 7 2 7 7 4 2 Output YES Input 6 2 3 4 5 3 13 4 10 Output YES Input 3 2 5 10 16 22 Output NO -----Note----- In the first example it is possible to distribute pencils into 2 boxes with 3 pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won't exceed 10. In the second example you can split pencils of saturations [4, 5, 3, 4] into 2 boxes of size 2 and put the remaining ones into another box.
n, k, d = map(int, input().split()) A = sorted(map(int, input().split())) can_do = [0] * (n + 1) can_do[0] = 1 l, r = 0, 0 for i, a in enumerate(A, start=1): while a - d > A[l]: l += 1 r = i - k can = 0 if r >= l: s = can_do[r] if l: s -= can_do[l - 1] if s > 0: can = 1 can_do[i] = can_do[i - 1] + can print("YES" if can_do[n] - can_do[n - 1] > 0 else "NO")
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 ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER STRING STRING
Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence a_1, a_2, ..., a_{n} of n integer numbers β€” saturation of the color of each pencil. Now Mishka wants to put all the mess in the pack in order. He has an infinite number of empty boxes to do this. He would like to fill some boxes in such a way that: Each pencil belongs to exactly one box; Each non-empty box has at least k pencils in it; If pencils i and j belong to the same box, then |a_{i} - a_{j}| ≀ d, where |x| means absolute value of x. Note that the opposite is optional, there can be pencils i and j such that |a_{i} - a_{j}| ≀ d and they belong to different boxes. Help Mishka to determine if it's possible to distribute all the pencils into boxes. Print "YES" if there exists such a distribution. Otherwise print "NO". -----Input----- The first line contains three integer numbers n, k and d (1 ≀ k ≀ n ≀ 5Β·10^5, 0 ≀ d ≀ 10^9) β€” the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively. The second line contains n integer numbers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” saturation of color of each pencil. -----Output----- Print "YES" if it's possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print "NO". -----Examples----- Input 6 3 10 7 2 7 7 4 2 Output YES Input 6 2 3 4 5 3 13 4 10 Output YES Input 3 2 5 10 16 22 Output NO -----Note----- In the first example it is possible to distribute pencils into 2 boxes with 3 pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won't exceed 10. In the second example you can split pencils of saturations [4, 5, 3, 4] into 2 boxes of size 2 and put the remaining ones into another box.
import sys readline = sys.stdin.readline class Segtree: def __init__(self, A, intv, initialize=True, segf=max): self.N = len(A) self.N0 = 2 ** (self.N - 1).bit_length() self.intv = intv self.segf = segf if initialize: self.data = [intv] * self.N0 + A + [intv] * (self.N0 - self.N) for i in range(self.N0 - 1, 0, -1): self.data[i] = self.segf(self.data[2 * i], self.data[2 * i + 1]) else: self.data = [intv] * (2 * self.N0) def update(self, k, x): k += self.N0 self.data[k] = x while k > 0: k = k >> 1 self.data[k] = self.segf(self.data[2 * k], self.data[2 * k + 1]) def query(self, l, r): L, R = l + self.N0, r + self.N0 s = self.intv while L < R: if R & 1: R -= 1 s = self.segf(s, self.data[R]) if L & 1: s = self.segf(s, self.data[L]) L += 1 L >>= 1 R >>= 1 return s def binsearch(self, l, r, check, reverse=False): L, R = l + self.N0, r + self.N0 SL, SR = [], [] while L < R: if R & 1: R -= 1 SR.append(R) if L & 1: SL.append(L) L += 1 L >>= 1 R >>= 1 if reverse: for idx in SR + SL[::-1]: if check(self.data[idx]): break else: return -1 while idx < self.N0: if check(self.data[2 * idx + 1]): idx = 2 * idx + 1 else: idx = 2 * idx return idx - self.N0 else: for idx in SL + SR[::-1]: if check(self.data[idx]): break else: return -1 while idx < self.N0: if check(self.data[2 * idx]): idx = 2 * idx else: idx = 2 * idx + 1 return idx - self.N0 N, K, D = list(map(int, readline().split())) A = list(map(int, readline().split())) A.sort() lm = [None] * N for i in range(N): a = A[i] ok = i ng = -1 while abs(ok - ng) > 1: med = (ok + ng) // 2 if a - A[med] <= D: ok = med else: ng = med lm[i] = ok T = Segtree([None] * (N + 1), 0, initialize=False, segf=max) T.update(0, 1) for i in range(N): rm = i - K + 2 if rm <= lm[i]: continue if T.query(lm[i], rm): T.update(i + 1, 1) print("YES" if T.query(N, N + 1) else "NO")
IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP LIST VAR VAR VAR BIN_OP LIST VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP NUMBER VAR FUNC_DEF VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR LIST LIST WHILE VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FOR VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR RETURN BIN_OP VAR VAR FOR VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR 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 EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP LIST NONE BIN_OP VAR NUMBER NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING STRING
Mishka received a gift of multicolored pencils for his birthday! Unfortunately he lives in a monochrome world, where everything is of the same color and only saturation differs. This pack can be represented as a sequence a_1, a_2, ..., a_{n} of n integer numbers β€” saturation of the color of each pencil. Now Mishka wants to put all the mess in the pack in order. He has an infinite number of empty boxes to do this. He would like to fill some boxes in such a way that: Each pencil belongs to exactly one box; Each non-empty box has at least k pencils in it; If pencils i and j belong to the same box, then |a_{i} - a_{j}| ≀ d, where |x| means absolute value of x. Note that the opposite is optional, there can be pencils i and j such that |a_{i} - a_{j}| ≀ d and they belong to different boxes. Help Mishka to determine if it's possible to distribute all the pencils into boxes. Print "YES" if there exists such a distribution. Otherwise print "NO". -----Input----- The first line contains three integer numbers n, k and d (1 ≀ k ≀ n ≀ 5Β·10^5, 0 ≀ d ≀ 10^9) β€” the number of pencils, minimal size of any non-empty box and maximal difference in saturation between any pair of pencils in the same box, respectively. The second line contains n integer numbers a_1, a_2, ..., a_{n} (1 ≀ a_{i} ≀ 10^9) β€” saturation of color of each pencil. -----Output----- Print "YES" if it's possible to distribute all the pencils into boxes and satisfy all the conditions. Otherwise print "NO". -----Examples----- Input 6 3 10 7 2 7 7 4 2 Output YES Input 6 2 3 4 5 3 13 4 10 Output YES Input 3 2 5 10 16 22 Output NO -----Note----- In the first example it is possible to distribute pencils into 2 boxes with 3 pencils in each with any distribution. And you also can put all the pencils into the same box, difference of any pair in it won't exceed 10. In the second example you can split pencils of saturations [4, 5, 3, 4] into 2 boxes of size 2 and put the remaining ones into another box.
import sys n, k, d = map(int, input().split()) a = [0] + sorted(map(int, input().split())) dp = [1] + [0] * n j = k for i in range(n): if dp[i] == 0: continue j = max(j, i + k) while j <= n and a[j] - a[i + 1] <= d: dp[j] |= dp[i] j += 1 print("YES" if dp[-1] else "NO")
IMPORT ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR WHILE VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING STRING
During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables. Now she has a table filled with integers. The table consists of n rows and m columns. By a_{i}, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if a_{i}, j ≀ a_{i} + 1, j for all i from 1 to n - 1. Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that a_{i}, j ≀ a_{i} + 1, j for all i from l to r - 1 inclusive. Alyona is too small to deal with this task and asks you to help! -----Input----- The first line of the input contains two positive integers n and m (1 ≀ nΒ·m ≀ 100 000)Β β€” the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table. Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for a_{i}, j (1 ≀ a_{i}, j ≀ 10^9). The next line of the input contains an integer k (1 ≀ k ≀ 100 000)Β β€” the number of task that teacher gave to Alyona. The i-th of the next k lines contains two integers l_{i} and r_{i} (1 ≀ l_{i} ≀ r_{i} ≀ n). -----Output----- Print "Yes" to the i-th line of the output if the table consisting of rows from l_{i} to r_{i} inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No". -----Example----- Input 5 4 1 2 3 5 3 1 3 2 4 5 2 3 5 5 3 2 4 4 3 4 6 1 1 2 5 4 5 3 5 1 3 1 5 Output Yes No Yes Yes Yes No -----Note----- In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.
from sys import stdin n, m = list(map(int, stdin.readline().rstrip().split())) data = [] for i in range(0, n): data.append(list(map(int, stdin.readline().rstrip().split()))) sorted_data = [0] * n for col in range(0, m): mark_r = 0 cur_r = 1 st = [] while cur_r < n: if data[cur_r][col] < data[cur_r - 1][col]: st.extend([cur_r - 1] * (cur_r - mark_r)) mark_r = cur_r cur_r += 1 st.extend([cur_r - 1] * (cur_r - mark_r)) for index, item in enumerate(st): if sorted_data[index] < st[index]: sorted_data[index] = st[index] q_count = int(stdin.readline().rstrip()) for _ in range(0, q_count): l, r = [(int(x) - 1) for x in stdin.readline().rstrip().split()] if sorted_data[l] >= r: print("Yes") else: print("No")
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables. Now she has a table filled with integers. The table consists of n rows and m columns. By a_{i}, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if a_{i}, j ≀ a_{i} + 1, j for all i from 1 to n - 1. Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that a_{i}, j ≀ a_{i} + 1, j for all i from l to r - 1 inclusive. Alyona is too small to deal with this task and asks you to help! -----Input----- The first line of the input contains two positive integers n and m (1 ≀ nΒ·m ≀ 100 000)Β β€” the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table. Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for a_{i}, j (1 ≀ a_{i}, j ≀ 10^9). The next line of the input contains an integer k (1 ≀ k ≀ 100 000)Β β€” the number of task that teacher gave to Alyona. The i-th of the next k lines contains two integers l_{i} and r_{i} (1 ≀ l_{i} ≀ r_{i} ≀ n). -----Output----- Print "Yes" to the i-th line of the output if the table consisting of rows from l_{i} to r_{i} inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No". -----Example----- Input 5 4 1 2 3 5 3 1 3 2 4 5 2 3 5 5 3 2 4 4 3 4 6 1 1 2 5 4 5 3 5 1 3 1 5 Output Yes No Yes Yes Yes No -----Note----- In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.
import sys input = sys.stdin.readline n, m = map(int, input().split()) a = [list(map(int, input().split())) for i in range(n)] v = [([1] * m) for i in range(n)] for j in range(m): for i in range(n - 1)[::-1]: if a[i][j] <= a[i + 1][j]: v[i][j] = v[i + 1][j] + 1 max_l = [max(arr) for arr in v] q = int(input()) for _ in range(q): l, r = map(int, input().split()) if max_l[l - 1] >= r - l + 1: print("Yes") else: print("No")
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables. Now she has a table filled with integers. The table consists of n rows and m columns. By a_{i}, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if a_{i}, j ≀ a_{i} + 1, j for all i from 1 to n - 1. Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that a_{i}, j ≀ a_{i} + 1, j for all i from l to r - 1 inclusive. Alyona is too small to deal with this task and asks you to help! -----Input----- The first line of the input contains two positive integers n and m (1 ≀ nΒ·m ≀ 100 000)Β β€” the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table. Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for a_{i}, j (1 ≀ a_{i}, j ≀ 10^9). The next line of the input contains an integer k (1 ≀ k ≀ 100 000)Β β€” the number of task that teacher gave to Alyona. The i-th of the next k lines contains two integers l_{i} and r_{i} (1 ≀ l_{i} ≀ r_{i} ≀ n). -----Output----- Print "Yes" to the i-th line of the output if the table consisting of rows from l_{i} to r_{i} inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No". -----Example----- Input 5 4 1 2 3 5 3 1 3 2 4 5 2 3 5 5 3 2 4 4 3 4 6 1 1 2 5 4 5 3 5 1 3 1 5 Output Yes No Yes Yes Yes No -----Note----- In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.
def ri(): return map(int, input().split()) n, m = ri() a = [] for i in range(n): a.append(list(ri())) t = [[(0) for i in range(m)] for j in range(n)] T = [(0) for i in range(n)] for i in range(m): for j in range(n - 1): if a[j][i] > a[j + 1][i]: t[j + 1][i] = 0 else: t[j + 1][i] = t[j][i] + 1 for i in range(n): T[i] = max(t[i]) k = int(input()) ans = [] for i in range(k): l, r = ri() l = l - 1 r = r - 1 if T[r] >= r - l: ans.append("Yes") else: ans.append("No") print("\n".join(ans))
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR 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 NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables. Now she has a table filled with integers. The table consists of n rows and m columns. By a_{i}, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if a_{i}, j ≀ a_{i} + 1, j for all i from 1 to n - 1. Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that a_{i}, j ≀ a_{i} + 1, j for all i from l to r - 1 inclusive. Alyona is too small to deal with this task and asks you to help! -----Input----- The first line of the input contains two positive integers n and m (1 ≀ nΒ·m ≀ 100 000)Β β€” the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table. Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for a_{i}, j (1 ≀ a_{i}, j ≀ 10^9). The next line of the input contains an integer k (1 ≀ k ≀ 100 000)Β β€” the number of task that teacher gave to Alyona. The i-th of the next k lines contains two integers l_{i} and r_{i} (1 ≀ l_{i} ≀ r_{i} ≀ n). -----Output----- Print "Yes" to the i-th line of the output if the table consisting of rows from l_{i} to r_{i} inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No". -----Example----- Input 5 4 1 2 3 5 3 1 3 2 4 5 2 3 5 5 3 2 4 4 3 4 6 1 1 2 5 4 5 3 5 1 3 1 5 Output Yes No Yes Yes Yes No -----Note----- In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.
n, m = map(int, input().split()) Table = [] for r in range(n): row = list(map(int, input().split())) Table.append(row) k = int(input()) Tasklist = [] for task in range(k): l, r = map(int, input().split()) Tasklist.append([l, r]) locate = [0] * (n + 1) for c in range(m): start = 0 for r in range(0, n - 1): if Table[r][c] <= Table[r + 1][c]: pass else: for index in range(start, r + 1): if locate[index] < r: locate[index] = r start = r + 1 for index in range(start, n): if locate[index] < n - 1: locate[index] = n - 1 for task in range(k): Task = Tasklist[task] l = Task[0] r = Task[1] if locate[l - 1] >= r - 1: print("Yes") else: print("No")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables. Now she has a table filled with integers. The table consists of n rows and m columns. By a_{i}, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if a_{i}, j ≀ a_{i} + 1, j for all i from 1 to n - 1. Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that a_{i}, j ≀ a_{i} + 1, j for all i from l to r - 1 inclusive. Alyona is too small to deal with this task and asks you to help! -----Input----- The first line of the input contains two positive integers n and m (1 ≀ nΒ·m ≀ 100 000)Β β€” the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table. Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for a_{i}, j (1 ≀ a_{i}, j ≀ 10^9). The next line of the input contains an integer k (1 ≀ k ≀ 100 000)Β β€” the number of task that teacher gave to Alyona. The i-th of the next k lines contains two integers l_{i} and r_{i} (1 ≀ l_{i} ≀ r_{i} ≀ n). -----Output----- Print "Yes" to the i-th line of the output if the table consisting of rows from l_{i} to r_{i} inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No". -----Example----- Input 5 4 1 2 3 5 3 1 3 2 4 5 2 3 5 5 3 2 4 4 3 4 6 1 1 2 5 4 5 3 5 1 3 1 5 Output Yes No Yes Yes Yes No -----Note----- In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.
from sys import stdin, stdout def main(): n, m = map(int, stdin.readline().split()) k = [] for i in range(n): k.append(list(map(int, stdin.readline().split()))) ans = [] BASE = [(1) for i in range(m)] ans.append(list(BASE)) for i in range(1, n): ans.append(list(BASE)) for j in range(m): if k[i][j] >= k[i - 1][j]: ans[i][j] = ans[i - 1][j] + 1 else: ans[i][j] = 1 max_ans = [(0) for i in range(n)] for i in range(n): max_ans[i] = max(ans[i]) k = int(stdin.readline()) answer = [] for i in range(k): l, r = map(int, stdin.readline().split()) if max_ans[r - 1] >= r - l + 1: answer.append("Yes\n") else: answer.append("No\n") stdout.write("".join(answer)) main()
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables. Now she has a table filled with integers. The table consists of n rows and m columns. By a_{i}, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if a_{i}, j ≀ a_{i} + 1, j for all i from 1 to n - 1. Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that a_{i}, j ≀ a_{i} + 1, j for all i from l to r - 1 inclusive. Alyona is too small to deal with this task and asks you to help! -----Input----- The first line of the input contains two positive integers n and m (1 ≀ nΒ·m ≀ 100 000)Β β€” the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table. Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for a_{i}, j (1 ≀ a_{i}, j ≀ 10^9). The next line of the input contains an integer k (1 ≀ k ≀ 100 000)Β β€” the number of task that teacher gave to Alyona. The i-th of the next k lines contains two integers l_{i} and r_{i} (1 ≀ l_{i} ≀ r_{i} ≀ n). -----Output----- Print "Yes" to the i-th line of the output if the table consisting of rows from l_{i} to r_{i} inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No". -----Example----- Input 5 4 1 2 3 5 3 1 3 2 4 5 2 3 5 5 3 2 4 4 3 4 6 1 1 2 5 4 5 3 5 1 3 1 5 Output Yes No Yes Yes Yes No -----Note----- In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.
import sys input = sys.stdin.readline mod = 1000000007 def gcd(a, b): if a == 0: return b return gcd(b % a, a) def lcm(a, b): return a * b / gcd(a, b) def main(): n, m = map(int, input().split()) a = [] fei = [] maxi = [(1) for i in range(n)] for i in range(n): a.append(list(map(int, input().split()))) fei.append([(1) for j in range(m)]) for j in range(m): prev = 0 for i in range(1, n): if a[i][j] >= a[i - 1][j]: fei[i][j] = fei[i - 1][j] + 1 maxi[i] = max(maxi[i], fei[i][j]) k = int(input()) while k: l, r = map(int, input().split()) l -= 1 r -= 1 if maxi[r] >= r - l + 1: print("Yes") else: print("No") k -= 1 return main()
IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER RETURN EXPR FUNC_CALL VAR
During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables. Now she has a table filled with integers. The table consists of n rows and m columns. By a_{i}, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if a_{i}, j ≀ a_{i} + 1, j for all i from 1 to n - 1. Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that a_{i}, j ≀ a_{i} + 1, j for all i from l to r - 1 inclusive. Alyona is too small to deal with this task and asks you to help! -----Input----- The first line of the input contains two positive integers n and m (1 ≀ nΒ·m ≀ 100 000)Β β€” the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table. Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for a_{i}, j (1 ≀ a_{i}, j ≀ 10^9). The next line of the input contains an integer k (1 ≀ k ≀ 100 000)Β β€” the number of task that teacher gave to Alyona. The i-th of the next k lines contains two integers l_{i} and r_{i} (1 ≀ l_{i} ≀ r_{i} ≀ n). -----Output----- Print "Yes" to the i-th line of the output if the table consisting of rows from l_{i} to r_{i} inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No". -----Example----- Input 5 4 1 2 3 5 3 1 3 2 4 5 2 3 5 5 3 2 4 4 3 4 6 1 1 2 5 4 5 3 5 1 3 1 5 Output Yes No Yes Yes Yes No -----Note----- In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.
n, m = map(int, input().split()) l = [] ans = [] for i in range(n): k = list(map(int, input().split())) l.append(k) c = [1] * len(k) ans.append(c) hash = {} for j in range(m): count = 0 for i in range(1, n): if l[i][j] >= l[i - 1][j]: ans[i][j] = ans[i - 1][j] else: ans[i][j] = i + 1 bo = [] q = int(input()) for i in range(n): mini = 10**18 for j in range(m): mini = min(ans[i][j], mini) bo.append(mini) lo = [] for i in range(q): a, b = map(int, input().split()) try: hash[a, b] except: if a == b: lo.append("Yes") hash[a, b] = "Yes" elif bo[b - 1] <= a: lo.append("Yes") hash[a, b] = "Yes" else: lo.append("No") hash[a, b] = "No" else: lo.append(hash[a, b]) print(*lo)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR STRING IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables. Now she has a table filled with integers. The table consists of n rows and m columns. By a_{i}, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if a_{i}, j ≀ a_{i} + 1, j for all i from 1 to n - 1. Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that a_{i}, j ≀ a_{i} + 1, j for all i from l to r - 1 inclusive. Alyona is too small to deal with this task and asks you to help! -----Input----- The first line of the input contains two positive integers n and m (1 ≀ nΒ·m ≀ 100 000)Β β€” the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table. Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for a_{i}, j (1 ≀ a_{i}, j ≀ 10^9). The next line of the input contains an integer k (1 ≀ k ≀ 100 000)Β β€” the number of task that teacher gave to Alyona. The i-th of the next k lines contains two integers l_{i} and r_{i} (1 ≀ l_{i} ≀ r_{i} ≀ n). -----Output----- Print "Yes" to the i-th line of the output if the table consisting of rows from l_{i} to r_{i} inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No". -----Example----- Input 5 4 1 2 3 5 3 1 3 2 4 5 2 3 5 5 3 2 4 4 3 4 6 1 1 2 5 4 5 3 5 1 3 1 5 Output Yes No Yes Yes Yes No -----Note----- In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.
import sys N, M = list(map(int, input().split())) G = [list(map(int, input().split())) for i in range(N)] rmaxv = [(0) for i in range(N)] for i in range(M): reach = [(0) for j in range(N)] for j in range(N - 1, -1, -1): if j + 1 == N or not G[j][i] <= G[j + 1][i]: reach[j] = 1 else: reach[j] = reach[j + 1] + 1 rmaxv[j] = max(rmaxv[j], reach[j]) Q = int(input()) ans = [] for qi in range(Q): L, R = list(map(int, input().split())) if rmaxv[L - 1] >= R - L + 1: ans.append("Yes\n") else: ans.append("No\n") sys.stdout.write("".join(ans))
IMPORT ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables. Now she has a table filled with integers. The table consists of n rows and m columns. By a_{i}, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if a_{i}, j ≀ a_{i} + 1, j for all i from 1 to n - 1. Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that a_{i}, j ≀ a_{i} + 1, j for all i from l to r - 1 inclusive. Alyona is too small to deal with this task and asks you to help! -----Input----- The first line of the input contains two positive integers n and m (1 ≀ nΒ·m ≀ 100 000)Β β€” the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table. Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for a_{i}, j (1 ≀ a_{i}, j ≀ 10^9). The next line of the input contains an integer k (1 ≀ k ≀ 100 000)Β β€” the number of task that teacher gave to Alyona. The i-th of the next k lines contains two integers l_{i} and r_{i} (1 ≀ l_{i} ≀ r_{i} ≀ n). -----Output----- Print "Yes" to the i-th line of the output if the table consisting of rows from l_{i} to r_{i} inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No". -----Example----- Input 5 4 1 2 3 5 3 1 3 2 4 5 2 3 5 5 3 2 4 4 3 4 6 1 1 2 5 4 5 3 5 1 3 1 5 Output Yes No Yes Yes Yes No -----Note----- In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.
import sys def debug(x, table): for name, val in table.items(): if x is val: print("DEBUG:{} -> {}".format(name, val), file=sys.stderr) return None def solve(): n, m = map(int, sys.stdin.readline().split()) A = [[int(i) for i in sys.stdin.readline().split()] for j in range(n)] q = int(sys.stdin.readline().rstrip()) querys = [tuple(map(int, sys.stdin.readline().split())) for i in range(q)] f = [i for i in range(n)] for j in range(m): s = 0 for i in range(n): if i == n - 1 or A[i][j] > A[i + 1][j]: for k in range(s, i + 1): f[k] = max(f[k], i) s = i + 1 ans = [] for l, r in querys: if r - 1 <= f[l - 1]: ans.append("Yes") else: ans.append("No") print(*ans, sep="\n") solve()
IMPORT FUNC_DEF FOR VAR VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR RETURN NONE FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables. Now she has a table filled with integers. The table consists of n rows and m columns. By a_{i}, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if a_{i}, j ≀ a_{i} + 1, j for all i from 1 to n - 1. Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that a_{i}, j ≀ a_{i} + 1, j for all i from l to r - 1 inclusive. Alyona is too small to deal with this task and asks you to help! -----Input----- The first line of the input contains two positive integers n and m (1 ≀ nΒ·m ≀ 100 000)Β β€” the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table. Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for a_{i}, j (1 ≀ a_{i}, j ≀ 10^9). The next line of the input contains an integer k (1 ≀ k ≀ 100 000)Β β€” the number of task that teacher gave to Alyona. The i-th of the next k lines contains two integers l_{i} and r_{i} (1 ≀ l_{i} ≀ r_{i} ≀ n). -----Output----- Print "Yes" to the i-th line of the output if the table consisting of rows from l_{i} to r_{i} inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No". -----Example----- Input 5 4 1 2 3 5 3 1 3 2 4 5 2 3 5 5 3 2 4 4 3 4 6 1 1 2 5 4 5 3 5 1 3 1 5 Output Yes No Yes Yes Yes No -----Note----- In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.
from sys import stdin, stdout n, m = map(int, stdin.readline().strip().split()) arr = [list(map(int, stdin.readline().strip().split())) for i in range(n)] dp = [[(1) for i in range(m)] for i in range(n)] for i in range(1, n): for j in range(m): if arr[i][j] >= arr[i - 1][j]: dp[i][j] = dp[i - 1][j] + 1 dp = [max(i) for i in dp] s = [] for q in range(int(stdin.readline())): L, R = map(int, stdin.readline().strip().split()) if dp[R - 1] >= R - L + 1: s.append("Yes") else: s.append("No") stdout.write("\n".join(s))
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 VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables. Now she has a table filled with integers. The table consists of n rows and m columns. By a_{i}, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if a_{i}, j ≀ a_{i} + 1, j for all i from 1 to n - 1. Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that a_{i}, j ≀ a_{i} + 1, j for all i from l to r - 1 inclusive. Alyona is too small to deal with this task and asks you to help! -----Input----- The first line of the input contains two positive integers n and m (1 ≀ nΒ·m ≀ 100 000)Β β€” the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table. Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for a_{i}, j (1 ≀ a_{i}, j ≀ 10^9). The next line of the input contains an integer k (1 ≀ k ≀ 100 000)Β β€” the number of task that teacher gave to Alyona. The i-th of the next k lines contains two integers l_{i} and r_{i} (1 ≀ l_{i} ≀ r_{i} ≀ n). -----Output----- Print "Yes" to the i-th line of the output if the table consisting of rows from l_{i} to r_{i} inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No". -----Example----- Input 5 4 1 2 3 5 3 1 3 2 4 5 2 3 5 5 3 2 4 4 3 4 6 1 1 2 5 4 5 3 5 1 3 1 5 Output Yes No Yes Yes Yes No -----Note----- In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.
from sys import stdin, stdout n, m = map(int, stdin.readline().split()) ans = [float("infinity")] * n cou = [0] * m A = [0] * n A[0] = list(map(int, stdin.readline().split())) ans[0] = 0 for j in range(1, n): A[j] = list(map(int, stdin.readline().split())) for i in range(m): if A[j][i] < A[j - 1][i]: cou[i] = j ans[j] = min(ans[j], cou[i]) k = int(stdin.readline()) for j in range(k): per1, per2 = map(int, stdin.readline().split()) if ans[per2 - 1] + 1 <= per1: stdout.write("Yes") else: stdout.write("No") stdout.write("\n")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables. Now she has a table filled with integers. The table consists of n rows and m columns. By a_{i}, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if a_{i}, j ≀ a_{i} + 1, j for all i from 1 to n - 1. Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that a_{i}, j ≀ a_{i} + 1, j for all i from l to r - 1 inclusive. Alyona is too small to deal with this task and asks you to help! -----Input----- The first line of the input contains two positive integers n and m (1 ≀ nΒ·m ≀ 100 000)Β β€” the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table. Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for a_{i}, j (1 ≀ a_{i}, j ≀ 10^9). The next line of the input contains an integer k (1 ≀ k ≀ 100 000)Β β€” the number of task that teacher gave to Alyona. The i-th of the next k lines contains two integers l_{i} and r_{i} (1 ≀ l_{i} ≀ r_{i} ≀ n). -----Output----- Print "Yes" to the i-th line of the output if the table consisting of rows from l_{i} to r_{i} inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No". -----Example----- Input 5 4 1 2 3 5 3 1 3 2 4 5 2 3 5 5 3 2 4 4 3 4 6 1 1 2 5 4 5 3 5 1 3 1 5 Output Yes No Yes Yes Yes No -----Note----- In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.
from sys import stdin n, m = [int(i) for i in stdin.readline().split()] arr = [[int(i) for i in stdin.readline().split()] for _ in range(n)] temp = [[(1) for i in range(m)] for _ in range(n)] for i in range(m): for j in range(n - 2, -1, -1): if arr[j][i] <= arr[j + 1][i]: temp[j][i] += temp[j + 1][i] l = [0] * (n + 1) for i in range(n): for j in range(m): l[i + 1] = max(l[i + 1], temp[i][j]) k = int(input()) for i in range(k): l1, r = [int(i) for i in stdin.readline().split()] if l[l1] >= r - l1 + 1: print("Yes") else: print("No")
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables. Now she has a table filled with integers. The table consists of n rows and m columns. By a_{i}, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if a_{i}, j ≀ a_{i} + 1, j for all i from 1 to n - 1. Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that a_{i}, j ≀ a_{i} + 1, j for all i from l to r - 1 inclusive. Alyona is too small to deal with this task and asks you to help! -----Input----- The first line of the input contains two positive integers n and m (1 ≀ nΒ·m ≀ 100 000)Β β€” the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table. Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for a_{i}, j (1 ≀ a_{i}, j ≀ 10^9). The next line of the input contains an integer k (1 ≀ k ≀ 100 000)Β β€” the number of task that teacher gave to Alyona. The i-th of the next k lines contains two integers l_{i} and r_{i} (1 ≀ l_{i} ≀ r_{i} ≀ n). -----Output----- Print "Yes" to the i-th line of the output if the table consisting of rows from l_{i} to r_{i} inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No". -----Example----- Input 5 4 1 2 3 5 3 1 3 2 4 5 2 3 5 5 3 2 4 4 3 4 6 1 1 2 5 4 5 3 5 1 3 1 5 Output Yes No Yes Yes Yes No -----Note----- In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.
n, m = map(int, input().split()) A = [] for _ in range(n): A.append(list(map(int, input().split()))) dp = [[(1) for _ in range(m)] for _ in range(n)] best = [(1) for _ in range(n)] for i in range(1, n): for j in range(m): if A[i][j] >= A[i - 1][j]: dp[i][j] = dp[i - 1][j] + 1 best[i] = max(best[i], dp[i][j]) ans = "" k = int(input()) for _ in range(k): l, r = map(int, input().split()) if best[r - 1] >= r - l + 1: ans += "Yes\n" else: ans += "No\n" print(ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR STRING VAR STRING EXPR FUNC_CALL VAR VAR
During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables. Now she has a table filled with integers. The table consists of n rows and m columns. By a_{i}, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if a_{i}, j ≀ a_{i} + 1, j for all i from 1 to n - 1. Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that a_{i}, j ≀ a_{i} + 1, j for all i from l to r - 1 inclusive. Alyona is too small to deal with this task and asks you to help! -----Input----- The first line of the input contains two positive integers n and m (1 ≀ nΒ·m ≀ 100 000)Β β€” the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table. Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for a_{i}, j (1 ≀ a_{i}, j ≀ 10^9). The next line of the input contains an integer k (1 ≀ k ≀ 100 000)Β β€” the number of task that teacher gave to Alyona. The i-th of the next k lines contains two integers l_{i} and r_{i} (1 ≀ l_{i} ≀ r_{i} ≀ n). -----Output----- Print "Yes" to the i-th line of the output if the table consisting of rows from l_{i} to r_{i} inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No". -----Example----- Input 5 4 1 2 3 5 3 1 3 2 4 5 2 3 5 5 3 2 4 4 3 4 6 1 1 2 5 4 5 3 5 1 3 1 5 Output Yes No Yes Yes Yes No -----Note----- In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.
from sys import stdin, stdout n, m = map(int, stdin.readline().split()) values = [] for i in range(n): values.append(list(map(int, stdin.readline().split()))) d = {} questions = [] k = int(stdin.readline()) for i in range(k): l, r = map(int, stdin.readline().split()) questions.append((l, r)) for j in range(m): l, r = 0, 0 while r < n: if l == r: if r in d: d[r] = min(d[r], l) else: d[r] = l r += 1 elif values[r][j] >= values[r - 1][j]: if r in d: d[r] = min(d[r], l) else: d[r] = l r += 1 else: l = r for l, r in questions: l -= 1 r -= 1 if r in d and d[r] <= l: stdout.write("Yes\n") else: stdout.write("No\n")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables. Now she has a table filled with integers. The table consists of n rows and m columns. By a_{i}, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if a_{i}, j ≀ a_{i} + 1, j for all i from 1 to n - 1. Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that a_{i}, j ≀ a_{i} + 1, j for all i from l to r - 1 inclusive. Alyona is too small to deal with this task and asks you to help! -----Input----- The first line of the input contains two positive integers n and m (1 ≀ nΒ·m ≀ 100 000)Β β€” the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table. Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for a_{i}, j (1 ≀ a_{i}, j ≀ 10^9). The next line of the input contains an integer k (1 ≀ k ≀ 100 000)Β β€” the number of task that teacher gave to Alyona. The i-th of the next k lines contains two integers l_{i} and r_{i} (1 ≀ l_{i} ≀ r_{i} ≀ n). -----Output----- Print "Yes" to the i-th line of the output if the table consisting of rows from l_{i} to r_{i} inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No". -----Example----- Input 5 4 1 2 3 5 3 1 3 2 4 5 2 3 5 5 3 2 4 4 3 4 6 1 1 2 5 4 5 3 5 1 3 1 5 Output Yes No Yes Yes Yes No -----Note----- In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.
import sys input = sys.stdin.buffer.readline n, m = map(int, input().split()) arr = [] for i in range(n): arr.append(list(map(int, input().split()))) ans = [n + 3] * n ans[0] = 0 dp = [0] * m for i in range(1, n): for j in range(m): if arr[i][j] < arr[i - 1][j]: dp[j] = i ans[i] = min(ans[i], dp[j]) for i in range(int(input())): l, r = map(int, input().split()) l -= 1 r -= 1 if ans[r] <= l: print("Yes") continue print("No")
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables. Now she has a table filled with integers. The table consists of n rows and m columns. By a_{i}, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if a_{i}, j ≀ a_{i} + 1, j for all i from 1 to n - 1. Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that a_{i}, j ≀ a_{i} + 1, j for all i from l to r - 1 inclusive. Alyona is too small to deal with this task and asks you to help! -----Input----- The first line of the input contains two positive integers n and m (1 ≀ nΒ·m ≀ 100 000)Β β€” the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table. Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for a_{i}, j (1 ≀ a_{i}, j ≀ 10^9). The next line of the input contains an integer k (1 ≀ k ≀ 100 000)Β β€” the number of task that teacher gave to Alyona. The i-th of the next k lines contains two integers l_{i} and r_{i} (1 ≀ l_{i} ≀ r_{i} ≀ n). -----Output----- Print "Yes" to the i-th line of the output if the table consisting of rows from l_{i} to r_{i} inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No". -----Example----- Input 5 4 1 2 3 5 3 1 3 2 4 5 2 3 5 5 3 2 4 4 3 4 6 1 1 2 5 4 5 3 5 1 3 1 5 Output Yes No Yes Yes Yes No -----Note----- In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.
import sys input = sys.stdin.readline n, m = map(int, input().split()) arr = [] for i in range(n): arr.append(list(map(int, input().split()))) mp = [] for i in range(n): mp.append(i + 1) for i in range(m): pre = -1 st = 0 for j in range(n): if arr[j][i] < pre: pre = arr[j][i] st = j mp[j] = min(mp[j], st) else: mp[j] = min(mp[j], st) pre = arr[j][i] m = int(input()) for i in range(m): a, b = map(int, input().split()) if mp[b - 1] <= a - 1: print("Yes") else: print("No")
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables. Now she has a table filled with integers. The table consists of n rows and m columns. By a_{i}, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if a_{i}, j ≀ a_{i} + 1, j for all i from 1 to n - 1. Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that a_{i}, j ≀ a_{i} + 1, j for all i from l to r - 1 inclusive. Alyona is too small to deal with this task and asks you to help! -----Input----- The first line of the input contains two positive integers n and m (1 ≀ nΒ·m ≀ 100 000)Β β€” the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table. Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for a_{i}, j (1 ≀ a_{i}, j ≀ 10^9). The next line of the input contains an integer k (1 ≀ k ≀ 100 000)Β β€” the number of task that teacher gave to Alyona. The i-th of the next k lines contains two integers l_{i} and r_{i} (1 ≀ l_{i} ≀ r_{i} ≀ n). -----Output----- Print "Yes" to the i-th line of the output if the table consisting of rows from l_{i} to r_{i} inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No". -----Example----- Input 5 4 1 2 3 5 3 1 3 2 4 5 2 3 5 5 3 2 4 4 3 4 6 1 1 2 5 4 5 3 5 1 3 1 5 Output Yes No Yes Yes Yes No -----Note----- In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.
import sys input = sys.stdin.readline n, m = map(int, input().split()) a = [] for i in range(n): a.append(list(map(int, input().split()))) count = [[(1) for i in range(m)] for j in range(n)] for i in range(1, n): for j in range(m): if a[i][j] >= a[i - 1][j]: count[i][j] = count[i - 1][j] + 1 maxx = [] for i in range(n): maxx.append(max(count[i])) for i in range(int(input())): l, r = map(int, input().split()) if l == r: print("Yes") continue if maxx[r - 1] >= r - l + 1: print("Yes") else: print("No")
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables. Now she has a table filled with integers. The table consists of n rows and m columns. By a_{i}, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if a_{i}, j ≀ a_{i} + 1, j for all i from 1 to n - 1. Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that a_{i}, j ≀ a_{i} + 1, j for all i from l to r - 1 inclusive. Alyona is too small to deal with this task and asks you to help! -----Input----- The first line of the input contains two positive integers n and m (1 ≀ nΒ·m ≀ 100 000)Β β€” the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table. Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for a_{i}, j (1 ≀ a_{i}, j ≀ 10^9). The next line of the input contains an integer k (1 ≀ k ≀ 100 000)Β β€” the number of task that teacher gave to Alyona. The i-th of the next k lines contains two integers l_{i} and r_{i} (1 ≀ l_{i} ≀ r_{i} ≀ n). -----Output----- Print "Yes" to the i-th line of the output if the table consisting of rows from l_{i} to r_{i} inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No". -----Example----- Input 5 4 1 2 3 5 3 1 3 2 4 5 2 3 5 5 3 2 4 4 3 4 6 1 1 2 5 4 5 3 5 1 3 1 5 Output Yes No Yes Yes Yes No -----Note----- In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.
import sys input = sys.stdin.readline n, m = list(map(int, input().split())) M = [[] for i in range(m)] for i in range(n): z = list(map(int, input().split())) for i in range(len(z)): M[i].append(z[i]) dp = [(0) for i in range(n)] dp[-1] = 1 for i in range(len(M)): x = M[i] temp = [(0) for i in range(len(x))] temp[0] = 1 for i in range(1, len(x)): if x[i] >= x[i - 1]: temp[i] = 1 else: temp[i] = 2 re = [(0) for i in range(len(temp))] re[-1] = 1 for i in range(len(re) - 2, -1, -1): if temp[i + 1] == 2: re[i] = 1 else: re[i] = re[i + 1] + 1 for i in range(len(dp)): dp[i] = max(dp[i], re[i]) r = int(input()) dp[-1] = 1 for i in range(r): l, r = list(map(int, input().split())) if dp[l - 1] >= r - l + 1: print("Yes") else: print("No")
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables. Now she has a table filled with integers. The table consists of n rows and m columns. By a_{i}, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if a_{i}, j ≀ a_{i} + 1, j for all i from 1 to n - 1. Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that a_{i}, j ≀ a_{i} + 1, j for all i from l to r - 1 inclusive. Alyona is too small to deal with this task and asks you to help! -----Input----- The first line of the input contains two positive integers n and m (1 ≀ nΒ·m ≀ 100 000)Β β€” the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table. Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for a_{i}, j (1 ≀ a_{i}, j ≀ 10^9). The next line of the input contains an integer k (1 ≀ k ≀ 100 000)Β β€” the number of task that teacher gave to Alyona. The i-th of the next k lines contains two integers l_{i} and r_{i} (1 ≀ l_{i} ≀ r_{i} ≀ n). -----Output----- Print "Yes" to the i-th line of the output if the table consisting of rows from l_{i} to r_{i} inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No". -----Example----- Input 5 4 1 2 3 5 3 1 3 2 4 5 2 3 5 5 3 2 4 4 3 4 6 1 1 2 5 4 5 3 5 1 3 1 5 Output Yes No Yes Yes Yes No -----Note----- In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.
from sys import stdin lines = stdin.readlines() n = int(lines[0].split(" ")[0]) m = int(lines[0].split(" ")[1]) base_dict = {(i + 1): (i + 1) for i in range(n)} def yes_or_no(max_array, ll): l = int(ll[0]) r = int(ll[1]) if max_array[l - 1] >= r: return "Yes" return "No" a = [[int(i) for i in x.split(" ")] for x in lines[1 : n + 1]] for i in range(m): prev_minimum = 1 for j in range(1, n): if a[j][i] >= a[j - 1][i]: if base_dict[prev_minimum] < j + 1: base_dict[prev_minimum] = j + 1 else: prev_minimum = j + 1 max_array = [base_dict[i + 1] for i in range(n)] for i in range(1, n): max_array[i] = max(base_dict[i + 1], max_array[i - 1]) k = lines[n + 1] b = [yes_or_no(max_array, x.split(" ")) for x in lines[n + 2 :]] print("\n".join(b))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER STRING NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER STRING NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR RETURN STRING RETURN STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables. Now she has a table filled with integers. The table consists of n rows and m columns. By a_{i}, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if a_{i}, j ≀ a_{i} + 1, j for all i from 1 to n - 1. Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that a_{i}, j ≀ a_{i} + 1, j for all i from l to r - 1 inclusive. Alyona is too small to deal with this task and asks you to help! -----Input----- The first line of the input contains two positive integers n and m (1 ≀ nΒ·m ≀ 100 000)Β β€” the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table. Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for a_{i}, j (1 ≀ a_{i}, j ≀ 10^9). The next line of the input contains an integer k (1 ≀ k ≀ 100 000)Β β€” the number of task that teacher gave to Alyona. The i-th of the next k lines contains two integers l_{i} and r_{i} (1 ≀ l_{i} ≀ r_{i} ≀ n). -----Output----- Print "Yes" to the i-th line of the output if the table consisting of rows from l_{i} to r_{i} inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No". -----Example----- Input 5 4 1 2 3 5 3 1 3 2 4 5 2 3 5 5 3 2 4 4 3 4 6 1 1 2 5 4 5 3 5 1 3 1 5 Output Yes No Yes Yes Yes No -----Note----- In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.
n, m = [int(x) for x in input().split()] arr = [[int(x) for x in input().split()] for i in range(0, n)] p = [i for i in range(0, n + 1)] for j in range(0, m): cur = 0 for i in range(0, n - 1): if arr[i][j] <= arr[i + 1][j]: p[i + 1] = min(cur, p[i + 1]) else: cur = i + 1 q = int(input()) res = "" for _ in range(0, q): a, b = [int(x) for x in input().split()] b -= 1 a -= 1 if p[b] <= a: res += "Yes" else: res += "No" if _ != q - 1: res += "\n" print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR STRING VAR STRING IF VAR BIN_OP VAR NUMBER VAR STRING EXPR FUNC_CALL VAR VAR
During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables. Now she has a table filled with integers. The table consists of n rows and m columns. By a_{i}, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if a_{i}, j ≀ a_{i} + 1, j for all i from 1 to n - 1. Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that a_{i}, j ≀ a_{i} + 1, j for all i from l to r - 1 inclusive. Alyona is too small to deal with this task and asks you to help! -----Input----- The first line of the input contains two positive integers n and m (1 ≀ nΒ·m ≀ 100 000)Β β€” the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table. Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for a_{i}, j (1 ≀ a_{i}, j ≀ 10^9). The next line of the input contains an integer k (1 ≀ k ≀ 100 000)Β β€” the number of task that teacher gave to Alyona. The i-th of the next k lines contains two integers l_{i} and r_{i} (1 ≀ l_{i} ≀ r_{i} ≀ n). -----Output----- Print "Yes" to the i-th line of the output if the table consisting of rows from l_{i} to r_{i} inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No". -----Example----- Input 5 4 1 2 3 5 3 1 3 2 4 5 2 3 5 5 3 2 4 4 3 4 6 1 1 2 5 4 5 3 5 1 3 1 5 Output Yes No Yes Yes Yes No -----Note----- In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.
from sys import stdin input = stdin.readline n, m = map(int, input().split()) arr = list(list(map(int, input().split())) for _ in range(n)) dp = [[(1) for i in range(m)] for j in range(n)] for i in range(1, n): for j in range(m): if arr[i][j] >= arr[i - 1][j]: dp[i][j] = dp[i - 1][j] + 1 dp, all_res = [max(i) for i in dp], [] k = int(input()) quries = list(list(map(int, input().split())) for _ in range(k)) for l, r in quries: all_res.append("Yes" if dp[r - 1] >= r - l + 1 else "No") print("\n".join(all_res))
ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER STRING STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
This is a hard version of the problem. In this version, the given array can contain equal elements and the constraints on $n$ are greater than in the easy version of the problem. You are given an array $a$ of $n$ integers (the given array can contain equal elements). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array. For example, if $n = 5$, $a = [4, 7, 2, 2, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 2, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 2, 9, 4]$. You can perform operations of any type any number of times in any order. Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$)Β β€” the number of test cases in the test. Then $t$ test cases follow. Each test case starts with a line containing an integer $n$ ($1 \le n \le 2 \cdot 10^5$)Β β€” the size of the array $a$. Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β€” an array that needs to be sorted by the given operations. The given array can contain equal elements. The sum of $n$ for all test cases in one test does not exceed $2 \cdot 10^5$. -----Output----- For each test case output one integerΒ β€” the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order. -----Example----- Input 9 5 4 7 2 2 9 5 3 5 8 1 7 5 1 2 2 4 5 2 0 1 3 0 1 0 4 0 1 0 0 4 0 1 0 1 4 0 1 0 2 20 16 15 1 10 0 14 0 10 3 9 2 5 4 5 17 9 10 20 0 9 Output 2 2 0 0 1 1 1 1 16 -----Note----- In the first test case, you first need to move two 2, to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 2, 9] \rightarrow [2, 4, 7, 2, 9] \rightarrow [2, 2, 4, 7, 9]$. In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β€” to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$. In the third test case, the array is already sorted.
import sys int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] def main(): for _ in range(II()): inf = 10**9 n = II() aa = LI() s = set(aa) enc = {a: i for i, a in enumerate(sorted(s))} aa = [enc[a] for a in aa] first = [False] * n fin = [False] * len(s) for i in range(n): if fin[aa[i]]: continue first[i] = True fin[aa[i]] = True end = [False] * n fin = [False] * len(s) for i in range(n - 1, -1, -1): if fin[aa[i]]: continue end[i] = True fin[aa[i]] = True last = [-1] * len(s) dp = [([0] * 3) for _ in range(n)] mx = 0 for i, a in enumerate(aa): val = 1 i0 = last[a] if i0 != -1: val = dp[i0][0] + 1 dp[i][0] = val mx = max(mx, val) val = -inf if i0 != -1: val = dp[i0][1] + 1 i1 = -1 if a: i1 = last[a - 1] if i1 != -1: if first[i]: val = max(val, dp[i1][0] + 1) if end[i1] and first[i]: val = max(val, dp[i1][1] + 1) dp[i][1] = val mx = max(mx, val) val = -inf if i0 != -1: val = dp[i0][2] + 1 if i1 != -1: val = max(val, dp[i1][0] + 1) if end[i1]: val = max(val, dp[i1][1] + 1) dp[i][2] = val mx = max(mx, val) last[a] = i print(n - mx) main()
IMPORT ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR
This is a hard version of the problem. In this version, the given array can contain equal elements and the constraints on $n$ are greater than in the easy version of the problem. You are given an array $a$ of $n$ integers (the given array can contain equal elements). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array. For example, if $n = 5$, $a = [4, 7, 2, 2, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 2, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 2, 9, 4]$. You can perform operations of any type any number of times in any order. Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$)Β β€” the number of test cases in the test. Then $t$ test cases follow. Each test case starts with a line containing an integer $n$ ($1 \le n \le 2 \cdot 10^5$)Β β€” the size of the array $a$. Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β€” an array that needs to be sorted by the given operations. The given array can contain equal elements. The sum of $n$ for all test cases in one test does not exceed $2 \cdot 10^5$. -----Output----- For each test case output one integerΒ β€” the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order. -----Example----- Input 9 5 4 7 2 2 9 5 3 5 8 1 7 5 1 2 2 4 5 2 0 1 3 0 1 0 4 0 1 0 0 4 0 1 0 1 4 0 1 0 2 20 16 15 1 10 0 14 0 10 3 9 2 5 4 5 17 9 10 20 0 9 Output 2 2 0 0 1 1 1 1 16 -----Note----- In the first test case, you first need to move two 2, to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 2, 9] \rightarrow [2, 4, 7, 2, 9] \rightarrow [2, 2, 4, 7, 9]$. In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β€” to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$. In the third test case, the array is already sorted.
from sys import stdin, stdout def flying_sort(n, a): b = sorted(a) dic = {} seq = 1 dic[b[0]] = seq num = [(0) for i in range(n + 1)] head = [(0) for i in range(n + 1)] tail = [(-1) for i in range(n + 1)] pos = [(0) for i in range(n + 1)] for i in range(1, len(b)): if b[i] != b[i - 1]: seq += 1 dic[b[i]] = seq for i in range(len(a)): a[i] = dic[a[i]] num[a[i]] += 1 tail[a[i]] = i + 1 if head[a[i]] == 0: head[a[i]] = i + 1 dp = [[0, 0, 0] for i in range(n + 1)] maxseq = 0 for i in range(1, n + 1): v = a[i - 1] dp[i][0] = dp[pos[v]][0] + 1 dp[i][1] = max(dp[pos[v]][1] + 1, dp[pos[v - 1]][0] + 1, dp[pos[v - 1]][2] + 1) if tail[v] == i: dp[i][2] = dp[head[v]][1] + num[v] - 1 pos[v] = i for k in range(3): maxseq = max(maxseq, dp[i][k]) res = len(a) - maxseq return res t = int(stdin.readline()) for i in range(t): n = int(stdin.readline()) a = list(map(int, stdin.readline().split())) stdout.write(str(flying_sort(n, a)) + "\n")
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING
This is a hard version of the problem. In this version, the given array can contain equal elements and the constraints on $n$ are greater than in the easy version of the problem. You are given an array $a$ of $n$ integers (the given array can contain equal elements). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array. For example, if $n = 5$, $a = [4, 7, 2, 2, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 2, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 2, 9, 4]$. You can perform operations of any type any number of times in any order. Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$)Β β€” the number of test cases in the test. Then $t$ test cases follow. Each test case starts with a line containing an integer $n$ ($1 \le n \le 2 \cdot 10^5$)Β β€” the size of the array $a$. Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β€” an array that needs to be sorted by the given operations. The given array can contain equal elements. The sum of $n$ for all test cases in one test does not exceed $2 \cdot 10^5$. -----Output----- For each test case output one integerΒ β€” the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order. -----Example----- Input 9 5 4 7 2 2 9 5 3 5 8 1 7 5 1 2 2 4 5 2 0 1 3 0 1 0 4 0 1 0 0 4 0 1 0 1 4 0 1 0 2 20 16 15 1 10 0 14 0 10 3 9 2 5 4 5 17 9 10 20 0 9 Output 2 2 0 0 1 1 1 1 16 -----Note----- In the first test case, you first need to move two 2, to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 2, 9] \rightarrow [2, 4, 7, 2, 9] \rightarrow [2, 2, 4, 7, 9]$. In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β€” to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$. In the third test case, the array is already sorted.
from sys import stdin for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) id = list(zip(l, list(range(n)))) id.sort() val, pos = zip(*id) blok = [] cur = [pos[0]] for i in range(1, n): if val[i] == val[i - 1]: cur.append(pos[i]) else: cur.sort() blok.append(cur) cur = [pos[i]] cur.sort() blok.append(cur) best = 0 m = len(blok) for j in range(m): best = max(len(blok[j]), best) i = 0 while True: if i >= m - 2: break cyk = min(blok[i + 1]) j = -1 while j + 1 < len(blok[i]) and blok[i][j + 1] < cyk: j += 1 su = j + 1 ii = i + 2 while ii < m: if min(blok[ii]) > max(blok[ii - 1]): su += len(blok[ii - 1]) ii += 1 else: break if ii == m: su += len(blok[-1]) best = max(best, su) else: xxx = max(blok[ii - 1]) su += len(blok[ii - 1]) inde = len(blok[ii]) - 1 while inde >= 0 and blok[ii][inde] >= xxx: su += 1 inde -= 1 best = max(best, su) i = max(i + 1, ii - 1) for i in range(1, m): b1 = blok[i] b0 = blok[i - 1] l0, l1, i1 = len(b0), len(b1), 0 for ind in range(l0): while True: if i1 < l1 and b1[i1] <= b0[ind]: i1 += 1 else: break if l1 == i1: break best = max(best, ind + 1 + (l1 - i1)) print(n - best)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
This is a hard version of the problem. In this version, the given array can contain equal elements and the constraints on $n$ are greater than in the easy version of the problem. You are given an array $a$ of $n$ integers (the given array can contain equal elements). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array. For example, if $n = 5$, $a = [4, 7, 2, 2, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 2, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 2, 9, 4]$. You can perform operations of any type any number of times in any order. Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$)Β β€” the number of test cases in the test. Then $t$ test cases follow. Each test case starts with a line containing an integer $n$ ($1 \le n \le 2 \cdot 10^5$)Β β€” the size of the array $a$. Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β€” an array that needs to be sorted by the given operations. The given array can contain equal elements. The sum of $n$ for all test cases in one test does not exceed $2 \cdot 10^5$. -----Output----- For each test case output one integerΒ β€” the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order. -----Example----- Input 9 5 4 7 2 2 9 5 3 5 8 1 7 5 1 2 2 4 5 2 0 1 3 0 1 0 4 0 1 0 0 4 0 1 0 1 4 0 1 0 2 20 16 15 1 10 0 14 0 10 3 9 2 5 4 5 17 9 10 20 0 9 Output 2 2 0 0 1 1 1 1 16 -----Note----- In the first test case, you first need to move two 2, to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 2, 9] \rightarrow [2, 4, 7, 2, 9] \rightarrow [2, 2, 4, 7, 9]$. In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β€” to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$. In the third test case, the array is already sorted.
for t in range(int(input())): n = int(input()) aa = list(map(int, input().split(" "))) asort = sorted([(v, i) for i, v in enumerate(aa)]) inds = {} for a, ai in asort: if a not in inds: inds[a] = [] inds[a] += [ai] def left_eq(b, i): binds = inds[b] l = -1 r = len(binds) - 1 while l < r: m = (l + r + 1) // 2 if binds[m] < i: l = m else: r = m - 1 return l + 1 def right_eq(b, i): binds = inds[b] l = 0 r = len(binds) while l < r: m = (l + r) // 2 if binds[m] > i: r = m else: l = m + 1 return len(binds) - l ls = [None] * n for i, (a, ai) in enumerate(asort): if i == 0: ls[ai] = 1 continue if asort[i - 1][1] < ai: curl = ls[asort[i - 1][1]] + 1 else: curl = left_eq(asort[i - 1][0], ai) + 1 ls[ai] = curl for i, (a, ai) in reversed(list(enumerate(asort))): if i == n - 1: continue if asort[i + 1][0] != a: ls[ai] += right_eq(asort[i + 1][0], ai) alter = [-10] values = list(inds.keys()) v2vi = {v: i for i, v in enumerate(values)} for i, (a, ai) in enumerate(asort): avin = v2vi[a] if avin > 0: alter += [left_eq(values[avin - 1], ai) + 1 + right_eq(a, ai)] maxSeqLen = max(max(ls), max(alter)) print(n - maxSeqLen)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR LIST VAR VAR LIST VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR 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 BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR LIST BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
This is a hard version of the problem. In this version, the given array can contain equal elements and the constraints on $n$ are greater than in the easy version of the problem. You are given an array $a$ of $n$ integers (the given array can contain equal elements). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array. For example, if $n = 5$, $a = [4, 7, 2, 2, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 2, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 2, 9, 4]$. You can perform operations of any type any number of times in any order. Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$)Β β€” the number of test cases in the test. Then $t$ test cases follow. Each test case starts with a line containing an integer $n$ ($1 \le n \le 2 \cdot 10^5$)Β β€” the size of the array $a$. Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β€” an array that needs to be sorted by the given operations. The given array can contain equal elements. The sum of $n$ for all test cases in one test does not exceed $2 \cdot 10^5$. -----Output----- For each test case output one integerΒ β€” the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order. -----Example----- Input 9 5 4 7 2 2 9 5 3 5 8 1 7 5 1 2 2 4 5 2 0 1 3 0 1 0 4 0 1 0 0 4 0 1 0 1 4 0 1 0 2 20 16 15 1 10 0 14 0 10 3 9 2 5 4 5 17 9 10 20 0 9 Output 2 2 0 0 1 1 1 1 16 -----Note----- In the first test case, you first need to move two 2, to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 2, 9] \rightarrow [2, 4, 7, 2, 9] \rightarrow [2, 2, 4, 7, 9]$. In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β€” to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$. In the third test case, the array is already sorted.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) p = [] for i in range(n): p.append((a[i], i)) p.sort(key=lambda x: x[0]) aOrder = [-1] * n curIndex = 0 for i in range(n): if i >= 1 and p[i][0] != p[i - 1][0]: curIndex += 1 aOrder[p[i][1]] = curIndex aCount = [0] * (curIndex + 1) for i in range(n): aCount[aOrder[i]] += 1 maxSubseq0 = [0] * (curIndex + 1) maxSubseq1 = [0] * (curIndex + 1) maxSubseq2 = [0] * (curIndex + 1) maxSubseq3 = [0] * (curIndex + 1) for i in range(n): aCurOrder = aOrder[i] curSubseq1 = maxSubseq1[aCurOrder] + 1 curSubseq2 = maxSubseq2[aCurOrder] + 1 curSubseq3 = maxSubseq3[aCurOrder] + 1 if curSubseq1 == 1: curSubseq1 = 0 curSubseq2 = 0 if curSubseq3 == 1: curSubseq3 = 0 maxSubseq0[aCurOrder] += 1 justUpdated = False if aCurOrder >= 1 and maxSubseq0[aCurOrder - 1] > 0: if curSubseq1 == 0: curSubseq1 = maxSubseq0[aCurOrder - 1] + 1 curSubseq2 = 1 justUpdated = True elif ( curSubseq1 < maxSubseq0[aCurOrder - 1] + 1 and curSubseq3 < maxSubseq0[aCurOrder - 1] + 1 ): curSubseq3 = maxSubseq0[aCurOrder - 1] + 1 if ( aCurOrder >= 1 and maxSubseq1[aCurOrder - 1] > 0 and maxSubseq2[aCurOrder - 1] == aCount[aCurOrder - 1] ): if curSubseq1 == 0 or justUpdated: curSubseq1 = maxSubseq1[aCurOrder - 1] + 1 curSubseq2 = 1 elif ( curSubseq1 < maxSubseq1[aCurOrder - 1] + 1 and curSubseq3 < maxSubseq1[aCurOrder - 1] + 1 ): curSubseq3 = maxSubseq1[aCurOrder - 1] + 1 maxSubseq1[aCurOrder] = curSubseq1 maxSubseq2[aCurOrder] = curSubseq2 maxSubseq3[aCurOrder] = curSubseq3 print(n - max(maxSubseq0 + maxSubseq1 + maxSubseq3))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR
This is a hard version of the problem. In this version, the given array can contain equal elements and the constraints on $n$ are greater than in the easy version of the problem. You are given an array $a$ of $n$ integers (the given array can contain equal elements). You can perform the following operations on array elements: choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the begin of the array; choose any index $i$ ($1 \le i \le n$) and move the element $a[i]$ to the end of the array. For example, if $n = 5$, $a = [4, 7, 2, 2, 9]$, then the following sequence of operations can be performed: after performing the operation of the first type to the second element, the array $a$ will become $[7, 4, 2, 2, 9]$; after performing the operation of the second type to the second element, the array $a$ will become $[7, 2, 2, 9, 4]$. You can perform operations of any type any number of times in any order. Find the minimum total number of operations of the first and second type that will make the $a$ array sorted in non-decreasing order. In other words, what is the minimum number of operations must be performed so the array satisfies the inequalities $a[1] \le a[2] \le \ldots \le a[n]$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$)Β β€” the number of test cases in the test. Then $t$ test cases follow. Each test case starts with a line containing an integer $n$ ($1 \le n \le 2 \cdot 10^5$)Β β€” the size of the array $a$. Then follow $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$)Β β€” an array that needs to be sorted by the given operations. The given array can contain equal elements. The sum of $n$ for all test cases in one test does not exceed $2 \cdot 10^5$. -----Output----- For each test case output one integerΒ β€” the minimum total number of operations of the first and second type, which will make the array sorted in non-decreasing order. -----Example----- Input 9 5 4 7 2 2 9 5 3 5 8 1 7 5 1 2 2 4 5 2 0 1 3 0 1 0 4 0 1 0 0 4 0 1 0 1 4 0 1 0 2 20 16 15 1 10 0 14 0 10 3 9 2 5 4 5 17 9 10 20 0 9 Output 2 2 0 0 1 1 1 1 16 -----Note----- In the first test case, you first need to move two 2, to the beginning of the array. Therefore, the desired sequence of operations: $[4, 7, 2, 2, 9] \rightarrow [2, 4, 7, 2, 9] \rightarrow [2, 2, 4, 7, 9]$. In the second test case, you need to move the 1 to the beginning of the array, and the 8Β β€” to the end. Therefore, the desired sequence of operations: $[3, 5, 8, 1, 7] \rightarrow [1, 3, 5, 8, 7] \rightarrow [1, 3, 5, 7, 8]$. In the third test case, the array is already sorted.
import sys def minp(): return sys.stdin.readline().strip() def mint(): return int(minp()) def mints(): return map(int, minp().split()) def solve(): n = mint() b = [None] * n i = 0 for v in mints(): b[i] = v, i i += 1 b.sort() c = [0] * n ans = int(1000000000.0) s = None i = 0 while i < n: j = i + 1 v = b[i][0] while j < n and b[j][0] == v: j += 1 if s is None: for z in range(i, j): c[z] = j - z - 1 ans = n - j else: k = s while k < e and b[k][1] < b[i][1]: k += 1 for z in range(i, j - 1): c[z] = j - z - 1 + e if k == e: c[j - 1] = c[e - 1] else: c[j - 1] = s + e - k v = n - i - j for z in range(i, j): while k < e and b[k][1] < b[z][1]: k += 1 ans = min(ans, z + (e if k == s else c[k - 1]) + v) s, e, i = i, j, j print(ans) for i in range(mint()): solve()
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR NONE FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
There are $n$ cities numbered from $1$ to $n$, and city $i$ has beauty $a_i$. A salesman wants to start at city $1$, visit every city exactly once, and return to city $1$. For all $i\ne j$, a flight from city $i$ to city $j$ costs $\max(c_i,a_j-a_i)$ dollars, where $c_i$ is the price floor enforced by city $i$. Note that there is no absolute value. Find the minimum total cost for the salesman to complete his trip. -----Input----- The first line contains a single integer $n$ ($2\le n\le 10^5$) β€” the number of cities. The $i$-th of the next $n$ lines contains two integers $a_i$, $c_i$ ($0\le a_i,c_i\le 10^9$) β€” the beauty and price floor of the $i$-th city. -----Output----- Output a single integer β€” the minimum total cost. -----Examples----- Input 3 1 9 2 1 4 1 Output 11 Input 6 4 2 8 4 3 0 2 3 7 1 0 1 Output 13 -----Note----- In the first test case, we can travel in order $1\to 3\to 2\to 1$. The flight $1\to 3$ costs $\max(c_1,a_3-a_1)=\max(9,4-1)=9$. The flight $3\to 2$ costs $\max(c_3, a_2-a_3)=\max(1,2-4)=1$. The flight $2\to 1$ costs $\max(c_2,a_1-a_2)=\max(1,1-2)=1$. The total cost is $11$, and we cannot do better.
n = int(input()) a = [[int(s) for s in input().split(" ")] for i in range(n)] a.sort() ans, c = a[0][1], sum(a[0]) for w in a[1:]: ans += w[1] + max(w[0] - c, 0) c = max(c, sum(w)) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ cities numbered from $1$ to $n$, and city $i$ has beauty $a_i$. A salesman wants to start at city $1$, visit every city exactly once, and return to city $1$. For all $i\ne j$, a flight from city $i$ to city $j$ costs $\max(c_i,a_j-a_i)$ dollars, where $c_i$ is the price floor enforced by city $i$. Note that there is no absolute value. Find the minimum total cost for the salesman to complete his trip. -----Input----- The first line contains a single integer $n$ ($2\le n\le 10^5$) β€” the number of cities. The $i$-th of the next $n$ lines contains two integers $a_i$, $c_i$ ($0\le a_i,c_i\le 10^9$) β€” the beauty and price floor of the $i$-th city. -----Output----- Output a single integer β€” the minimum total cost. -----Examples----- Input 3 1 9 2 1 4 1 Output 11 Input 6 4 2 8 4 3 0 2 3 7 1 0 1 Output 13 -----Note----- In the first test case, we can travel in order $1\to 3\to 2\to 1$. The flight $1\to 3$ costs $\max(c_1,a_3-a_1)=\max(9,4-1)=9$. The flight $3\to 2$ costs $\max(c_3, a_2-a_3)=\max(1,2-4)=1$. The flight $2\to 1$ costs $\max(c_2,a_1-a_2)=\max(1,1-2)=1$. The total cost is $11$, and we cannot do better.
import sys N = int(input()) lst = [] for n in range(N): l = list(map(int, sys.stdin.readline().split())) lst.append(l) lst.sort() Sum = lst[0][1] c = lst[0][0] + lst[0][1] for i in lst[1:]: Sum += i[1] if i[0] > c: Sum += i[0] - c c = max(c, i[0] + i[1]) print(Sum)
IMPORT 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 VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ cities numbered from $1$ to $n$, and city $i$ has beauty $a_i$. A salesman wants to start at city $1$, visit every city exactly once, and return to city $1$. For all $i\ne j$, a flight from city $i$ to city $j$ costs $\max(c_i,a_j-a_i)$ dollars, where $c_i$ is the price floor enforced by city $i$. Note that there is no absolute value. Find the minimum total cost for the salesman to complete his trip. -----Input----- The first line contains a single integer $n$ ($2\le n\le 10^5$) β€” the number of cities. The $i$-th of the next $n$ lines contains two integers $a_i$, $c_i$ ($0\le a_i,c_i\le 10^9$) β€” the beauty and price floor of the $i$-th city. -----Output----- Output a single integer β€” the minimum total cost. -----Examples----- Input 3 1 9 2 1 4 1 Output 11 Input 6 4 2 8 4 3 0 2 3 7 1 0 1 Output 13 -----Note----- In the first test case, we can travel in order $1\to 3\to 2\to 1$. The flight $1\to 3$ costs $\max(c_1,a_3-a_1)=\max(9,4-1)=9$. The flight $3\to 2$ costs $\max(c_3, a_2-a_3)=\max(1,2-4)=1$. The flight $2\to 1$ costs $\max(c_2,a_1-a_2)=\max(1,1-2)=1$. The total cost is $11$, and we cannot do better.
n = int(input()) cities = [] ans = 0 for i in range(n): a, c = map(int, input().split()) cities += [(a, c)] ans += c cities = sorted(cities, key=lambda city: city[0]) mx = cities[0][0] + cities[0][1] for i in range(1, n): ans += max(0, cities[i][0] - mx) mx = max(mx, cities[i][0] + cities[i][1]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR LIST VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ cities numbered from $1$ to $n$, and city $i$ has beauty $a_i$. A salesman wants to start at city $1$, visit every city exactly once, and return to city $1$. For all $i\ne j$, a flight from city $i$ to city $j$ costs $\max(c_i,a_j-a_i)$ dollars, where $c_i$ is the price floor enforced by city $i$. Note that there is no absolute value. Find the minimum total cost for the salesman to complete his trip. -----Input----- The first line contains a single integer $n$ ($2\le n\le 10^5$) β€” the number of cities. The $i$-th of the next $n$ lines contains two integers $a_i$, $c_i$ ($0\le a_i,c_i\le 10^9$) β€” the beauty and price floor of the $i$-th city. -----Output----- Output a single integer β€” the minimum total cost. -----Examples----- Input 3 1 9 2 1 4 1 Output 11 Input 6 4 2 8 4 3 0 2 3 7 1 0 1 Output 13 -----Note----- In the first test case, we can travel in order $1\to 3\to 2\to 1$. The flight $1\to 3$ costs $\max(c_1,a_3-a_1)=\max(9,4-1)=9$. The flight $3\to 2$ costs $\max(c_3, a_2-a_3)=\max(1,2-4)=1$. The flight $2\to 1$ costs $\max(c_2,a_1-a_2)=\max(1,1-2)=1$. The total cost is $11$, and we cannot do better.
import sys input = sys.stdin.readline n = int(input()) l = [] out = 0 for _ in range(n): a, c = map(int, input().split()) out += c l.append((a, c)) l.sort(key=lambda x: x[0]) curr_best = l[0][0] for a, c in l: if a > curr_best: out += a - curr_best curr_best = max(curr_best, a + c) print(out)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ cities numbered from $1$ to $n$, and city $i$ has beauty $a_i$. A salesman wants to start at city $1$, visit every city exactly once, and return to city $1$. For all $i\ne j$, a flight from city $i$ to city $j$ costs $\max(c_i,a_j-a_i)$ dollars, where $c_i$ is the price floor enforced by city $i$. Note that there is no absolute value. Find the minimum total cost for the salesman to complete his trip. -----Input----- The first line contains a single integer $n$ ($2\le n\le 10^5$) β€” the number of cities. The $i$-th of the next $n$ lines contains two integers $a_i$, $c_i$ ($0\le a_i,c_i\le 10^9$) β€” the beauty and price floor of the $i$-th city. -----Output----- Output a single integer β€” the minimum total cost. -----Examples----- Input 3 1 9 2 1 4 1 Output 11 Input 6 4 2 8 4 3 0 2 3 7 1 0 1 Output 13 -----Note----- In the first test case, we can travel in order $1\to 3\to 2\to 1$. The flight $1\to 3$ costs $\max(c_1,a_3-a_1)=\max(9,4-1)=9$. The flight $3\to 2$ costs $\max(c_3, a_2-a_3)=\max(1,2-4)=1$. The flight $2\to 1$ costs $\max(c_2,a_1-a_2)=\max(1,1-2)=1$. The total cost is $11$, and we cannot do better.
from sys import stdin, stdout def travelling_salesman(n, ac_a): ac_a.sort() pre = 0 lmax = ac_a[0][0] + ac_a[0][1] r = ac_a[0][1] for i in range(1, n): if lmax < ac_a[i][0] + ac_a[i][1]: pre = i r += max(0, ac_a[i][0] - lmax) lmax = ac_a[i][0] + ac_a[i][1] r += ac_a[i][1] if pre != n - 1: r += max(0, ac_a[n - 1][0] - lmax) return r n = int(stdin.readline()) ac_a = [] for _ in range(n): a, c = map(int, stdin.readline().split()) ac_a.append([a, c]) r = travelling_salesman(n, ac_a) stdout.write(str(r) + "\n")
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
There are $n$ cities numbered from $1$ to $n$, and city $i$ has beauty $a_i$. A salesman wants to start at city $1$, visit every city exactly once, and return to city $1$. For all $i\ne j$, a flight from city $i$ to city $j$ costs $\max(c_i,a_j-a_i)$ dollars, where $c_i$ is the price floor enforced by city $i$. Note that there is no absolute value. Find the minimum total cost for the salesman to complete his trip. -----Input----- The first line contains a single integer $n$ ($2\le n\le 10^5$) β€” the number of cities. The $i$-th of the next $n$ lines contains two integers $a_i$, $c_i$ ($0\le a_i,c_i\le 10^9$) β€” the beauty and price floor of the $i$-th city. -----Output----- Output a single integer β€” the minimum total cost. -----Examples----- Input 3 1 9 2 1 4 1 Output 11 Input 6 4 2 8 4 3 0 2 3 7 1 0 1 Output 13 -----Note----- In the first test case, we can travel in order $1\to 3\to 2\to 1$. The flight $1\to 3$ costs $\max(c_1,a_3-a_1)=\max(9,4-1)=9$. The flight $3\to 2$ costs $\max(c_3, a_2-a_3)=\max(1,2-4)=1$. The flight $2\to 1$ costs $\max(c_2,a_1-a_2)=\max(1,1-2)=1$. The total cost is $11$, and we cannot do better.
n = int(input()) price = [] total = 0 for i in range(n): a, c = map(int, input().split()) price.append((a, a + c)) total = total + c price.sort() current = price[0][0] for i, j in price: total = total + max(0, i - current) current = max(current, j) print(total)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ cities numbered from $1$ to $n$, and city $i$ has beauty $a_i$. A salesman wants to start at city $1$, visit every city exactly once, and return to city $1$. For all $i\ne j$, a flight from city $i$ to city $j$ costs $\max(c_i,a_j-a_i)$ dollars, where $c_i$ is the price floor enforced by city $i$. Note that there is no absolute value. Find the minimum total cost for the salesman to complete his trip. -----Input----- The first line contains a single integer $n$ ($2\le n\le 10^5$) β€” the number of cities. The $i$-th of the next $n$ lines contains two integers $a_i$, $c_i$ ($0\le a_i,c_i\le 10^9$) β€” the beauty and price floor of the $i$-th city. -----Output----- Output a single integer β€” the minimum total cost. -----Examples----- Input 3 1 9 2 1 4 1 Output 11 Input 6 4 2 8 4 3 0 2 3 7 1 0 1 Output 13 -----Note----- In the first test case, we can travel in order $1\to 3\to 2\to 1$. The flight $1\to 3$ costs $\max(c_1,a_3-a_1)=\max(9,4-1)=9$. The flight $3\to 2$ costs $\max(c_3, a_2-a_3)=\max(1,2-4)=1$. The flight $2\to 1$ costs $\max(c_2,a_1-a_2)=\max(1,1-2)=1$. The total cost is $11$, and we cannot do better.
import sys input = sys.stdin.readline n = int(input()) l = [] ans = 0 for i in range(n): d = input().split() l.append((int(d[0]), int(d[1]))) ans += int(d[1]) l.sort() maxa = l[0][0] + l[0][1] for i in range(1, n): if l[i][0] > maxa: ans += l[i][0] - maxa maxa = max(maxa, l[i][0] + l[i][1]) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
There are $n$ cities numbered from $1$ to $n$, and city $i$ has beauty $a_i$. A salesman wants to start at city $1$, visit every city exactly once, and return to city $1$. For all $i\ne j$, a flight from city $i$ to city $j$ costs $\max(c_i,a_j-a_i)$ dollars, where $c_i$ is the price floor enforced by city $i$. Note that there is no absolute value. Find the minimum total cost for the salesman to complete his trip. -----Input----- The first line contains a single integer $n$ ($2\le n\le 10^5$) β€” the number of cities. The $i$-th of the next $n$ lines contains two integers $a_i$, $c_i$ ($0\le a_i,c_i\le 10^9$) β€” the beauty and price floor of the $i$-th city. -----Output----- Output a single integer β€” the minimum total cost. -----Examples----- Input 3 1 9 2 1 4 1 Output 11 Input 6 4 2 8 4 3 0 2 3 7 1 0 1 Output 13 -----Note----- In the first test case, we can travel in order $1\to 3\to 2\to 1$. The flight $1\to 3$ costs $\max(c_1,a_3-a_1)=\max(9,4-1)=9$. The flight $3\to 2$ costs $\max(c_3, a_2-a_3)=\max(1,2-4)=1$. The flight $2\to 1$ costs $\max(c_2,a_1-a_2)=\max(1,1-2)=1$. The total cost is $11$, and we cannot do better.
import sys sys.setrecursionlimit(10**5) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.buffer.readline()) def LI(): return list(map(int, sys.stdin.buffer.readline().split())) def LI1(): return list(map(int1, sys.stdin.buffer.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def BI(): return sys.stdin.buffer.readline().rstrip() def SI(): return sys.stdin.buffer.readline().rstrip().decode() dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] inf = 10**16 md = 10**9 + 7 n = II() ac = LLI(n) ac.sort(key=lambda x: x[0]) now = ac[0][0] ans = 0 for a, c in ac: ans += c ans += max(0, a - now) now = max(now, a + c) print(ans)
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING 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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ cities numbered from $1$ to $n$, and city $i$ has beauty $a_i$. A salesman wants to start at city $1$, visit every city exactly once, and return to city $1$. For all $i\ne j$, a flight from city $i$ to city $j$ costs $\max(c_i,a_j-a_i)$ dollars, where $c_i$ is the price floor enforced by city $i$. Note that there is no absolute value. Find the minimum total cost for the salesman to complete his trip. -----Input----- The first line contains a single integer $n$ ($2\le n\le 10^5$) β€” the number of cities. The $i$-th of the next $n$ lines contains two integers $a_i$, $c_i$ ($0\le a_i,c_i\le 10^9$) β€” the beauty and price floor of the $i$-th city. -----Output----- Output a single integer β€” the minimum total cost. -----Examples----- Input 3 1 9 2 1 4 1 Output 11 Input 6 4 2 8 4 3 0 2 3 7 1 0 1 Output 13 -----Note----- In the first test case, we can travel in order $1\to 3\to 2\to 1$. The flight $1\to 3$ costs $\max(c_1,a_3-a_1)=\max(9,4-1)=9$. The flight $3\to 2$ costs $\max(c_3, a_2-a_3)=\max(1,2-4)=1$. The flight $2\to 1$ costs $\max(c_2,a_1-a_2)=\max(1,1-2)=1$. The total cost is $11$, and we cannot do better.
import sys input = sys.stdin.readline n = int(input()) cit = [] ccost = 0 for i in range(n): a, c = map(int, input().split()) cit.append([a, c]) ccost += c acost = 0 cit.sort() mx = cit[0][0] + cit[0][1] for c in cit: if c[0] > mx: acost += c[0] - mx mx = max(mx, sum(c)) print(acost + ccost)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
There are $n$ cities numbered from $1$ to $n$, and city $i$ has beauty $a_i$. A salesman wants to start at city $1$, visit every city exactly once, and return to city $1$. For all $i\ne j$, a flight from city $i$ to city $j$ costs $\max(c_i,a_j-a_i)$ dollars, where $c_i$ is the price floor enforced by city $i$. Note that there is no absolute value. Find the minimum total cost for the salesman to complete his trip. -----Input----- The first line contains a single integer $n$ ($2\le n\le 10^5$) β€” the number of cities. The $i$-th of the next $n$ lines contains two integers $a_i$, $c_i$ ($0\le a_i,c_i\le 10^9$) β€” the beauty and price floor of the $i$-th city. -----Output----- Output a single integer β€” the minimum total cost. -----Examples----- Input 3 1 9 2 1 4 1 Output 11 Input 6 4 2 8 4 3 0 2 3 7 1 0 1 Output 13 -----Note----- In the first test case, we can travel in order $1\to 3\to 2\to 1$. The flight $1\to 3$ costs $\max(c_1,a_3-a_1)=\max(9,4-1)=9$. The flight $3\to 2$ costs $\max(c_3, a_2-a_3)=\max(1,2-4)=1$. The flight $2\to 1$ costs $\max(c_2,a_1-a_2)=\max(1,1-2)=1$. The total cost is $11$, and we cannot do better.
import sys mod = 1000000007 eps = 10**-9 def main(): import sys input = sys.stdin.buffer.readline N = int(input()) AC = [] ans = 0 for i in range(N): a, c = map(int, input().split()) AC.append((a, c, i)) ans += c AC.sort(key=lambda x: x[1]) AC.sort(key=lambda x: x[0]) b = AC[0][0] + AC[0][1] for i in range(1, N): a, c, j = AC[i] if a <= b: b = max(b, a + c) else: ans += a - b b = a + c print(ans) main()
IMPORT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
There are $n$ cities numbered from $1$ to $n$, and city $i$ has beauty $a_i$. A salesman wants to start at city $1$, visit every city exactly once, and return to city $1$. For all $i\ne j$, a flight from city $i$ to city $j$ costs $\max(c_i,a_j-a_i)$ dollars, where $c_i$ is the price floor enforced by city $i$. Note that there is no absolute value. Find the minimum total cost for the salesman to complete his trip. -----Input----- The first line contains a single integer $n$ ($2\le n\le 10^5$) β€” the number of cities. The $i$-th of the next $n$ lines contains two integers $a_i$, $c_i$ ($0\le a_i,c_i\le 10^9$) β€” the beauty and price floor of the $i$-th city. -----Output----- Output a single integer β€” the minimum total cost. -----Examples----- Input 3 1 9 2 1 4 1 Output 11 Input 6 4 2 8 4 3 0 2 3 7 1 0 1 Output 13 -----Note----- In the first test case, we can travel in order $1\to 3\to 2\to 1$. The flight $1\to 3$ costs $\max(c_1,a_3-a_1)=\max(9,4-1)=9$. The flight $3\to 2$ costs $\max(c_3, a_2-a_3)=\max(1,2-4)=1$. The flight $2\to 1$ costs $\max(c_2,a_1-a_2)=\max(1,1-2)=1$. The total cost is $11$, and we cannot do better.
import sys input = sys.stdin.readline n = int(input()) keyframes = [] ans = 0 for i in range(n): a, c = map(int, input().split()) ans += c keyframes.append((a, -1)) keyframes.append((a + c, 0)) keyframes.sort() count = 0 a, b = keyframes[0] prev = a for pos, t in keyframes: if t == -1: if count == 0: ans += pos - prev count += 1 prev = pos else: count -= 1 prev = pos print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR VAR IF VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ cities numbered from $1$ to $n$, and city $i$ has beauty $a_i$. A salesman wants to start at city $1$, visit every city exactly once, and return to city $1$. For all $i\ne j$, a flight from city $i$ to city $j$ costs $\max(c_i,a_j-a_i)$ dollars, where $c_i$ is the price floor enforced by city $i$. Note that there is no absolute value. Find the minimum total cost for the salesman to complete his trip. -----Input----- The first line contains a single integer $n$ ($2\le n\le 10^5$) β€” the number of cities. The $i$-th of the next $n$ lines contains two integers $a_i$, $c_i$ ($0\le a_i,c_i\le 10^9$) β€” the beauty and price floor of the $i$-th city. -----Output----- Output a single integer β€” the minimum total cost. -----Examples----- Input 3 1 9 2 1 4 1 Output 11 Input 6 4 2 8 4 3 0 2 3 7 1 0 1 Output 13 -----Note----- In the first test case, we can travel in order $1\to 3\to 2\to 1$. The flight $1\to 3$ costs $\max(c_1,a_3-a_1)=\max(9,4-1)=9$. The flight $3\to 2$ costs $\max(c_3, a_2-a_3)=\max(1,2-4)=1$. The flight $2\to 1$ costs $\max(c_2,a_1-a_2)=\max(1,1-2)=1$. The total cost is $11$, and we cannot do better.
n = int(input()) a = sorted([*map(int, input().split())] for i in range(n)) c = sum(a[0]) t = a[0][1] for w in a[1:]: t += w[1] if w == a[-1] or sum(w) > c: t += max(w[0] - c, 0) c = sum(w) print(t)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ cities numbered from $1$ to $n$, and city $i$ has beauty $a_i$. A salesman wants to start at city $1$, visit every city exactly once, and return to city $1$. For all $i\ne j$, a flight from city $i$ to city $j$ costs $\max(c_i,a_j-a_i)$ dollars, where $c_i$ is the price floor enforced by city $i$. Note that there is no absolute value. Find the minimum total cost for the salesman to complete his trip. -----Input----- The first line contains a single integer $n$ ($2\le n\le 10^5$) β€” the number of cities. The $i$-th of the next $n$ lines contains two integers $a_i$, $c_i$ ($0\le a_i,c_i\le 10^9$) β€” the beauty and price floor of the $i$-th city. -----Output----- Output a single integer β€” the minimum total cost. -----Examples----- Input 3 1 9 2 1 4 1 Output 11 Input 6 4 2 8 4 3 0 2 3 7 1 0 1 Output 13 -----Note----- In the first test case, we can travel in order $1\to 3\to 2\to 1$. The flight $1\to 3$ costs $\max(c_1,a_3-a_1)=\max(9,4-1)=9$. The flight $3\to 2$ costs $\max(c_3, a_2-a_3)=\max(1,2-4)=1$. The flight $2\to 1$ costs $\max(c_2,a_1-a_2)=\max(1,1-2)=1$. The total cost is $11$, and we cannot do better.
n = int(input()) l = [list(map(int, input().split())) for _ in range(n)] l.sort() t = [l[0][0], l[0][1] + l[0][0]] ans = 0 for a, c in l: ans += c p = a q = a + c if t[-1] < p: t.append(p) t.append(q) else: t[-1] = max(t[-1], q) for i in range(1, len(t) - 1, 2): ans += t[i + 1] - t[i] print(ans)
ASSIGN VAR FUNC_CALL VAR 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 ASSIGN VAR LIST VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
There are $n$ cities numbered from $1$ to $n$, and city $i$ has beauty $a_i$. A salesman wants to start at city $1$, visit every city exactly once, and return to city $1$. For all $i\ne j$, a flight from city $i$ to city $j$ costs $\max(c_i,a_j-a_i)$ dollars, where $c_i$ is the price floor enforced by city $i$. Note that there is no absolute value. Find the minimum total cost for the salesman to complete his trip. -----Input----- The first line contains a single integer $n$ ($2\le n\le 10^5$) β€” the number of cities. The $i$-th of the next $n$ lines contains two integers $a_i$, $c_i$ ($0\le a_i,c_i\le 10^9$) β€” the beauty and price floor of the $i$-th city. -----Output----- Output a single integer β€” the minimum total cost. -----Examples----- Input 3 1 9 2 1 4 1 Output 11 Input 6 4 2 8 4 3 0 2 3 7 1 0 1 Output 13 -----Note----- In the first test case, we can travel in order $1\to 3\to 2\to 1$. The flight $1\to 3$ costs $\max(c_1,a_3-a_1)=\max(9,4-1)=9$. The flight $3\to 2$ costs $\max(c_3, a_2-a_3)=\max(1,2-4)=1$. The flight $2\to 1$ costs $\max(c_2,a_1-a_2)=\max(1,1-2)=1$. The total cost is $11$, and we cannot do better.
import sys from sys import stdin n = int(stdin.readline()) ans = 0 ac = [] LAC = [] RAC = [] for i in range(n): a, c = map(int, stdin.readline().split()) ans += c ac.append((a, c)) LAC.append((a, c)) LAC.sort() nans1 = 0 nmax = LAC[0][0] + LAC[0][1] for ac in LAC: a, c = ac if nmax < a: nans1 += a - nmax nmax = max(nmax, a + c) print(ans + nans1)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
There are $n$ cities numbered from $1$ to $n$, and city $i$ has beauty $a_i$. A salesman wants to start at city $1$, visit every city exactly once, and return to city $1$. For all $i\ne j$, a flight from city $i$ to city $j$ costs $\max(c_i,a_j-a_i)$ dollars, where $c_i$ is the price floor enforced by city $i$. Note that there is no absolute value. Find the minimum total cost for the salesman to complete his trip. -----Input----- The first line contains a single integer $n$ ($2\le n\le 10^5$) β€” the number of cities. The $i$-th of the next $n$ lines contains two integers $a_i$, $c_i$ ($0\le a_i,c_i\le 10^9$) β€” the beauty and price floor of the $i$-th city. -----Output----- Output a single integer β€” the minimum total cost. -----Examples----- Input 3 1 9 2 1 4 1 Output 11 Input 6 4 2 8 4 3 0 2 3 7 1 0 1 Output 13 -----Note----- In the first test case, we can travel in order $1\to 3\to 2\to 1$. The flight $1\to 3$ costs $\max(c_1,a_3-a_1)=\max(9,4-1)=9$. The flight $3\to 2$ costs $\max(c_3, a_2-a_3)=\max(1,2-4)=1$. The flight $2\to 1$ costs $\max(c_2,a_1-a_2)=\max(1,1-2)=1$. The total cost is $11$, and we cannot do better.
n = int(input()) inps = [] ans = 0 for i in range(n): a, c = map(int, input().split()) ans += c inps.append((a, c)) inps.sort() topcover = inps[0][0] for i in range(len(inps)): if inps[i][0] > topcover: ans += inps[i][0] - topcover topcover = max(topcover, inps[i][0] + inps[i][1]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR