description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You are given a string $s$. You have to reverse it β€” that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal β€” and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) β€” the length of $s$. The second line contains $s$ β€” a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
import sys input = sys.stdin.readline class bit: def __init__(self, n): self.n = n + 1 self.mx_p = 0 while 1 << self.mx_p < self.n: self.mx_p += 1 self.a = [0] * self.n self.tot = 0 def add(self, idx, val): self.tot += val idx += 1 while idx < self.n: self.a[idx] += val idx += idx & -idx def sum_prefix(self, idx): tot = 0 idx += 1 while idx > 0: tot += self.a[idx] idx -= idx & -idx return tot def sum(self, l, r): return self.sum_prefix(r) - self.sum_prefix(l - 1) def inversions(a): tot = 0 loc = [0] * len(a) for i in range(len(a)): loc[a[i]] = i bt = bit(len(a)) for i in range(len(a) - 1, -1, -1): tot += bt.sum_prefix(loc[i]) bt.add(loc[i], 1) return tot def create_c(a, b): c = [0] * len(a) idx = {} positions = {} for i in range(len(b)): if b[i] in positions: positions[b[i]].append(i) else: positions[b[i]] = [i] for i in range(len(a)): if a[i] in idx: idx[a[i]] += 1 else: idx[a[i]] = 0 c[i] = positions[a[i]][idx[a[i]]] return c n = int(input()) s = input().strip() s_reversed = s[::-1] print(inversions(create_c(s, s_reversed)))
IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FUNC_DEF VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given a string $s$. You have to reverse it β€” that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal β€” and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) β€” the length of $s$. The second line contains $s$ β€” a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
from sys import stdin input = stdin.readline global T, N def get_index(char): return ord(char) - 97 def read(index): global T, N s = 0 while index > 0: s += T[index] index -= index & -index return s def update(index, val): global T, N while index <= N: T[index] += val index += index & -index N = int(input()) S = input()[:N] A = [[] for _ in range(26)] for i in range(N): ind = get_index(S[i]) A[ind].append(i + 1) inds = [(-1) for _ in range(26)] T = [(0) for _ in range(N + 1)] ans = 0 for i in range(N - 1, -1, -1): idx = get_index(S[N - i - 1]) x = A[idx][inds[idx]] inds[idx] -= 1 ans += i + 1 - x - read(x) update(x + 1, -1) print(ans)
ASSIGN VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given a string $s$. You have to reverse it β€” that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal β€” and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) β€” the length of $s$. The second line contains $s$ β€” a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
maxn = int(200000.0 + 5) n = int(input()) s = input() t = s[::-1] a = [[] for i in range(31)] tree = [0] * maxn num = [0] * maxn g = [0] * 30 def lowbit(x): return x & -x def tadd(x, val): while x <= maxn: tree[x] += val x += lowbit(x) def tsum(x): ans = 0 while x > 0: ans += tree[x] x -= lowbit(x) return ans for i in range(0, len(t)): ch = ord(t[i]) - ord("a") a[ch].append(i) for i in range(0, len(s)): ch = ord(s[i]) - ord("a") num[i] = a[ch][g[ch]] g[ch] += 1 ans = 0 for i in range(0, len(s)): tadd(num[i] + 1, 1) ans += i + 1 - tsum(num[i] + 1) print(ans)
ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_DEF RETURN BIN_OP VAR VAR FUNC_DEF WHILE VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given a string $s$. You have to reverse it β€” that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal β€” and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) β€” the length of $s$. The second line contains $s$ β€” a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
n = int(input()) s = input() target = s[::-1] cnt = 0 mapping = {} for i in range(n): if s[i] in mapping: mapping[s[i]].append(i) else: mapping[s[i]] = [i] final = [-1] * n for i in range(n - 1, -1, -1): final[i] = mapping[target[i]].pop(-1) def combine(start1, end1, start2, end2): n1 = end1 - start1 + 1 n2 = end2 - start2 + 1 i = start1 j = start2 new = [] cnt = 0 while i <= end1 and j <= end2: if final[i] < final[j]: new.append(final[i]) i += 1 else: new.append(final[j]) cnt += end1 - i + 1 j += 1 while i <= end1: new.append(final[i]) i += 1 while j <= end2: new.append(final[j]) j += 1 k = start1 for i in new: final[k] = i k += 1 return cnt def count_inversions(start, end): if abs(start - end) <= 1: if final[start] <= final[end]: return 0 final[start], final[end] = final[end], final[start] return 1 mid = (start + end) // 2 num_left = count_inversions(start, mid) num_right = count_inversions(mid + 1, end) cnt = combine(start, mid, mid + 1, end) return num_left + num_right + cnt print(count_inversions(0, n - 1))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER
You are given a string $s$. You have to reverse it β€” that is, the first letter should become equal to the last letter before the reversal, the second letter should become equal to the second-to-last letter before the reversal β€” and so on. For example, if your goal is to reverse the string "abddea", you should get the string "aeddba". To accomplish your goal, you can swap the neighboring elements of the string. Your task is to calculate the minimum number of swaps you have to perform to reverse the given string. -----Input----- The first line contains one integer $n$ ($2 \le n \le 200\,000$) β€” the length of $s$. The second line contains $s$ β€” a string consisting of $n$ lowercase Latin letters. -----Output----- Print one integer β€” the minimum number of swaps of neighboring elements you have to perform to reverse the string. -----Examples----- Input 5 aaaza Output 2 Input 6 cbaabc Output 0 Input 9 icpcsguru Output 30 -----Note----- In the first example, you have to swap the third and the fourth elements, so the string becomes "aazaa". Then you have to swap the second and the third elements, so the string becomes "azaaa". So, it is possible to reverse the string in two swaps. Since the string in the second example is a palindrome, you don't have to do anything to reverse it.
n = int(input()) s = input() rev_s = s[::-1] mapping = dict() ct = 1 for i in rev_s: if i in mapping: mapping[i].append(ct) else: mapping[i] = [ct] ct += 1 output_arr = [] iptr = dict() for i in s: if i not in iptr: iptr[i] = -1 iptr[i] += 1 output_arr.append(mapping[i][iptr[i]]) def ct_inv(A): ans = 0 for i in range(len(A)): for j in range(i + 1, len(A)): if A[i] > A[j]: ans += 1 return ans fenw = [(0) for i in range(n + 2)] def update(x): idx = x while idx <= n + 1: fenw[idx] += 1 idx += idx & -idx def query(idx): ans = 0 while idx > 0: ans += fenw[idx] idx -= idx & -idx return ans def count_inversions(A): ans = 0 for i in A: ans += query(n) - query(i) update(i) return ans print(count_inversions(output_arr))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
for i in range(int(input())): n = int(input()) a = list(map(int, input().split())) f = 0 for j in range(1, n): f += max(0, a[j - 1] - a[j]) a[0] -= f f += abs(a[-1] - a[0]) + abs(a[0]) print(f)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
for test_case in range(int(input())): n = int(input()) l = list(map(int, input().split())) ans = 0 d = 0 for i in range(n - 1): x = l[i] - l[i + 1] if x > 0: ans += x else: ans += -x d += -x d = l[-1] - d if d < 0: d = -1 * d print(ans + d)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) ans = 0 r = 0 for i in range(n - 1): ans += abs(l[i + 1] - l[i]) if l[i + 1] > l[i]: r += l[i + 1] - l[i] print(ans + abs(l[n - 1] - r))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) maxi = a[0] ops = 0 for i in range(1, n): a[i] -= ops if a[i] <= maxi: maxi = a[i] else: ops += a[i] - maxi a[i] = maxi maxi = a[-1] ans = ops ops = 0 for i in range(n - 2, -1, -1): a[i] -= ops if a[i] > maxi: ops += a[i] - maxi a[i] = maxi ans += ops ans += abs(a[0]) 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 VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
def help(A, n): curr = A[0] ops = 0 change = 0 for i in range(1, n): A[i] = A[i] - change if A[i] <= curr: ops += curr - A[i] curr = A[i] continue else: change += A[i] - curr A[i] = curr continue ops += abs(curr) ops += change return ops t = int(input()) for _ in range(t): n = int(input()) A = list(map(int, input().split())) print(help(A, n))
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
import sys t = int(sys.stdin.readline()) for _ in range(t): n = int(sys.stdin.readline()) nums = list(map(int, sys.stdin.readline().strip().split())) d = [0] * n for i in range(1, n): d[i] = nums[i] - nums[i - 1] res = 0 d[0] = nums[0] for i in range(1, n): if d[i] > 0: res += d[i] else: res -= d[i] d[0] += d[i] print(res + abs(d[0]))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
for _ in range(int(input())): n = int(input()) arr1 = list(map(int, input().split())) arr2 = [] for a in arr1[::-1]: arr2.append(a) def func(arr): left = arr[0] sub = 0 ans = 0 for a in arr: val = a + sub if val > left: sub -= abs(val - left) ans += abs(val - left) elif val < left: ans += abs(left - val) left = val ans += abs(left) return ans print(min(func(arr1), func(arr2)))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
for _ in range(int(input())): n = int(input()) l = list(map(int, input().split(" "))) k = 0 for i in range(1, n): z = l[i] - l[i - 1] if z >= 0: k += z m = l[-1] - k mcd = abs(l[0] - m) ans = k + abs(m) + mcd 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 STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
T = int(input()) q = [] for i in range(T): n = int(input()) data = [int(i) for i in input().split()] q.append((n, data)) def solve(n, data): ans = 0 for i in range(1, n): if data[i - 1] > data[i]: ans += data[i - 1] - data[i] data[0] -= data[i - 1] - data[i] ans += abs(data[0]) ans += data[n - 1] - data[0] print(ans) for n, data in q: solve(n, data)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
for _ in range(int(input())): n = int(input()) nums = list(map(int, input().split())) nums = [0] + nums d = [0] * (n + 1) for i in range(1, n + 1): if i == 1: d[i] = nums[i] else: d[i] = nums[i] - nums[i - 1] cnt = 0 for i in range(2, n + 1): if d[i] > 0: cnt += d[i] elif d[i] < 0: cnt += -d[i] d[1] += d[i] if d[1] < 0: cnt += -d[1] elif d[1] > 0: cnt += d[1] print(cnt)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER NUMBER VAR VAR NUMBER IF VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
def func(n, a): b = [(0) for i in range(n)] ans = 0 b[-1] = a[-1] for i in range(n - 1, 0, -1): if a[i - 1] > a[i]: ans += a[i - 1] - a[i] b[i - 1] = b[i] else: b[i - 1] = a[i - 1] - ans ans += abs(b[0]) for i in range(n - 1): if b[i] != b[i + 1]: ans += b[i + 1] - b[i] return ans for i in range(int(input())): n = int(input()) a = list(map(int, input().split())) print(func(n, a))
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) if n == 1: print(abs(l[0])) continue ans = 0 x = 0 t = l[0] for i in range(n - 1): c = x + l[i + 1] ans += abs(l[i + 1] - l[i]) if l[i] <= l[i + 1]: x -= l[i + 1] - l[i] else: t = min(t, l[i + 1] + x) ans += abs(t) 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 IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
t = int(input()) for i in range(t): n = int(input()) s = input() s = s.split() a = [int(x) for x in s] out = 0 correction = a[0] for i in range(1, n): diff = a[i] - a[i - 1] if diff < 0: out -= diff correction += diff elif diff > 0: out += diff out += abs(correction) print(out)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
for _t in range(int(input())): n = int(input()) a = list(map(int, input().split())) ans = 0 res = 0 for i in range(1, n): a[i] += res ans += abs(a[i] - a[i - 1]) if a[i - 1] <= a[i]: res += a[i - 1] - a[i] a[i] = a[i - 1] else: a[i - 1] = a[i] print(ans + abs(a[-1]))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
def main(): n = int(input()) a = list(map(int, input().split())) p = 0 ans = 0 for i in range(n - 1): if a[i] < a[i + 1]: d = a[i + 1] - a[i] p += d ans += d else: d = a[i] - a[i + 1] ans += d v = a[-1] - p ans += abs(v) print(ans) for _ in range(int(input())): main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
import sys def solution(arr_): diff = [arr_[0] for i in range(len(arr_))] for i in range(1, len(arr_)): diff[i] = arr_[i] - arr_[i - 1] res = 0 for n in diff[1:]: if n < 0: res -= n diff[0] += n else: res += n res += abs(diff[0]) return res case_num = int(sys.stdin.readline().strip()) for case_idx in range(case_num): sys.stdin.readline().strip() arr_ = list(map(int, sys.stdin.readline().strip().split())) print(solution(arr_))
IMPORT FUNC_DEF ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
for i in range(int(input())): n = int(input()) ls = list(map(int, input().split())) ans = am = 0 for x in range(n - 1): cnt = ls[x] - ls[x + 1] if cnt > 0: ans += cnt else: ans -= cnt am -= cnt print(ans + abs(ls[-1] - am))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
def saveTrees(): n = int(input()) arr = list(map(int, input().split())) ans = 0 first = arr[0] for i in range(n - 1): ans += abs(arr[i] - arr[i + 1]) if arr[i] > arr[i + 1]: first += arr[i + 1] - arr[i] print(ans + abs(first)) t = int(input()) for i in range(t): saveTrees()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
def solve(): n = int(input()) a = [int(x) for x in input().split()] ops = 0 maxi = a[0] for i in range(1, n): a[i] -= ops if a[i] <= maxi: maxi = a[i] else: ops += a[i] - maxi a[i] = maxi maxi = a[n - 1] ans = ops ops = 0 for i in range(n - 2, -1, -1): a[i] -= ops if a[i] > maxi: ops += a[i] - maxi a[i] = maxi ans += ops ans += abs(a[0]) print(ans) for _ in range(int(input())): solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
for i in range(int(input())): lenn = int(input()) list1 = list(map(int, input().split())) ans = 0 minn = min(list1) imp = list1[0] imp1 = list1[-1] for i in range(lenn - 1): if list1[i] > list1[i + 1]: ans += list1[i] - list1[i + 1] imp -= list1[i] - list1[i + 1] if imp < 0: ans += abs(imp) imp1 += abs(imp) ans += imp1 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 NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
x = int(input()) def cal(n, p): if n == 1: return abs(p[0]) ans = 0 carry = 0 for i in range(1, n): if p[i] < p[i - 1]: ans += p[i - 1] - p[i] else: ans += p[i] - p[i - 1] carry += p[i] - p[i - 1] return ans + abs(carry - p[-1]) for jj in range(x): n = int(input()) p = [int(i) for i in input().split(" ")] print(cal(n, p))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) sum = 0 mini = arr[0] extra = 0 for i in range(n - 1): mini = min(mini, arr[i + 1] - extra) if arr[i] >= arr[i + 1]: sum += arr[i] - arr[i + 1] else: sum += arr[i + 1] - arr[i] extra += arr[i + 1] - arr[i] print(int(sum + abs(mini)))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN 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 IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
import sys input = sys.stdin.readline def solve(): n = int(input()) arr = list(map(int, input().split())) curr = arr[0] ans = 0 for i in range(n - 1): if curr <= arr[i + 1]: curr = arr[i + 1] else: ans += curr - arr[i + 1] curr = arr[i + 1] f = arr[0] - ans b = arr[-1] if f < 0: ans += abs(f) b += abs(f) return b + ans for _ in range(int(input())): print(solve())
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
for line in [*open(0)][2::2]: tot = 0 list = line.split(" ") list2 = [0] + list minv = int(list[0]) for val in range(len(list) - 1): diff = int(list2[val + 1]) - int(list[val + 1]) if diff >= 0: tot += diff minv -= diff print(tot - minv + abs(minv) + int(list[len(list) - 1]))
FOR VAR LIST FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
def solve(): n = int(input()) lis = list(map(int, input().split())) ans = 0 diff = 0 ref, diff = lis[0], lis[0] for i in range(1, n): diff = lis[i] - lis[i - 1] ans += abs(diff) if diff < 0: ref += diff print(ans + abs(ref)) for _ in range(int(input())): solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
import sys for _ in range(int(input())): n = int(input()) arr = list(map(int, sys.stdin.readline().split())) sub = 0 last = arr[-1] res = 0 for i in range(n - 2, -1, -1): if arr[i] - sub > last: diff = last - (arr[i] - sub) res += abs(diff) sub += abs(diff) else: last = arr[i] - sub arr[i] = last if arr[0] < 0: res += abs(arr[0]) arr[-1] += abs(arr[0]) print(res + arr[-1])
IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
t = int(input()) while t: n = int(input()) a = [0] + list(map(int, input().split(" "))) + [0] b = [0] * (n + 2) for i in range(1, n + 1): b[i] = a[i] - a[i - 1] b[n + 1] = -a[n] x, y = 0, 0 for i in range(2, n + 1): if b[i] > 0: x += b[i] else: y -= b[i] b[1] -= x b[-1] += y ans = x + y + abs(b[1] - b[n + 1]) // 2 print(ans) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
for time in range(int(input())): n = int(input()) nums = [int(a) for a in input().split()] s = 0 k = nums[0] for j in range(n - 1): l = abs(nums[j] - nums[j + 1]) s += l if nums[j] > nums[j + 1]: k -= l print(s + abs(k))
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 NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
def solve(): n = int(input()) a = [int(x) for x in input().split()] init_sum = sum(a) ans = 0 diff = 0 for i in range(1, n): if a[i] > a[i - 1]: d = a[i] - a[i - 1] ans += d diff += (n - i) * d else: d = a[i - 1] - a[i] ans += d diff += i * d final_sum = init_sum - diff fin = final_sum // n ans += fin if fin >= 0 else -fin print(ans) for _ in range(int(input())): solve()
FUNC_DEF 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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
for _ in range(int(input())): n = int(input()) l = [0] + list(map(int, input().split())) l1 = [] for i in range(n): l1.append(l[i + 1] - l[i]) ans = 0 for i in range(1, n): if l1[i] > 0: ans += l1[i] else: l1[0] += l1[i] ans -= l1[i] print(ans + abs(l1[0]))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER 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 BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
for q in range(int(input())): n = int(input()) arr = [int(x) for x in input().split()] lis = arr.copy() minus = 0 ans = 0 for i in range(1, n): lis[i] = lis[i] - minus if lis[i] > lis[i - 1]: ans += lis[i] - lis[i - 1] minus += lis[i] - lis[i - 1] lis[i] = lis[i - 1] for i in range(n - 1): ans += lis[i] - lis[i + 1] ans += abs(lis[-1]) 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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
def help_the_trees(l: list): result = 0 temp = 0 last = l[-1] for index in range(len(l) - 1): if l[index] > l[index + 1]: dif = l[index] - l[index + 1] result += dif elif l[index] < l[index + 1]: dif = l[index + 1] - l[index] result += dif temp += dif return result + abs(last - temp) n = int(input()) for i in range(n): garbale = input() l = list(map(int, input().split())) print(help_the_trees(l))
FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
def testcase(): n = int(input()) a = list(map(int, input().split())) assert n == len(a) moves = 0 offset = 0 base = a[0] for i in range(n): h = offset + a[i] if h == base: continue if h < base: moves += base - h base = h else: offset -= h - base moves += h - base print(moves + abs(base)) t = int(input()) for i in range(t): testcase()
FUNC_DEF 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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
for p in range(int(input())): n = int(input()) a = list(map(int, input().split())) minv = 10**10 oper = 0 t = 0 curv = a[0] for i in range(1, n): if a[i] - t <= curv: oper += curv - (a[i] - t) curv = a[i] - t else: oper += a[i] - t - curv t = t + (a[i] - t - curv) print(abs(curv) + oper)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
t = int(input()) for i in range(0, t): totalsteps = 0 trees = int(input()) value = input() value = value.split(" ") value = [int(k) for k in value] finaldistance = value[trees - 1] for j in range(0, trees - 1): steps = 0 if value[j] == value[j + 1]: continue elif value[j] < value[j + 1]: steps = value[j + 1] - value[j] finaldistance -= steps elif value[j] > value[j + 1]: steps = value[j] - value[j + 1] totalsteps += steps totalsteps += abs(finaldistance) print(totalsteps)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) pos, neg = 0, 0 ans = 0 positive, negative = [0] * n, [0] * n for i in range(1, n): if arr[i] > arr[i - 1]: positive[i] += arr[i] - arr[i - 1] else: negative[i - 1] += arr[i - 1] - arr[i] neg += negative[i - 1] for i in range(len(arr)): ans += positive[i] + negative[i] pos += positive[i] arr[i] -= pos + neg neg -= negative[i] print(ans + abs(arr[0]))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
for _ in range(int(input())): s = int(input()) z = list(map(int, input().split())) total = 0 right = 0 for i in range(1, len(z)): z[i] -= right if z[i] <= z[i - 1]: total += abs(z[i] - z[i - 1]) else: total += abs(z[i] - z[i - 1]) right += abs(z[i] - z[i - 1]) z[i] = z[i - 1] print(total + abs(z[-1]))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
t = int(input()) for i in range(t): l = int(input()) arr = [int(i) for i in input().split(" ")] ans = 0 rem = arr[0] for i in range(l - 1): ans += abs(arr[i + 1] - arr[i]) if arr[i + 1] < arr[i]: rem += arr[i + 1] - arr[i] ans += abs(rem) 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 STRING ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) ans = 0 b = 0 for i in range(n - 1): l[i + 1] -= b if l[i] > l[i + 1]: temp = l[i] - l[i + 1] ans += temp l[i] -= temp else: temp = l[i + 1] - l[i] b += temp ans += temp l[i + 1] -= temp print(ans + abs(l[-1]))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
t = int(input()) for i in range(t): nt = int(input()) lst = list(map(int, input().split())) act = 0 for i in range(nt - 1): act += abs(lst[i] - lst[i + 1]) if lst[i + 1] - lst[i] < 0: lst[0] -= abs(lst[i + 1] - lst[i]) act += abs(lst[0]) print(act)
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 NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
import sys def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def SI(): return sys.stdin.readline().rstrip() def MIN_INT(): return -1 * sys.maxsize def MAX_INT(): return sys.maxsize def prefSum(arr, n): P = [0] * (n + 1) for k in range(1, n + 1): P[k] = P[k - 1] + arr[k - 1] return P def GCD(x, y): while y: x, y = y, x % y return abs(x) def solve(): n = II() a = LI() prefix = a[0] suffix = 0 ans = 0 for i in range(1, n): a[i] -= suffix if a[i] <= prefix: ans += prefix - a[i] prefix = a[i] else: suffix += a[i] - prefix ans += a[i] - prefix ans += abs(prefix) return ans def main(): t = II() for _ in range(t): print(solve()) main()
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP NUMBER VAR FUNC_DEF RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF WHILE VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
T = int(input()) for Ti in range(T): n = int(input().strip()) l = [int(i) for i in input().split(" ")] sum = 0 ans = 0 for i in range(1, len(l)): dif = l[i] - l[i - 1] ans += abs(dif) if dif < 0: sum += dif ans += abs(l[0] + sum) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
t = int(input()) while t > 0: n = int(input()) moves = 0 lst = list(map(int, input().strip().split())) for i in range(len(lst) - 1): moves = moves + abs(lst[i + 1] - lst[i]) if lst[i] > lst[i + 1]: lst[0] = lst[0] + lst[i + 1] - lst[i] moves = moves + abs(lst[0]) t = t - 1 print(moves)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE 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 FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
import sys input = sys.stdin.readline def inp(): return int(input()) def inlt(): return list(map(int, input().split())) def insr(): s = input() return list(s[: len(s) - 1]) def invr(): return map(int, input().split()) def solution(n, a): diff = [] for i in range(n - 1): diff.append(a[i + 1] - a[i]) left_dec = 0 right_dec = 0 for d in diff: if d > 0: right_dec += d elif d < 0: left_dec += -d level_posn = a[0] - left_dec return left_dec + right_dec + abs(level_posn) tests = inp() for test in range(tests): n = inp() a = inlt() print(solution(n, a))
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
import sys def solve(): inp = sys.stdin.readline n = int(inp()) a = list(map(int, inp().split())) d = [0] * n c = 0 d[0] = a[0] for i in range(1, n): d[i] = a[i] - a[i - 1] if d[i] < 0: c -= d[i] d[0] += d[i] else: c += d[i] print(c + abs(d[0])) def main(): for i in range(int(sys.stdin.readline())): solve() main()
IMPORT FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) comp = a[0] curr = a[0] tot = 0 for a_i in a[1:]: diff = a_i - curr tot += abs(diff) if diff < 0: comp += diff curr = a_i tot += abs(comp) 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 VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
def ck(n, lis): dv = 0 la = lis[0] val = 0 for i in range(1, n): ke = lis[i] + dv if la > ke: val += abs(ke - la) la = ke else: dv += la - ke val += abs(la - ke) return abs(la) + val for _ in range(int(input())): n = int(input()) lis = list(map(int, input().split())) if n == 1: print(abs(lis[0])) else: print(ck(n, lis))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
t = int(input()) while t: t -= 1 n = int(input()) l = [int(x) for x in input().split()] diff = [(l[i] - l[i + 1]) for i in range(n - 1)] ans = 0 for d in diff: ans += abs(d) tmp = l[0] for d in diff: if d > 0: tmp -= d ans += abs(tmp) print(ans)
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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
t = int(input()) for i in range(t): n = int(input()) substracted = 0 a = list(map(int, input().split())) diff = [(a[i] - a[i + 1]) for i in range(n - 1)] substracted = sum([i for i in diff if i < 0]) steps = sum([abs(i) for i in diff]) steps += abs(a[n - 1] + substracted) print(steps)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR 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 BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
x = int(input()) for testCases in range(x): n = int(input()) answer = 0 array = input().split(" ") for i in range(len(array)): array[i] = int(array[i]) currH = array[0] for i in range(len(array) - 1): answer += abs(array[i] - array[i + 1]) if array[i] > array[i + 1]: currH += array[i + 1] - array[i] print(answer + abs(currH))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
Little Leon lives in the forest. He has recently noticed that some trees near his favourite path are withering, while the other ones are overhydrated so he decided to learn how to control the level of the soil moisture to save the trees. There are $n$ trees growing near the path, the current levels of moisture of each tree are denoted by the array $a_1, a_2, \dots, a_n$. Leon has learned three abilities which will help him to dry and water the soil. Choose a position $i$ and decrease the level of moisture of the trees $1, 2, \dots, i$ by $1$. Choose a position $i$ and decrease the level of moisture of the trees $i, i + 1, \dots, n$ by $1$. Increase the level of moisture of all trees by $1$. Leon wants to know the minimum number of actions he needs to perform to make the moisture of each tree equal to $0$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) β€” the number of test cases. The description of $t$ test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 200000$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) β€” the initial levels of trees moisture. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $200000$. -----Output----- For each test case output a single integer β€” the minimum number of actions. It can be shown that the answer exists. -----Examples----- Input 4 3 -2 -2 -2 3 10 4 7 4 4 -4 4 -4 5 1 -2 3 -4 5 Output 2 13 36 33 -----Note----- In the first test case it's enough to apply the operation of adding $1$ to the whole array $2$ times. In the second test case you can apply the operation of decreasing $4$ times on the prefix of length $3$ and get an array $6, 0, 3$. After that apply the operation of decreasing $6$ times on the prefix of length $1$ and $3$ times on the suffix of length $1$. In total, the number of actions will be $4 + 6 + 3 = 13$. It can be shown that it's impossible to perform less actions to get the required array, so the answer is $13$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) if n == 1: print(abs(a[0])) continue p = 0 for i in range(n - 1): if a[i] > a[i + 1]: p += a[i] - a[i + 1] if a[0] - p < 0: a[-1] += p - a[0] p += p - a[0] p += a[-1] print(p)
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 IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
t = int(input()) for _ in range(t): n = int(input()) A = [] for i in range(n): l, r = list(map(int, input().split())) A.append((l, 0)) A.append((r, 1)) A = sorted(A) minimumIntervals = float("inf") currentIntervals = 0 lLeft = n foundR = False for i, (l, r) in enumerate(A[:-1]): if r == 0: currentIntervals += 1 lLeft -= 1 else: foundR = True currentIntervals -= 1 if not foundR: continue if lLeft == 0: continue if currentIntervals < minimumIntervals: minimumIntervals = currentIntervals if minimumIntervals == float("inf"): print(-1) else: print(minimumIntervals)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
for _ in range(int(input())): n, lisa, lisb, curr, ans, i, j = int(input()), [], [], 0, 10000000.0, 0, 0 for _ in range(n): a, b = map(int, input().split()) lisa.append(a), lisb.append(b + 1) lisa.sort(), lisb.sort() while i < n: if lisa[i] < lisb[j]: curr, i = curr + 1, i + 1 else: curr, j = curr - 1, j + 1 if curr != i: ans = min(ans, curr) print(-1) if ans == 10000000.0 else print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR LIST LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR VAR NUMBER FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
for _ in range(int(input())): n = int(input()) a, b = [], [] for i in range(n): l, r = map(int, input().split()) a.append(l) b.append(r) temp = [] a.sort(reverse=True) b.sort(reverse=True) count = 0 while a and b: if a[-1] <= b[-1]: count += 1 temp.append([count, "a"]) a.pop() else: count -= 1 temp.append([count, "d"]) b.pop() while a: count += 1 temp.append([count, "a"]) a.pop() while b: count -= 1 temp.append([count, "d"]) b.pop() flag = False ans = 100000000 for i, j in temp: if j == "a": if flag: ans = min(ans, i - 1) else: flag = True if ans == 100000000: print(-1) else: print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR STRING EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR STRING EXPR FUNC_CALL VAR WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR STRING EXPR FUNC_CALL VAR WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR STRING IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
T = int(input()) while T: N = int(input()) arrive = [] depart = [] for i in range(N): u, v = map(int, input().split()) arrive.append(u) depart.append(v) depart.sort() arrive.sort() List = [-1] * 2 * N i, j, k = 0, 0, 0 prev = 0 while i < N and j < N: if arrive[i] > depart[j]: prev -= 1 List[k] = ["d", prev] j += 1 else: prev += 1 List[k] = ["a", prev] i += 1 k += 1 while i < N: prev += 1 List[k] = ["a", prev] i += 1 k += 1 while j < N: prev -= 1 List[k] = ["d", prev] j += 1 k += 1 count = 10**7 flag = False for u, v in List: if u == "a": if flag: count = min(count, v - 1) else: flag = True if count == 10**7: print(-1) else: print(count) T -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST 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 VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER NUMBER VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR LIST STRING VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR LIST STRING VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR LIST STRING VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR LIST STRING VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR STRING IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
t = int(input()) for i in range(t): n = int(input()) start = [] end = [] for i in range(n): s = list(map(int, input().split())) start.append(s[0]) end.append(s[1]) s = e = ps = pe = 0 start.sort() end.sort() a = start[0] b = start[n - 1] c = end[0] d = end[n - 1] m = n if b <= c: m = -1 else: while pe == 0: if start[ps] < end[pe]: tmp = start[ps] s += 1 ps += 1 while ps < n and start[ps] == tmp: s += 1 ps += 1 elif start[ps] == end[pe]: tmp = start[ps] e += 1 pe += 1 s += 1 ps += 1 while ps < n and start[ps] == tmp: s += 1 ps += 1 while pe < n and end[pe] == tmp: e += 1 pe += 1 else: tmp = end[pe] e += 1 pe += 1 while pe < n and end[pe] == tmp: e += 1 pe += 1 while ps < n and pe < n: if s - e < m: m = s - e if start[ps] < end[pe]: s += 1 ps += 1 elif start[ps] == end[pe]: s += 1 ps += 1 e += 1 pe += 1 else: e += 1 pe += 1 print(m)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR 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 NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
T = int(input()) for _ in range(T): N = int(input()) begin = [] end = [] for iterations in range(N): l = [int(i) for i in input().split()] begin.append(l[0]) end.append(l[1]) begin = sorted(begin) end = sorted(end) array = [-1] * (2 * N) begin_index = 0 end_index = 0 k = 0 count = 0 while end_index < N and begin_index < N: if begin[begin_index] > end[end_index]: count -= 1 array[k] = ["-", count] end_index += 1 else: count += 1 array[k] = ["+", count] begin_index += 1 k += 1 while begin_index < N: count += 1 array[k] = ["+", count] begin_index += 1 k += 1 while end_index < N: count -= 1 array[k] = ["-", count] end_index += 1 k += 1 result = float("inf") temp = 0 for item in array: if item[0] == "-": temp = 1 elif item[0] == "+": if temp: result = min(result, item[1] - 1) if result == float("inf"): print(-1) else: print(result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR LIST STRING VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR LIST STRING VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR LIST STRING VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR LIST STRING VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER IF VAR NUMBER STRING IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
t = int(input()) while t > 0: t -= 1 n = int(input()) d = {} b = set() lm = 0 rm = 1000000001 mn = n while n > 0: n -= 1 l, r = list(map(int, input().split())) if l not in d: d[l] = 0 if r + 1 not in d: d[r + 1] = 0 if r not in d: d[r] = 0 d[l] += 1 d[r + 1] -= 1 b.add(l) b.add(r) if rm > r: rm = r if lm < l: lm = l if rm >= lm: print(-1) else: b1 = list(b) b1.sort() p = 0 f = 0 for i in range(len(b1) - 1): p = p + d[b1[i]] + f if b1[i] >= rm and b1[i] <= lm: if p < mn: mn = p if p == 1: break if b1[i] + 1 in d and b1[i] + 1 != b1[i + 1]: f = d[b1[i] + 1] else: f = 0 print(mn - 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
def main(): t = int(input()) for p in range(t): n = int(input()) arr = [] left_max = 0 right_min = 10**9 for i in range(n): x = input().split() x[0], x[1] = int(x[0]), int(x[1]) left_max = max(left_max, x[0]) right_min = min(right_min, x[1]) arr.append([x[0], "L"]) arr.append([x[1], "R"]) arr.sort() final_ans = 10**5 ans = 0 for i in range(2 * n): if arr[i][1] == "L": if arr[i][0] > right_min: final_ans = min(final_ans, ans) ans += 1 else: ans -= 1 if final_ans == 10**5: print(-1) else: print(final_ans) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER STRING EXPR FUNC_CALL VAR LIST VAR NUMBER STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR NUMBER STRING IF VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
import itertools for _ in range(0, int(input())): n = int(input()) l = {} d = {} mi = 10**9 + 4 ma = 0 mi1 = 0 ma1 = 10**9 + 4 for i in range(0, n): x, y = map(int, input().split()) if x in l: l[x] += 1 else: l[x] = 1 if y + 1 in l: l[y + 1] -= 1 else: l[y + 1] = -1 if x not in d: d[x] = 1 else: d[x] += 1 mi = min(mi, x) mi1 = max(mi1, x) ma = max(ma, y) ma1 = min(ma1, y) ans = 10**9 + 6 l = sorted(l.items(), key=lambda t: t[0]) s = 0 j = 0 c = {} for i in range(0, len(l)): s += l[i][1] if l[i][0] >= ma1 + 1 and l[i][0] < mi1 + 1 and l[i][0] in d: c[l[i][0]] = s for u, v in c.items(): ans = min(c[u] - d[u], ans) if n - ans <= 1: print(-1) else: print(ans)
IMPORT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR FOR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
for t in range(int(input())): n = int(input()) starts, ends = [], [] minimum, maximum = float("inf"), 0 for i in range(n): t = [int(i) for i in input().split()] starts.append(t[0]) ends.append(t[1]) starts.sort() ends.sort() counts, startI = {}, 0 for i, end in enumerate(ends): end += 0.5 while startI < n and starts[startI] < end: startI += 1 endBef = i + 1 counts[end] = startI, endBef best = None for pt, (startBef, endBef) in counts.items(): contains = startBef - endBef if startBef != n and startBef != 0 and (best == None or contains < best): best = contains if best == None: print(-1) else: print(best)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR DICT NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NONE FOR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER VAR NONE VAR VAR ASSIGN VAR VAR IF VAR NONE EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
t = int(input()) for _ in range(t): n = int(input()) s = [] e = [] for i in range(n): l, r = map(int, input().strip().split(" ")) s.append(l) e.append(r) s.sort() e.sort() i = 0 j = 0 mi = float("inf") for i in range(1, n): while e[j] < s[i]: j += 1 if j == 0: continue mi = min(i - j, mi) if mi == float("inf"): print(-1) else: print(mi)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
for _ in range(int(input())): N = int(input()) li = [] for i in range(N): a, b = [int(j) for j in input().split()] li.append((a, 0)) li.append((b, 1)) li.sort() r = len(li) - 1 while li[r][1] == 1: r -= 1 flag = -1 curr = -1 mn = N for ind, i in enumerate(li): if curr == 0: flag = 1 break elif curr == N: flag = 2 break elif curr == -1: curr = 0 if i[1] == 0: curr += 1 else: curr -= 1 if ind <= r + 1: mn = min(curr, mn) if flag == -1: print(mn) elif flag == 1: print(0) else: print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
def upper_bound(arr, x): l, h = 0, len(arr) while l < h: mid = (l + h) // 2 if x >= arr[mid]: l = mid + 1 else: h = mid return l def main(): for t in range(int(input())): n = int(input()) XS = [(0) for i in range(n)] YS = [(0) for i in range(n)] for i in range(n): a, b = map(int, input().split()) XS[i] = a YS[i] = b XS.sort() YS.sort() res, i = n, 0 while i < n: it = i while it < n and YS[it] == YS[i]: it += 1 idx = upper_bound(XS, YS[i]) if it < n and idx < n: res = min(res, idx - it) i = it print(res if res != n else -1) main()
FUNC_DEF ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
def testcase(arr, dep, n): arr.sort() dep.sort() plat_needed = 0 result = n i = 0 j = 0 flag = False while i < n and j < n - 1: if arr[i] <= dep[j]: plat_needed += 1 i += 1 else: flag = True plat_needed -= 1 j += 1 if plat_needed < result and flag: result = plat_needed if result == 0: return 0 if n - result < 2: return -1 if result > n - 1: return -1 return result def testcase2(n, pair): counter = len(pair) for current in range(len(pair)): flag = True count = 0 upper = pair[current][1] for i in range(len(pair)): if i == current: continue if pair[i][1] <= upper: continue if pair[i][1] > upper: if pair[i][0] > upper: flag = False continue else: count += 1 if flag: count = len(pair) counter = min(counter, count) if counter >= n - 1: return -1 else: return counter t = int(input()) for i in range(t): n = int(input()) arr = [] dep = [] for j in range(n): a, b = [int(_) for _ in input().split()] arr.append(a) dep.append(b) print(testcase(arr, dep, n))
FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
def main(debug=False): for _ in range(int(input())): n = int(input()) l, r = [], [] for i in range(n): lr = list(map(int, input().split())) l.append(lr[0]) r.append(lr[1]) l.sort() r.sort() if debug: print(l) print(r) lp, rp, mp = 0, 0, 0 merged = [(0) for i in range(2 * n)] if debug: print(merged) cnt = 0 while True: if mp == 2 * n: break if lp == n: merged[mp:] = [(cnt - (i - rp + 1)) for i in range(rp, n)] break if rp == n: merged[mp:] = [(cnt + (i - lp + 1)) for i in range(lp, n)] break if debug: print("For i=", mp, "l[lp] r[rp]", l[lp], r[rp]) if l[lp] < r[rp]: cnt += 1 merged[mp] = cnt lp += 1 elif l[lp] == r[rp]: cnt += 1 merged[mp] = cnt lp += 1 elif l[lp] > r[rp]: cnt -= 1 merged[mp] = cnt rp += 1 mp += 1 if debug: print(merged) prev = merged[0] flag = False count = 10**5 + 1 for i in range(1, 2 * n - 1): curr = merged[i] if curr > prev: if flag: count = min(count, merged[i] - 1) elif curr < prev: flag = True prev = curr if debug: print("Count", count) if count == 10**5 + 1: print(-1) else: print(count) main(debug=False)
FUNC_DEF NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING VAR STRING VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR STRING VAR IF VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
for _ in range(int(input())): n = int(input()) l, r = [], [] for _ in range(n): x, y = map(int, input().strip().split()) l.append(x) r.append(y) l.sort() r.sort() ans, b, j = float("Inf"), False, 0 for i in range(n): while l[j] <= r[i]: j += 1 if j == n: b = True break if b: break ans = min(ans, j - i - 1) print(-1 if ans == float("Inf") else ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR STRING NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
T = int(input()) for t in range(T): n = int(input()) ranges = [] for i in range(n): a, b = map(int, input().split()) ranges.append((a, 0)) ranges.append((b, 1)) ranges = list(sorted(ranges)) cnt = 0 res = 1000000000.0 possible = False for _, dir in ranges: if dir == 0: if possible: res = min(res, cnt) cnt += 1 else: cnt -= 1 possible = True if res == 1000000000.0: res = -1 print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
t = int(input()) while t > 0: n = int(input()) ends = [] starts = [] for _ in range(n): inp = list(map(int, input().split())) starts.append(inp[0]) ends.append(inp[1]) starts.sort() ends.sort() i = 0 j = 0 vals = [] cnt = 999999999 ctr = 0 while i < n and j < n: if starts[i] > ends[j]: ctr -= 1 j += 1 cnt = min(cnt, ctr) else: i += 1 ctr += 1 if cnt == 999999999 or cnt == n: print(-1) else: print(cnt) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
def binary(a, l, r, e): if e > a[r] or l > r: return -1 start, end, mid, max1 = l, r, -1, -1 while start <= end: mid = start + end >> 1 if a[mid] > e: end = mid - 1 else: start = mid + 1 max1 = max(mid, max1) return max1 for _ in range(int(input())): n = int(input()) tr = {} arr = [(0) for i in range(n)] for i in range(n): l, r = [int(i) for i in input().split(" ")] tr[l] = tr.get(l, 0) + 1 arr[i] = r arr.sort() last, count, ans = 0, 0, n for k in sorted(tr.keys()): last = max(binary(arr, last, n - 1, k - 1) + 1, last) delete = count - last if delete != count: ans = min(ans, delete) if ans == 0: break count += tr[k] if ans != n: print(ans) else: print(-1)
FUNC_DEF IF VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
t = int(input()) for _ in range(t): n = int(input()) ranges = {(1): [0, 0, 0, 0]} for i in range(n): l, r = map(int, input().split()) if l in ranges: ranges[l][0] += 1 else: ranges[l] = [1, 0, 0, 0] if r in ranges: ranges[r][1] += 1 else: ranges[r] = [0, 1, 0, 0] sortedkeys = sorted(ranges) opened = 0 closed = 0 for i in sortedkeys: opened += ranges[i][0] closed += ranges[i][1] opened -= ranges[i][1] ranges[i][2] = opened ranges[i][3] = closed sortedkeysDesc = sorted(ranges, reverse=True) flag = 0 todelete = -1 x = -1 for j in sortedkeysDesc: if flag == 0 and ranges[j][0] >= 1: flag = 1 elif flag == 1 and ranges[j][3] > 0: if ranges[j][2] < todelete or todelete == -1: todelete = ranges[j][2] print(todelete)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT NUMBER LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR LIST NUMBER NUMBER NUMBER NUMBER IF VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
t = int(input()) for _ in range(t): n = int(input()) s = [] for i in range(n): l, r = input().split() l = int(l) r = int(r) s.append([l, r]) s.sort() l = [] l.append([s[0][0], s[0][1], 1]) for i in range(1, n): if s[i][0] > l[-1][1]: l.append([s[i][0], s[i][1], 1]) elif s[i][0] <= l[-1][1]: l[-1][1] = max(l[-1][1], s[i][1]) l[-1][2] += 1 if len(l) > 1: print(0) elif l[0][2] <= 2: print(-1) else: l2 = [] for i in range(0, len(s)): l2.append(s[i][1]) l2.sort() sp = 0 ep = 0 m = n + 1 i = 0 j = 0 f = 0 while i < n: if s[i][0] <= l2[j]: sp += 1 if sp - 1 - ep < m and ep != 0: f = 1 m = sp - ep - 1 i += 1 else: ep += 1 j += 1 if f == 1: print(m) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
t = int(input()) while t > 0: t -= 1 n = int(input()) a = [] b = [] for i in range(n): l, r = map(int, input().split()) a.append(l) b.append(r) a.sort() b.sort() i, j = 0, 0 cur = 0 ans = 2 << 32 while i < n and j < n: if a[i] <= b[j]: cur += 1 i += 1 else: cur -= 1 j += 1 if j > 0: ans = min(ans, cur) if ans == 2 << 32: print(-1) else: print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST 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 VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
for _ in range(int(input())): N = int(input()) X, Y = [], [] for i in range(N): H, Z = map(int, input().split()) X.append([H, 0]) X.append([Z, 1]) X.sort() M = float("inf") count = 0 a = 0 for i in range(len(X)): if a == N: break if X[i][1] == 0: count += 1 a += 1 else: count -= 1 if count < M: M = count if M < N - 1: print(M) else: print("-1")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST 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 NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
T = int(input()) for _ in range(T): N = int(input()) A = [] B = [] C = [] a, b = map(int, input().split()) mb = b xb = b ma = a xa = a A.append([a, b]) B.append(a) C.append(b) for i in range(N - 1): a, b = map(int, input().split()) if b < mb: mb = b if b > xb: xb = b if a < ma: ma = a if a > xa: xa = a A.append([a, b]) B.append(a) C.append(b) if xa <= mb: print(-1) continue maxval = N - 1 B.sort(reverse=True) C.sort(reverse=True) count = 0 i = len(B) - 1 j = len(C) - 1 while True: if i == -1: break if B[i] <= C[j]: count = count + 1 if B[i] > mb: maxval = min(count - 1, maxval) i = i - 1 else: count = count - 1 j = j - 1 print(maxval)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE NUMBER IF VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
def main(): nn = rem = cnt = a = b = i = temp = 0 for _ in range(int(input())): vec = [] nn = int(input()) rem = cnt = 0 res = nn for i in range(nn): a, b = input().split() vec.append([int(a), 1]) vec.append([int(b), 0]) vec.sort() i = 0 while i < len(vec) - 1: j = i while j < len(vec) and vec[j][0] == vec[i][0]: if vec[j][1] == 1: cnt += 1 else: cnt -= 1 rem += 1 j += 1 i = j temp = nn - rem - cnt if temp != 0 and rem != 0: if res > cnt: res = cnt if res == nn: res = -1 print(res) main()
FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
t = int(input()) for i in range(t): n = int(input()) li = [] for j in range(n): x, y = map(int, input().split()) li.append([x, "s"]) li.append([y + 0.01, "c"]) li.sort(key=lambda x: x[0]) mi = 1000000001 l = 0 pre = li[0] for i in range(1, len(li)): if li[i][1] == "s": l += 1 elif li[i][1] == "c": l -= 1 if li[i][1] == "s" and pre[1] == "c": mi = min(mi, l) pre = li[i] if mi == 1000000001: print(-1) else: print(mi)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR STRING EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER STRING VAR NUMBER IF VAR VAR NUMBER STRING VAR NUMBER IF VAR VAR NUMBER STRING VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
for _ in range(int(input())): arr = [] dep = [] pair = [] n = int(input()) for _ in range(n): a, d = input().split() pair.append((int(a), int(d))) arr.append(int(a)) dep.append(int(d)) arr.sort() dep.sort() i = 0 j = 0 count = 0 plat = [] while i < n and j < n: if arr[i] <= dep[j]: count += 1 plat.append((count, "a")) i += 1 elif arr[i] > dep[j]: count -= 1 plat.append((count, "d")) j += 1 while i < n: count += 1 plat.append((count, "a")) i += 1 while j < n: count -= 1 plat.append((count, "d")) j += 1 flag = False ans = 1000000 for cnt, typ in plat: if typ == "a": if flag: ans = min(ans, cnt - 1) else: flag = True if ans == 1000000: ans = -1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR NUMBER WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR NUMBER WHILE VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR STRING IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
def solve(l, r): l.sort() r.sort() overlap = 1 n = len(l) arr = [] i, j = 1, 0 while i < n and j < n: if l[i] <= r[j]: overlap += 1 i += 1 else: overlap -= 1 j += 1 arr.append(overlap) if overlap == 0: print(0) return if overlap == n: print(-1) return result = float("inf") for ind in range(len(arr) - 2): if arr[ind] == arr[ind + 2]: result = min(result, arr[ind + 1]) print(result) for case in range(int(input())): l, r = [], [] for tp in range(int(input())): a, b = list(map(int, input().split())) l.append(a) r.append(b) solve(l, r)
FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
from sys import stdin def search2(array, s, end): i = s j = len(array) - 1 - s while j > 0: while i + j < len(array) and array[i + j] <= end: i += j j //= 2 if i == len(array) - 1: if array[len(array) - 1] < end: i += 1 return i for __ in range(int(stdin.readline())): starts = [] ends = [] n = int(stdin.readline()) for i in range(n): start, end = map(int, stdin.readline().split()) starts.append(start) ends.append(end) starts.sort() ends.sort() ans = 1010101 e = s = 0 while e < n and s < n: while s < n and starts[s] <= ends[e]: s += 1 if s - e > 0 and s - e < ans and s < n: ans = s - e e += 1 if ans == 1010101: ans = 0 print(ans - 1)
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR WHILE VAR NUMBER WHILE BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST 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 EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
for _ in range(int(input())): n = int(input()) start = [] end = [] for __ in range(n): a, b = map(int, input().split()) start.append(a) end.append(b) start.sort() end.sort() s = {} e = {} su = 1 en = 0 k = 0 for i in range(1, n): if start[i] > start[i - 1]: s[start[i]] = su su += 1 while start[i] > end[k]: en += 1 k += 1 e[start[i]] = k ans = 1000000007 for i in s.keys(): if i > end[0]: t = s[i] - e[i] if ans > t: ans = t if ans == 1000000007: print(-1) else: print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
for _ in range(int(input())): n = int(input()) xlis = [] ylis = [] for i in range(n): a, b = map(int, input().split()) xlis.append(a) ylis.append(b) xlis.sort() ylis.sort() ans = 0 poi = 0 for i in range(n): y = ylis[i] while poi != n: if xlis[poi] <= y: poi += 1 else: break gx = n - poi if gx == 0: break else: smly = i + 1 ans = max(ans, smly + gx) fl = 1 if ans: print(n - ans) else: print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
from sys import stdin input = stdin.readline for _ in range(int(input())): n = int(input()) ranges = [] first = 0, 10**9 last = -1, 0 for _ in range(n): l, r = map(int, input().split()) ranges.append((l, r)) if r < first[1]: first = l, r if l > last[0]: last = l, r if first[1] >= last[0]: print(-1) continue vals = {} for tpl in ranges: l, r = tpl vals[l] = vals.get(l, 0) + 1 vals[l + 0.5] = vals.get(l + 0.5, 0) vals[r + 0.5] = vals.get(r + 0.5, 0) - 1 points = [] for point in vals.keys(): points.append(point) points.sort() points.pop() mincount = 10**9 count = 0 for point in points: count += vals[point] if point > first[1] and point < last[0]: mincount = min(mincount, count) if mincount == 0: break print(mincount)
ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER 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 IF VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
import sys try: import sys t = int(input()) for _ in range(t): l = [] r = [] arr = [] n = int(input()) for i in range(n): a, b = map(int, input().split()) l.append(a) r.append(b) l.sort() r.sort() overlap = 1 result = 1 i = 1 j = 0 while i < n and j < n: if l[i] <= r[j]: overlap += 1 i += 1 else: overlap -= 1 j += 1 arr.append(overlap) if overlap == 0: result = 0 break elif overlap == n: result = -1 break a = sys.maxsize if result != 0 and result != -1: i = 0 while i <= len(arr) - 3: if arr[i] == arr[i + 2]: if a > arr[i + 1]: a = arr[i + 1] i += 1 print(a) else: print(result) except: pass
IMPORT IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST 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 EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
test = int(input()) for intnt in range(test): num = int(input()) arr1 = [] arr2 = [] for indx1 in range(num): val1, y = list(map(int, input().split())) arr1.append(val1) arr2.append(y) arr1.sort() arr2.sort() ar = [] for i in range(num): ar.append(i) if num != 1: val1 = list(set(arr2)) val1.sort() p = [0] * len(val1) q = [0] * len(val1) indx2 = 0 indx1 = 0 while indx1 < num: if indx2 == len(val1): break if arr1[indx1] <= val1[indx2]: p[indx2] += 1 indx1 += 1 else: indx2 += 1 indx2 = 0 indx1 = 0 while indx1 < num: if indx2 == len(val1): break if arr2[indx1] <= val1[indx2]: q[indx2] += 1 indx1 += 1 else: indx2 += 1 q[indx2] = q[indx2 - 1] for indx1 in range(1, len(p)): p[indx1] += p[indx1 - 1] for indx1 in range(len(p)): p[indx1] = num - p[indx1] ans = num + 2 for indx1 in range(len(val1)): if not (p[indx1] == 0 or q[indx1] == 0): ans = min(ans, num - p[indx1] - q[indx1]) if ans == num + 2: print(-1) else: print(ans) else: print(-1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
t = int(input()) while t: t -= 1 n = int(input()) l = [] r = [] arr = [] for i in range(n): l1, l2 = map(int, input().split()) l.append(l1) r.append(l2) l.sort() r.sort() res = 1 overlapping = 1 i = 1 j = 0 while i < n and j < n: if l[i] <= r[j]: overlapping += 1 i += 1 else: overlapping -= 1 j += 1 arr.append(overlapping) if overlapping == 0: res = 0 break if overlapping == n: res = -1 break if res != 0 and res != -1: m = -1 for i in range(len(arr) - 2): if arr[i] == arr[i + 2]: if m == -1: m = arr[i + 1] else: m = min(m, arr[i + 1]) print(m) else: print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR 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 EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
for _ in range(int(input())): n = int(input()) l = [] r = [] for i in range(n): li, ri = map(int, input().split()) l.append(li) r.append(ri) l.sort() r.sort() exists, flag = False, False j = 0 ans = n for i in range(n - 1): while l[j] <= r[i]: j += 1 if j == n: flag = True break if flag == True: break ans = min(ans, j - i - 1) exists = True if exists == True: print(ans) else: print(-1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
t = int(input()) for _ in range(t): n = int(input()) arr = [] end = [] count1 = [] for i in range(n): a, b = map(int, input().split()) arr.append(a) end.append(b) arr.sort() end.sort() p1 = p2 = 0 pre = 0 while p1 != n and p2 != n: if arr[p1] <= end[p2]: if pre == 0: count1.append((1, "a")) else: x, y = count1[pre - 1] count1.append((x + 1, "a")) pre += 1 p1 += 1 else: x, y = count1[pre - 1] count1.append((x - 1, "d")) pre += 1 p2 += 1 while p2 != n: x, y = count1[pre - 1] count1.append((x - 1, "d")) pre += 1 p2 += 1 ans = 10000000 flag = False for i, j in count1: if j == "a": if flag: ans = min(ans, i - 1) else: flag = True if ans == 10000000: print("-1") else: 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 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 EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER STRING ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR STRING IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
for h in range(int(input())): beg = [] end = [] n = int(input()) for _ in range(n): s, e = map(int, input().strip().split()) beg.append(s) end.append(e) mini = float("inf") beg.sort() end.sort() j1, j2 = n - 1, n - 1 while beg[j1] == beg[j1 - 1] and j1 > 0: j1 -= 1 while end[j2] == end[j2 - 1] and j2 > 0: j2 -= 1 if j1 == 0 or j2 == 0: print(-1) continue c = 0 i1, i2 = 0, 0 while i1 < j1 and i2 < j2: if beg[i1] <= end[i2]: c += 1 i1 += 1 else: c -= 1 i2 += 1 mini = min(c, mini) while i2 < j2 and end[i2] < beg[j1]: c -= 1 i2 += 1 mini = min(c, mini) if mini == n or mini == float("inf"): print(-1) else: print(mini)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST 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 FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
def fun(partitions): if len(partitions) == 2: print(0) else: end_points = {} for j in partitions: end_points[j[0]] = end_points.get(j[0], 0) + 1 end_points[j[1]] = end_points.get(j[1], 0) - 1 final_partitions = sorted(end_points.items()) temp = [] temp.append(0) for j in final_partitions: temp.append(temp[-1] + j[1]) if len(temp) > 4: a = min(temp[2:-2]) else: a = min(temp[1:-1]) if a >= len(partitions) - 1: print(-1) else: print(a) t = int(input()) for i in range(t): n = int(input()) ans = n maxl0 = int(0) minl1 = int(0) partitions = {} for j in range(n): l = input().split(" ") l[0] = int(l[0]) l[1] = int(l[1]) partitions[l[0]] = partitions.get(l[0], 0) + 1 partitions[l[1]] = partitions.get(l[1], 0) - 1 if j == 0: maxl0 = l[0] minl1 = l[1] else: maxl0 = max(maxl0, l[0]) minl1 = min(minl1, l[1]) sorted_partitions = [] for j in sorted(partitions.keys()): sorted_partitions.append([j, partitions[j]]) cnt = int(0) for j in sorted_partitions: cnt += j[1] if cnt < ans and j[0] >= minl1 and j[0] < maxl0: ans = cnt if ans > n - 2: print(-1) else: print(ans)
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL 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 VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
for _ in range(int(input())): n = int(input()) d = {} inter = [] laststart = 0 firstend = 1000000000 for i in range(n): l, r = input().split() l = int(l) r = int(r) if l > laststart: laststart = l if firstend > r: firstend = r if l in d.keys(): d[l][0] += 1 else: d[l] = [1, 0] if r in d.keys(): d[r][1] += 1 else: d[r] = [0, 1] k = sorted(list(d.keys())) flag = 0 temp = 0 for i in k: if i == laststart: break if flag == 0: temp += d[i][0] - d[i][1] if i == firstend and d[i][1] != 0: inter.append(temp) flag = 1 elif flag == 1: temp += d[i][0] - d[i][1] inter.append(temp) if len(inter) == 0: print(-1) else: inter = sorted(inter) print(inter[0])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR LIST NUMBER NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR IF VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
T = int(input()) while T > 0: N = int(input()) res = N count = 0 l = [] r = [] for _ in range(N): t1, t2 = list(map(int, input().split())) l.append(t1) r.append(t2) l = sorted(l) r = sorted(r) for _ in range(N): if l[_] > r[0]: count = _ break else: print(-1) T -= 1 continue result, i, j = 0, count, 0 while True: if l[i] <= r[j]: count += 1 i += 1 if i == N: break else: count -= 1 res = min(res, count) j += 1 print(res) T -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER WHILE NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
for t in range(int(input())): n = int(input()) d = dict() for _ in range(n): x, y = map(int, input().split()) if x in d: d[x][0] += 1 else: d[x] = [1, 0, 0] if y in d: d[y][1] += 1 else: d[y] = [0, 1, 0] o = 0 c = 0 ans = n + 1 for i in sorted(d): o += d[i][0] c += d[i][1] d[i][2] = o - c if c == 0 or o == n: continue ans = min(ans, d[i][2]) if n - ans > 1: print(ans) else: print("-1")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR LIST NUMBER NUMBER NUMBER IF VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
from sys import stdin, stdout for _ in range(int(stdin.readline())): n = int(stdin.readline()) l = [] departure = cnt = 0 ans = float("inf") for i in range(n): arr, dep = list(map(int, stdin.readline().split())) l += [[arr, 1], [dep, -1]] l.sort(key=lambda x: (x[0], -x[1])) for pt, type in l: cnt += type if type == 1: if departure: ans = min(ans, cnt - 1) else: departure = 1 print(ans if ans != float("inf") else -1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR LIST LIST VAR NUMBER LIST VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR VAR VAR VAR VAR IF VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR NUMBER
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well. Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset. Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains a single integer $N$. $N$ lines follow. For each $i$ ($1 ≀ i ≀ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$. ------ Output ------ For each test case, print a single line containing one integer ― the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets. ------ Constraints ------ $1 ≀ T ≀ 10$ $1 ≀ N ≀ 10^{5}$ $1 ≀ l_{i} ≀ r_{i} ≀ 10^{9}$ for each valid $i$ ------ Subtasks ------ Subtask #1 (10 points): $N = 16$ Subtask #2 (15 points): $N = 1,000$ Subtask #3 (15 points): $r_{i} ≀ 10^{6}$ for each valid $i$ Subtask #4 (60 points): original constraints ----- Sample Input 1 ------ 3 3 1 4 2 6 5 7 2 1 4 4 6 2 3 7 8 9 ----- Sample Output 1 ------ 1 -1 0 ----- explanation 1 ------ Example case 1: It is enough to delete the range $[2, 6]$. Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too. Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset.
try: testcase = int(input()) while testcase: testcase -= 1 n = int(input()) mi, ma1 = 10**9 + 9, 10**9 + 9 ma, mi1 = 0, 0 l, d = {}, {} for i in range(0, n): x, y = map(int, input().split()) l.setdefault(x, 0) l.setdefault(y + 1, 0) d.setdefault(x, 0) l[x] += 1 l[y + 1] -= 1 d[x] += 1 mi = min(mi, x) mi1 = max(mi1, x) ma = max(ma, y) ma1 = min(ma1, y) o = sorted(l.items(), key=lambda t: t[0]) sp = 0 c = {} for i in o: sp += i[1] if i[0] >= ma1 + 1 and i[0] < mi1 + 1 and i[0] in d: c[i[0]] = sp an = 10**9 + 6 for x, y in c.items(): an = min(c[x] - d[x], an) print(-1 if n - an <= 1 else an) except: pass
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR DICT DICT FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR