description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins. Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it. After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied: Vasya hasn't bought $x$. Vasya has bought integer $l$ that is less than $x$. Vasya has bought integer $r$ that is greater than $x$. Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift. For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift. Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop. Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them. For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available. -----Input----- The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β€” the number of segments in the shop. Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β€” the ends of the $i$-th segments and its cost. It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$. -----Output----- For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available. -----Examples----- Input 3 2 2 4 20 7 8 22 2 5 11 42 5 11 42 6 1 4 4 5 8 9 7 8 7 2 10 252 1 11 271 1 10 1 Output 20 42 42 42 4 13 11 256 271 271 -----Note----- In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers. The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement. In the second test case note, that there can be the same segments in the shop.
import sys input = sys.stdin.readline inf = 11000000000.0 t = int(input()) for i in range(t): n = int(input()) s = [inf, inf] e = [-inf, -inf] o = [-inf, -inf] for i in range(n): l, r, c = map(int, input().split()) s = min(s, [l, c]) e = max(e, [r, -c]) o = max(o, [r - l, -c]) ans = s[1] - e[1] if e[0] - s[0] == o[0]: ans = min(ans, -o[1]) print(str(ans))
IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR LIST VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR LIST BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
n = int(input()) arr = list(map(int, input().split())) dif = [0] * n l = 0 for i in range(n - 1, -1, -1): if arr[i] == 0: l += 1 dif[i] = l nuksan = 0 for i in range(n - 1): if arr[i] == 1: nuksan += dif[i + 1] print(nuksan)
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 FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
n = int(input()) z = [int(x) for x in input().split()] x = z.count(0) an = 0 for i in range(n): if z[i] == 0: x -= 1 else: an += x x = z.count(1) z = z[::-1] an12 = 0 for i in range(n): if z[i] == 1: x -= 1 else: an12 += x print(min(an, an12))
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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
def P0122_cow_milk(n, a): ans = 0 r = 0 for x in a: if x == 1: r += 1 else: ans += r return ans n = int(input()) a = [int(x) for x in input().split()] result = P0122_cow_milk(n, a) print(result)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
n = int(input()) arr = list(map(int, input().split())) cnt_zero, res = 0, 0 for i in range(n - 1, -1, -1): if arr[i] == 0: cnt_zero += 1 else: res += cnt_zero print(res)
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 FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
n = int(input()) lis = list(map(int, input().split())) oc, tc = 0, 0 for i in lis: if i == 1: oc += 1 elif i == 0: tc += oc val1 = tc tc, oc = 0, 0 for i in reversed(lis): if i == 0: oc += 1 elif i == 1: tc += oc val2 = tc print(min([val1, val2]))
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 FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR LIST VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
n = int(input()) a = list(map(int, input().split())) b = [0, 0] ans = 0 for i in range(n): b[a[i]] += 1 if a[i] == 0: ans += b[1] print(ans)
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 NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
n = int(input()) array = list(map(int, input().split())) cnt = 0 s = [(0) for i in range(n)] s[n - 1] = 1 * (array[n - 1] == 0) for i in range(n - 2, -1, -1): s[i] = s[i + 1] + 1 * (array[i] == 0) for i in range(n): cnt += s[i] * (array[i] == 1) print(cnt)
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 VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
n = int(input()) cnt = 0 s = 0 l = [int(i) for i in input().split()] for i in range(n): if l[i] == 1: cnt += 1 else: s += cnt print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
n = input() a = list(map(int, input().split(" "))) n0 = a.count(0) n1 = a.count(1) res = list() if n0 < n1: n1_cnt = 0 for i in a: if i == 1: n1_cnt += 1 else: res.append(n1_cnt) else: n0_cnt = 0 for i in reversed(a): if i == 0: n0_cnt += 1 else: res.append(n0_cnt) print(sum(res))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
n = int(input()) l = [int(i) for i in input().split()] zeroes = [0] * n if l[-1] == 0: zeroes[n - 1] = 1 else: zeroes[n - 1] = 0 for i in range(n - 2, -1, -1): zeroes[i] = zeroes[i + 1] if l[i] == 0: zeroes[i] += 1 cnt = 0 for i in range(n): if l[i] == 1: cnt += zeroes[i] print(cnt)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
def main(): input() i = res = 0 for c in input()[::2]: if c == "1": i += 1 else: res += i print(res) def __starting_point(): main() __starting_point()
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR STRING VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
n = int(input()) l = list(map(int, input().split())) x = l.count(0) y = l.count(1) if x >= y: a = [] k = 0 for i in range(n - 1, -1, -1): if l[i] == 1: a.append(k) else: k += 1 else: a = [] k = 0 for i in range(n): if l[i] == 0: a.append(k) else: k += 1 print(sum(a))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
x = 0 y = 0 n = int(input()) a1 = input().split() a = [int(a1[i]) for i in range(len(a1))] for i in range(n): if a[i]: x += 1 else: y += x print(y)
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
__author__ = "Pavel Mavrin" n = int(input()) a = [int(x) for x in input().split()] s = 0 res = 0 for i in a: if i == 0: res += s else: s += 1 print(res)
ASSIGN VAR STRING 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 NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
def cows(lst): k1, k2 = 0, 0 for i in range(len(lst)): if lst[i] == 1: k1 += 1 else: k2 += k1 return k2 n = int(input()) a = [int(j) for j in input().split()] print(cows(a))
FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR RETURN 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 FUNC_CALL VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
n = int(input()) arr = list(map(int, input().split())) zero_count = [0] * n zero_count[n - 1] = int(arr[n - 1] == 0) for i in reversed(range(n - 1)): zero_count[i] = zero_count[i + 1] + int(arr[i] == 0) res = 0 for i in range(n - 1): if arr[i] == 1: res += zero_count[i + 1] print(res)
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 VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
n = int(input()) m = list(input().split()) ca = cb = int(0) for i in range(n): if int(m[i]) == 0: ca += 1 else: cb += 1 a = b = int(0) if ca > cb: for i in range(n): if int(m[n - i - 1]) == 0: a = a + cb else: cb -= 1 print(a) else: for i in range(n): if int(m[i]) == 1: b += ca else: ca -= 1 print(b)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
n, v = int(input()), list(map(int, input().split())) ans = 0 r = 0 for i in v: if i == 1: r += 1 else: ans += r print(ans)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
n = int(input()) arr = list(map(int, input().split())) cnt = [0] * n cnt[0] = arr[0] for i in range(1, n): cnt[i] = cnt[i - 1] + arr[i] ans = 0 for i in range(n): if arr[i] == 0: ans += cnt[i] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
n = int(input()) a = list(map(int, input().split())) b = 0 c = 0 d = 0 e = 0 for i in range(n): b += a[i] c += b * (1 - a[i]) for i in range(n - 1, -1, -1): d += 1 - a[i] e += d * a[i] print(min(c, e))
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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP NUMBER VAR VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
a = int(input()) z = list(map(int, input().split())) ans = [z[0]] for i in range(1, len(z)): ans.append(ans[-1] + z[i]) score = 0 for i in range(len(z)): if z[i] == 0: score += ans[i] print(score)
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 VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) cnt0 = [0] * n for i in range(n): if a[i] == 0: cnt0[i] += 1 for i in range(n - 1)[::-1]: cnt0[i] += cnt0[i + 1] ans = 0 for i in range(n): if a[i] == 1: ans += cnt0[i] print(ans)
IMPORT 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 FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
import sys def solve(cows): n = len(cows) num_vis = [0] * n facing = [0] * n left_inds = [] right_inds = [] for i in range(n): if cows[i] == 0: left_inds.append(i) num_vis[i] = max(i, 0) facing[i] = len(right_inds) else: right_inds.append(i) num_vis[i] = max(n - i - 1, 0) nleft = 0 for i in range(n - 1, -1, -1): if cows[i] == 1: facing[i] = nleft else: nleft += 1 milked_left_facing, milked_right_facing, tot_lost = 0, 0, 0 while milked_left_facing + milked_right_facing < n: if len(right_inds) == 0 or len(left_inds) == 0: break if ( num_vis[right_inds[0]] - milked_right_facing > num_vis[left_inds[-1]] - milked_left_facing ): ri = right_inds.pop(0) milked_right_facing += 1 milk_lost = max(facing[ri] - milked_left_facing, 0) tot_lost += milk_lost if len(right_inds) == 0: break else: li = left_inds.pop(-1) milked_left_facing += 1 milk_lost = max(facing[li] - milked_right_facing, 0) tot_lost += milk_lost if len(left_inds) == 0: break return tot_lost IN = [x.strip() for x in sys.stdin.readlines()] Q = int(IN[0]) cows = [int(x) for x in IN[1].split(" ")] print(solve(cows))
IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
n = input() a = [int(x) for x in input().split()] ans = sum = 0 for i in a: if i == 1: sum += 1 else: ans += sum print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
n = int(input()) lst = list(map(int, input().split())) dp_0 = [(0) for i in range(n)] dp_1 = [(0) for i in range(n)] count_1 = 0 for i in range(n): dp_1[i] = count_1 count_1 += lst[i] count_0 = 0 for i in range(n - 1, -1, -1): dp_0[i] = count_0 if lst[i] == 0: count_0 += 1 num_0 = 0 num_1 = 0 total_0 = 0 total_1 = 0 for i in range(n): if lst[i] == 1: total_1 = total_1 + dp_0[i] + dp_1[i] total_1 = total_1 - (n - i - 1 - dp_0[i]) else: total_0 = total_0 + dp_0[i] + dp_1[i] total_0 = total_0 - (i - dp_1[i]) print(min(total_1, total_0))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
n = int(input()) nm = [0] + list(map(int, input().split())) + [-1] sm = [0] * len(nm) for i in range(n + 1, 0, -1): if not nm[i]: sm[i - 1] = sm[i] + 1 else: sm[i - 1] = sm[i] ans = 0 for i in range(1, n + 1): if nm[i]: ans += sm[i] print(str(ans))
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 LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
ans = 0 cnt = 0 N = input() t = input().split() for i in t: if int(i) == 1: cnt += 1 else: ans += cnt print(ans)
ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
import sys n = int(input()) L = list(map(int, sys.stdin.readline().split())) z = L.count(0) if z == n or z == 0: print(0) Zeros = [0] * n Zeros[n - 1] += 1 - L[n - 1] for i in range(n - 2, -1, -1): Zeros[i] = Zeros[i + 1] if L[i] == 0: Zeros[i] += 1 else: Zeros = [0] * n Zeros[n - 1] += 1 - L[n - 1] for i in range(n - 2, -1, -1): Zeros[i] = Zeros[i + 1] if L[i] == 0: Zeros[i] += 1 Ans = 0 o = 0 z = 0 p = L[0] if L[0] == 1: o += 1 for i in range(1, n): if L[i] == p: if p == 1: o += 1 elif L[i] == 0: Ans += Zeros[i] * o p = 0 else: o = 1 p = 1 print(Ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
def solve(): n = int(input()) arr = list(map(int, input().split())) count_arr = [0] * n zero_count = 0 for _ in range(n): if arr[n - _ - 1] == 0: zero_count += 1 count_arr[n - _ - 1] = zero_count ans = 0 for _ in range(n): if arr[_] == 1: ans += count_arr[_] print(ans) 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 BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
from sys import stdin, stdout nmbr = lambda: int(stdin.readline()) lst = lambda: list(map(int, stdin.readline().split())) for _ in range(1): n = nmbr() a = lst() sm = 0 looking_l = [0] * (1 + n) for i in range(n - 1, -1, -1): if a[i] == 0: looking_l[i] = 1 + looking_l[i + 1] else: looking_l[i] = looking_l[i + 1] for i in range(n): if a[i] == 1: sm += looking_l[i] print(sm)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. -----Input----- The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a_1, a_2, ..., a_{n}, where a_{i} is 0 if the cow number i is facing left, and 1 if it is facing right. -----Output----- Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. -----Examples----- Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 -----Note----- In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
import sys try: sys.stdin = open("input.txt", "r") sys.stdout = open("output.txt", "w") except: pass input = sys.stdin.readline t = 1 while t: t -= 1 n = int(input()) a = list(map(int, input().split())) zero, one = 0, 0 z, o = 0, 0 for i in range(n): if a[i] == 0: zero += z else: z += 1 for i in range(n - 1, -1, -1): if a[i] == 1: one += o else: o += 1 print(min(zero, one))
IMPORT ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. -----Input----- The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 ≀ a_{i} ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct. -----Output----- Output a single integer β€” the number of threads that surely contain a new message. -----Examples----- Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 -----Note----- In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
n = int(input()) a = list(map(int, input().split())) no = n - 1 minm = a[-1] for i in range(n - 2, -1, -1): if a[i] <= minm: no -= 1 minm = a[i] else: break print(no)
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 VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. -----Input----- The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 ≀ a_{i} ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct. -----Output----- Output a single integer β€” the number of threads that surely contain a new message. -----Examples----- Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 -----Note----- In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
n = int(input()) temp = 0 c = 0 a = input().split() for i in range(0, n): if temp - int(a[i]) > 0: c = i + 1 temp = int(a[i]) if c != 0: print(c - 1) else: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. -----Input----- The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 ≀ a_{i} ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct. -----Output----- Output a single integer β€” the number of threads that surely contain a new message. -----Examples----- Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 -----Note----- In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
n = int(input()) threads = list(map(int, input().split())) count = 0 for i in range(n - 1): if threads[i] > threads[i + 1]: count = i + 1 print(count)
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 IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. -----Input----- The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 ≀ a_{i} ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct. -----Output----- Output a single integer β€” the number of threads that surely contain a new message. -----Examples----- Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 -----Note----- In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
n = int(input()) old_pos = list(map(int, input().split())) real_pos = list(reversed(old_pos)) flag = True for i in range(1, n): if real_pos[i] > real_pos[i - 1]: print(n - i) flag = False break if flag == True: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. -----Input----- The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 ≀ a_{i} ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct. -----Output----- Output a single integer β€” the number of threads that surely contain a new message. -----Examples----- Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 -----Note----- In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
nr_threads = int(input()) old_position = list(map(int, input().split())) ans = nr_threads - 1 if nr_threads == 1: print(0) quit() i = ans while i > 0: if old_position[i - 1] > old_position[i]: print(ans) quit() else: i -= 1 ans -= 1 print(ans)
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 VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. -----Input----- The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 ≀ a_{i} ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct. -----Output----- Output a single integer β€” the number of threads that surely contain a new message. -----Examples----- Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 -----Note----- In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
n = int(input()) a = str(input()).split(" ") ma = 0 output = 0 for i in range(n - 1): if int(a[i]) < int(a[i + 1]): ma += 1 else: output += 1 + ma ma = 0 print(output)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. -----Input----- The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 ≀ a_{i} ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct. -----Output----- Output a single integer β€” the number of threads that surely contain a new message. -----Examples----- Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 -----Note----- In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
n = int(input()) x = [int(i) for i in input().split(" ")] i = n - 1 while i > 0: if x[i] < x[i - 1]: break i -= 1 print(i)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. -----Input----- The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 ≀ a_{i} ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct. -----Output----- Output a single integer β€” the number of threads that surely contain a new message. -----Examples----- Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 -----Note----- In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
__author__ = "asmn" n = int(input()) a = list(reversed(list(map(int, input().split())))) for i in range(1, len(a)): if a[i] > a[i - 1]: print(len(a) - i) break else: print(0)
ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. -----Input----- The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 ≀ a_{i} ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct. -----Output----- Output a single integer β€” the number of threads that surely contain a new message. -----Examples----- Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 -----Note----- In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
import sys n = int(sys.stdin.readline()) l = list(map(int, sys.stdin.readline().split()))[::-1] ans = 0 acc = n pre = 9999999999999 for i in range(n): if l[i] < pre: ans += 1 pre = l[i] else: break print(n - ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. -----Input----- The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 ≀ a_{i} ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct. -----Output----- Output a single integer β€” the number of threads that surely contain a new message. -----Examples----- Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 -----Note----- In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
def main(): n = int(input()) aa = list(map(int, input().split())) m = aa[-1] for i in range(n - 2, -2, -1): a = aa[i] if m < a: break m = a print(i + 1) 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 VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. -----Input----- The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 ≀ a_{i} ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct. -----Output----- Output a single integer β€” the number of threads that surely contain a new message. -----Examples----- Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 -----Note----- In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
n = int(input()) t = [int(i) for i in input().split()] last_new = -1 for i in range(len(t)): if i == len(t) - 1: break if t[i] > t[i + 1]: last_new = i print(last_new + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. -----Input----- The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 ≀ a_{i} ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct. -----Output----- Output a single integer β€” the number of threads that surely contain a new message. -----Examples----- Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 -----Note----- In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
from sys import stdin inp = stdin.readline n = int(inp()) a = list(map(int, inp().split())) ans = 0 cur_max = a[0] for i in range(n - 1, 0, -1): if a[i] < a[i - 1]: print(i) exit() print(0)
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 NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. -----Input----- The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 ≀ a_{i} ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct. -----Output----- Output a single integer β€” the number of threads that surely contain a new message. -----Examples----- Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 -----Note----- In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
n = int(input()) a = [int(x) for x in input().split()] answer = 0 last = a[-1] for v in reversed(a): if v > last: break answer += 1 last = v print(len(a) - answer)
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 VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. -----Input----- The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 ≀ a_{i} ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct. -----Output----- Output a single integer β€” the number of threads that surely contain a new message. -----Examples----- Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 -----Note----- In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
R = input I = lambda: map(int, R().split()) n = int(R()) a = list(I())[::-1] + [999999] for i in range(n): if a[i + 1] > a[i]: print(n - i - 1) break
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. -----Input----- The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 ≀ a_{i} ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct. -----Output----- Output a single integer β€” the number of threads that surely contain a new message. -----Examples----- Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 -----Note----- In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
def ordem_crescente(lista, n, i, crescente): while i < n - 1: if int(lista[i]) > int(lista[i + 1]): crescente[0] = i - 1 return False i += 1 return True n = int(input()) fim = input().split(" ") count = 0 i = 0 crescente = [-1] while i < n: if i >= crescente[0]: num = int(fim[i]) if i + 1 < num: count += 1 elif i + 1 == num: if not ordem_crescente(fim, n, i, crescente): count += 1 else: break elif not ordem_crescente(fim, n, i, crescente): count += 1 else: break else: count += 1 i += 1 print(count)
FUNC_DEF WHILE VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. -----Input----- The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 ≀ a_{i} ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct. -----Output----- Output a single integer β€” the number of threads that surely contain a new message. -----Examples----- Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 -----Note----- In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
n = int(input()) l = [int(x) for x in input().split()] l.reverse() for i in range(1, n): if l[i] > l[i - 1]: print(n - i) exit(0) print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. -----Input----- The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 ≀ a_{i} ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct. -----Output----- Output a single integer β€” the number of threads that surely contain a new message. -----Examples----- Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 -----Note----- In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
def main(): n = int(input()) threads = [int(i) for i in input().split()] i = len(threads) - 1 while i > 0: if threads[i] < threads[i - 1]: print(i) return i -= 1 print(0) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. -----Input----- The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 ≀ a_{i} ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct. -----Output----- Output a single integer β€” the number of threads that surely contain a new message. -----Examples----- Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 -----Note----- In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
import sys input = sys.stdin.readline n = int(input()) a = list(map(int, input().split())) for i in range(n - 2, -1, -1): if a[i + 1] < a[i]: print(i + 1) break else: print(0)
IMPORT 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 FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. -----Input----- The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 ≀ a_{i} ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct. -----Output----- Output a single integer β€” the number of threads that surely contain a new message. -----Examples----- Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 -----Note----- In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
n = input() arr = input().split(" ") asc = 1 x = int(arr[0]) for i in range(1, int(n)): if int(arr[i]) > x: asc += 1 else: asc = 1 x = int(arr[i]) print(int(n) - asc)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. -----Input----- The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 ≀ a_{i} ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct. -----Output----- Output a single integer β€” the number of threads that surely contain a new message. -----Examples----- Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 -----Note----- In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
n = int(input()) inStr = input() pos = [int(x) for x in inStr.split()] min = n ans = 0 for i in range(n - 1, -1, -1): if pos[i] > min: ans = i + 1 break elif pos[i] < min: min = pos[i] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. -----Input----- The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 ≀ a_{i} ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct. -----Output----- Output a single integer β€” the number of threads that surely contain a new message. -----Examples----- Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 -----Note----- In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
n = int(input("")) a = [int(x) for x in input("").split(" ")] output = 0 cache = 0 for x in range(n - 1): if a[x] > a[x + 1]: output += 1 + cache cache = 0 else: cache += 1 print(output)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING STRING ASSIGN 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 NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. -----Input----- The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 ≀ a_{i} ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct. -----Output----- Output a single integer β€” the number of threads that surely contain a new message. -----Examples----- Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 -----Note----- In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
n = int(input()) aux = input().split() first = int(aux[0]) flag = 1 for i in range(1, n): if int(aux[i]) < first: flag = 1 else: flag += 1 first = int(aux[i]) print(n - flag)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. -----Input----- The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 ≀ a_{i} ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct. -----Output----- Output a single integer β€” the number of threads that surely contain a new message. -----Examples----- Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 -----Note----- In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
n = int(input()) threads = [int(i) for i in input().split()] min = threads[n - 1] new = 0 for i in range(2, n + 1): if threads[n - i] > min: new = n - i + 1 break else: min = threads[n - i] print(new)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. -----Input----- The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 ≀ a_{i} ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct. -----Output----- Output a single integer β€” the number of threads that surely contain a new message. -----Examples----- Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 -----Note----- In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
n = int(input()) arr = list(map(int, input().split())) temp, count = 1000000, 0 for i in reversed(arr): if i < temp: temp = i count += 1 if i > temp: break print(n - count)
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 FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. -----Input----- The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 ≀ a_{i} ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct. -----Output----- Output a single integer β€” the number of threads that surely contain a new message. -----Examples----- Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 -----Note----- In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
n, ar = int(input()), [int(x) for x in input().split()][::-1] ans = 0 for i in range(1, n): if ar[i] > ar[i - 1]: ans = n - i break print(ans)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL 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 EXPR FUNC_CALL VAR VAR
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. -----Input----- The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 ≀ a_{i} ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct. -----Output----- Output a single integer β€” the number of threads that surely contain a new message. -----Examples----- Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 -----Note----- In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
noth = int(input()) oldpos = [int(num) for num in input().split()] newpos = [(i + 1) for i in range(noth)] counter = 0 if oldpos != newpos: for i in reversed(range(1, noth)): if oldpos[i - 1] > oldpos[i]: counter = i break print(counter)
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 NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. -----Input----- The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 ≀ a_{i} ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct. -----Output----- Output a single integer β€” the number of threads that surely contain a new message. -----Examples----- Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 -----Note----- In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
input() t = [int(x) for x in input().split()] m = [True] + [(t[i] > t[i + 1]) for i in range(len(t) - 1)] print(len(m) - 1 - list(reversed(m)).index(True))
EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. -----Input----- The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 ≀ a_{i} ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct. -----Output----- Output a single integer β€” the number of threads that surely contain a new message. -----Examples----- Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 -----Note----- In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
n = int(input()) lista = input().split() cont = 0 i = n - 1 if n == 1: print("0") else: while i > 0: if int(lista[i]) > int(lista[i - 1]): cont += 1 i -= 1 if i == 0: cont += 1 else: cont += 1 break print(n - cont)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING WHILE VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Emuskald is addicted to Codeforces, and keeps refreshing the main page not to miss any changes in the "recent actions" list. He likes to read thread conversations where each thread consists of multiple messages. Recent actions shows a list of n different threads ordered by the time of the latest message in the thread. When a new message is posted in a thread that thread jumps on the top of the list. No two messages of different threads are ever posted at the same time. Emuskald has just finished reading all his opened threads and refreshes the main page for some more messages to feed his addiction. He notices that no new threads have appeared in the list and at the i-th place in the list there is a thread that was at the a_{i}-th place before the refresh. He doesn't want to waste any time reading old messages so he wants to open only threads with new messages. Help Emuskald find out the number of threads that surely have new messages. A thread x surely has a new message if there is no such sequence of thread updates (posting messages) that both conditions hold: thread x is not updated (it has no new messages); the list order 1, 2, ..., n changes to a_1, a_2, ..., a_{n}. -----Input----- The first line of input contains an integer n, the number of threads (1 ≀ n ≀ 10^5). The next line contains a list of n space-separated integers a_1, a_2, ..., a_{n} where a_{i} (1 ≀ a_{i} ≀ n) is the old position of the i-th thread in the new list. It is guaranteed that all of the a_{i} are distinct. -----Output----- Output a single integer β€” the number of threads that surely contain a new message. -----Examples----- Input 5 5 2 1 3 4 Output 2 Input 3 1 2 3 Output 0 Input 4 4 3 2 1 Output 3 -----Note----- In the first test case, threads 2 and 5 are placed before the thread 1, so these threads must contain new messages. Threads 1, 3 and 4 may contain no new messages, if only threads 2 and 5 have new messages. In the second test case, there may be no new messages at all, since the thread order hasn't changed. In the third test case, only thread 1 can contain no new messages.
n = int(input()) l = [int(i) for i in input().split()] ans = 0 f = 0 for i in range(n - 1, 0, -1): if l[i] < l[i - 1]: print(i) f = 1 break if not f: print(f)
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 NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR
​A 1-indexed array is called *positive* if every element of the array is greater than or equal to the index on which it lies. Formally, an array B of size M is called positive if B_{i} β‰₯ i for each 1≀ i ≀ M. For example, the arrays [1], [2, 2], [3, 2, 4, 4] are positive while the arrays [2, 1], [3, 1, 2] are not positive. You are given an array A containing N integers. You want to distribute all elements of the array into some positiveΒ arrays. The elementsΒ of a positive array might not occur in the order they appear in the array A. Find the minimum number of positive arrays that the elements of the array A can be divided into. Please see the sample cases below for more clarity. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines of input. - The first line of each test case contains an integer N β€” the number of elements in the array A. - The next line contains N space-separated integers A_{1}, A_{2},\ldots, A_{N} β€” the elements of array A. ------ Output Format ------ For each test case, output on a new line the minimum number of positive arrays that the elements of the array A can be divided into. ​ ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $1 ≀ A_{i} ≀ N$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 5 3 2 3 3 4 3 1 1 2 3 1 1 1 5 1 2 2 4 5 6 3 2 1 2 2 1 ----- Sample Output 1 ------ 1 2 3 2 3 ----- explanation 1 ------ Test case $1$: The given array is already positive. So the optimal division is $[2,3,3]$. Test case $2$: One possible division is $[1, 2], [1, 3]$. Test case $3$: The only possible division is $[1],[1],[1]$. Test case $4$: One possible division is $[1, 2, 5], [2, 4]$. Test case $5$: One possible division is $[1, 2, 3], [1], [2,2]$.
test = int(input()) while test: test -= 1 val = int(input()) A = input() A = list(map(int, A.split(" "))) maxIndex = max(max(A) + 1, val + 1) newA = [0] * maxIndex for i in A: newA[i] += 1 sumx = 0 k = 0 for i in range(1, val + 1): sumx += newA[i] k = max(k, (sumx + i - 1) // i) print(k)
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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
​A 1-indexed array is called *positive* if every element of the array is greater than or equal to the index on which it lies. Formally, an array B of size M is called positive if B_{i} β‰₯ i for each 1≀ i ≀ M. For example, the arrays [1], [2, 2], [3, 2, 4, 4] are positive while the arrays [2, 1], [3, 1, 2] are not positive. You are given an array A containing N integers. You want to distribute all elements of the array into some positiveΒ arrays. The elementsΒ of a positive array might not occur in the order they appear in the array A. Find the minimum number of positive arrays that the elements of the array A can be divided into. Please see the sample cases below for more clarity. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines of input. - The first line of each test case contains an integer N β€” the number of elements in the array A. - The next line contains N space-separated integers A_{1}, A_{2},\ldots, A_{N} β€” the elements of array A. ------ Output Format ------ For each test case, output on a new line the minimum number of positive arrays that the elements of the array A can be divided into. ​ ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $1 ≀ A_{i} ≀ N$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 5 3 2 3 3 4 3 1 1 2 3 1 1 1 5 1 2 2 4 5 6 3 2 1 2 2 1 ----- Sample Output 1 ------ 1 2 3 2 3 ----- explanation 1 ------ Test case $1$: The given array is already positive. So the optimal division is $[2,3,3]$. Test case $2$: One possible division is $[1, 2], [1, 3]$. Test case $3$: The only possible division is $[1],[1],[1]$. Test case $4$: One possible division is $[1, 2, 5], [2, 4]$. Test case $5$: One possible division is $[1, 2, 3], [1], [2,2]$.
for _ in range(int(input())): sizeOfInput = int(input()) tempList = list(map(int, input().split())) dict = {} for x in tempList: dict[x] = dict.get(x, 0) + 1 sortedDict = {} for key in sorted(dict.keys()): sortedDict[key] = dict[key] AnswerList = [] numberOfAnswerLists = 0 for element in list(sortedDict.keys()): nextList = 0 for i in range(1, sortedDict[element] + 1): if numberOfAnswerLists == 0: AnswerList.append([element]) numberOfAnswerLists += 1 else: inserted = False for lists in range(nextList, numberOfAnswerLists): if element >= len(AnswerList[lists]) + 1: AnswerList[lists].append(element) nextList = lists inserted = True break if inserted == False: AnswerList.append([element]) nextList = numberOfAnswerLists numberOfAnswerLists += 1 print(numberOfAnswerLists)
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 DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
​A 1-indexed array is called *positive* if every element of the array is greater than or equal to the index on which it lies. Formally, an array B of size M is called positive if B_{i} β‰₯ i for each 1≀ i ≀ M. For example, the arrays [1], [2, 2], [3, 2, 4, 4] are positive while the arrays [2, 1], [3, 1, 2] are not positive. You are given an array A containing N integers. You want to distribute all elements of the array into some positiveΒ arrays. The elementsΒ of a positive array might not occur in the order they appear in the array A. Find the minimum number of positive arrays that the elements of the array A can be divided into. Please see the sample cases below for more clarity. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines of input. - The first line of each test case contains an integer N β€” the number of elements in the array A. - The next line contains N space-separated integers A_{1}, A_{2},\ldots, A_{N} β€” the elements of array A. ------ Output Format ------ For each test case, output on a new line the minimum number of positive arrays that the elements of the array A can be divided into. ​ ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $1 ≀ A_{i} ≀ N$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 5 3 2 3 3 4 3 1 1 2 3 1 1 1 5 1 2 2 4 5 6 3 2 1 2 2 1 ----- Sample Output 1 ------ 1 2 3 2 3 ----- explanation 1 ------ Test case $1$: The given array is already positive. So the optimal division is $[2,3,3]$. Test case $2$: One possible division is $[1, 2], [1, 3]$. Test case $3$: The only possible division is $[1],[1],[1]$. Test case $4$: One possible division is $[1, 2, 5], [2, 4]$. Test case $5$: One possible division is $[1, 2, 3], [1], [2,2]$.
t = int(input()) for i in range(t): n = int(input()) A = [int(x) for x in input().split()] A.sort() count = 1 B = [] for i in range(n - 1): if A[i] != A[i + 1]: if (i + 1) % A[i] == 0: B.append((i + 1) // A[i]) else: B.append((i + 1) // A[i] + 1) if n % A[n - 1] == 0: B.append(n // A[n - 1]) else: B.append(n // A[n - 1] + 1) print(max(B))
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 EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
​A 1-indexed array is called *positive* if every element of the array is greater than or equal to the index on which it lies. Formally, an array B of size M is called positive if B_{i} β‰₯ i for each 1≀ i ≀ M. For example, the arrays [1], [2, 2], [3, 2, 4, 4] are positive while the arrays [2, 1], [3, 1, 2] are not positive. You are given an array A containing N integers. You want to distribute all elements of the array into some positiveΒ arrays. The elementsΒ of a positive array might not occur in the order they appear in the array A. Find the minimum number of positive arrays that the elements of the array A can be divided into. Please see the sample cases below for more clarity. ------ Input Format ------ - The first line of input will contain a single integer T, denoting the number of test cases. - Each test case consists of two lines of input. - The first line of each test case contains an integer N β€” the number of elements in the array A. - The next line contains N space-separated integers A_{1}, A_{2},\ldots, A_{N} β€” the elements of array A. ------ Output Format ------ For each test case, output on a new line the minimum number of positive arrays that the elements of the array A can be divided into. ​ ------ Constraints ------ $1 ≀ T ≀ 10^{4}$ $1 ≀ N ≀ 10^{5}$ $1 ≀ A_{i} ≀ N$ - The sum of $N$ over all test cases won't exceed $2\cdot 10^{5}$. ----- Sample Input 1 ------ 5 3 2 3 3 4 3 1 1 2 3 1 1 1 5 1 2 2 4 5 6 3 2 1 2 2 1 ----- Sample Output 1 ------ 1 2 3 2 3 ----- explanation 1 ------ Test case $1$: The given array is already positive. So the optimal division is $[2,3,3]$. Test case $2$: One possible division is $[1, 2], [1, 3]$. Test case $3$: The only possible division is $[1],[1],[1]$. Test case $4$: One possible division is $[1, 2, 5], [2, 4]$. Test case $5$: One possible division is $[1, 2, 3], [1], [2,2]$.
for i in range(int(input())): N = int(input()) A = list(map(int, input().split())) A.sort() print(max([(j // A[j] + 1) for j in range(N)]))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR
Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example 1: Input: "bcabc" Output: "abc" Example 2: Input: "cbacdcbc" Output: "acdb"
class Solution: def removeDuplicateLetters(self, s): if len(s) == 0: return "" count = collections.Counter(s) stack = [] visited = set() for c in s: count[c] -= 1 if c in visited: continue visited.add(c) while stack and stack[-1] > c and count[stack[-1]] > 0: visited.remove(stack[-1]) stack.pop() stack.append(c) return "".join(stack)
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL STRING VAR
Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example 1: Input: "bcabc" Output: "abc" Example 2: Input: "cbacdcbc" Output: "acdb"
class Solution: def removeDuplicateLetters(self, s): ret = "" while s: p = min(s.rindex(c) for c in set(s)) c = min(s[: p + 1]) ret += c s = s[s.index(c) :].replace(c, "") return ret
CLASS_DEF FUNC_DEF ASSIGN VAR STRING WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING RETURN VAR
Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example 1: Input: "bcabc" Output: "abc" Example 2: Input: "cbacdcbc" Output: "acdb"
class Solution: def removeDuplicateLetters(self, s): rindex = {c: i for i, c in enumerate(s)} result = "" for i, c in enumerate(s): if c not in result: while c < result[-1:] and i < rindex[result[-1]]: result = result[:-1] result += c return result
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR WHILE VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR RETURN VAR
Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example 1: Input: "bcabc" Output: "abc" Example 2: Input: "cbacdcbc" Output: "acdb"
class Solution: def removeDuplicateLetters(self, s): chars = set(s) for c in sorted(chars): suffix = s[s.index(c) :] if set(suffix) == chars: return c + self.removeDuplicateLetters(suffix.replace(c, "")) return ""
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING RETURN STRING
Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example 1: Input: "bcabc" Output: "abc" Example 2: Input: "cbacdcbc" Output: "acdb"
class Solution: def removeDuplicateLetters(self, s): for c in sorted(list(set(s))): substr = s[s.index(c) :] if set(substr) == set(s): return c + self.removeDuplicateLetters(substr.replace(c, "")) return s
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING RETURN VAR
Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example 1: Input: "bcabc" Output: "abc" Example 2: Input: "cbacdcbc" Output: "acdb"
class Solution: def removeDuplicateLetters(self, s): if not s: return "" res = "" sorted_set = sorted(set(s)) for c in sorted_set: sub_s = s[s.index(c) :] if set(s) == set(sub_s): return c + self.removeDuplicateLetters(sub_s.replace(c, "")) return res
CLASS_DEF FUNC_DEF IF VAR RETURN STRING ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING RETURN VAR
Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example 1: Input: "bcabc" Output: "abc" Example 2: Input: "cbacdcbc" Output: "acdb"
class Solution: def removeDuplicateLetters(self, s): counter = collections.Counter(s) added = set() ans = [] for c in s: while ( len(ans) > 0 and c < ans[-1] and counter[ans[-1]] > 0 and c not in added ): added.remove(ans.pop()) counter[c] -= 1 if c not in added: ans.append(c) added.add(c) return "".join(ans)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL STRING VAR
Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. Example 1: Input: "bcabc" Output: "abc" Example 2: Input: "cbacdcbc" Output: "acdb"
class Solution: def removeDuplicateLetters(self, s): counts = {} for c in s: if c not in counts: counts[c] = 0 counts[c] += 1 stack = [] visited = set() for c in s: counts[c] -= 1 if c in visited: continue while stack and stack[-1] > c and counts[stack[-1]] > 0: visited.remove(stack.pop()) stack.append(c) visited.add(c) return "".join(stack)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR NUMBER IF VAR VAR WHILE VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL STRING VAR
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. Formally the function should: Return true if there exists i, j, k such that arr[i] < arr[j] < arr[k] given 0 ≀ i < j < k ≀ n-1 else return false. Your algorithm should run in O(n) time complexity and O(1) space complexity. Examples: Given [1, 2, 3, 4, 5], return true. Given [5, 4, 3, 2, 1], return false. Credits:Special thanks to @DjangoUnchained for adding this problem and creating all test cases.
class Solution: def increasingTriplet(self, nums): if len(nums) < 3: return False tails = [-float("inf")] + [float("inf")] * 3 for x in nums: for i in range(2, -1, -1): if x > tails[i]: tails[i + 1] = x break if tails[-1] < float("inf"): return True return False
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP LIST FUNC_CALL VAR STRING NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER FUNC_CALL VAR STRING RETURN NUMBER RETURN NUMBER
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. Formally the function should: Return true if there exists i, j, k such that arr[i] < arr[j] < arr[k] given 0 ≀ i < j < k ≀ n-1 else return false. Your algorithm should run in O(n) time complexity and O(1) space complexity. Examples: Given [1, 2, 3, 4, 5], return true. Given [5, 4, 3, 2, 1], return false. Credits:Special thanks to @DjangoUnchained for adding this problem and creating all test cases.
class Solution: def increasingTriplet(self, nums): n = len(nums) if n < 3: return False forward = [nums[0]] * n backward = [nums[-1]] * n for i in range(1, n): forward[i] = min(forward[i - 1], nums[i]) for i in range(n - 2, -1, -1): backward[i] = max(backward[i + 1], nums[i]) for i in range(n): if forward[i] < nums[i] < backward[i]: return True return False
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST VAR NUMBER VAR ASSIGN VAR BIN_OP LIST VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. Formally the function should: Return true if there exists i, j, k such that arr[i] < arr[j] < arr[k] given 0 ≀ i < j < k ≀ n-1 else return false. Your algorithm should run in O(n) time complexity and O(1) space complexity. Examples: Given [1, 2, 3, 4, 5], return true. Given [5, 4, 3, 2, 1], return false. Credits:Special thanks to @DjangoUnchained for adding this problem and creating all test cases.
class Solution: def increasingTriplet(self, nums): x = y = float("inf") for n in nums: if n <= x: x = n elif n <= y: y = n else: return True return False
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR STRING FOR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN NUMBER RETURN NUMBER
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. Formally the function should: Return true if there exists i, j, k such that arr[i] < arr[j] < arr[k] given 0 ≀ i < j < k ≀ n-1 else return false. Your algorithm should run in O(n) time complexity and O(1) space complexity. Examples: Given [1, 2, 3, 4, 5], return true. Given [5, 4, 3, 2, 1], return false. Credits:Special thanks to @DjangoUnchained for adding this problem and creating all test cases.
class Solution: def increasingTriplet(self, nums): if len(nums) == 0: n = 1 else: n = len(nums) index_of_A = 0 index_of_B = n - 1 A_indices = [None] * n B_indices = [None] * n for i in range(2, n): if nums[i - 1] <= nums[index_of_A]: index_of_A = i - 1 A_indices[i - 1] = None else: A_indices[i - 1] = index_of_A if nums[n - i] >= nums[index_of_B]: index_of_B = n - i B_indices[n - i] = None else: B_indices[n - i] = index_of_B for i in range(0, n): if A_indices[i] != None and B_indices[i] != None: return True return False
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NONE ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR NONE ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NONE VAR VAR NONE RETURN NUMBER RETURN NUMBER
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. Formally the function should: Return true if there exists i, j, k such that arr[i] < arr[j] < arr[k] given 0 ≀ i < j < k ≀ n-1 else return false. Your algorithm should run in O(n) time complexity and O(1) space complexity. Examples: Given [1, 2, 3, 4, 5], return true. Given [5, 4, 3, 2, 1], return false. Credits:Special thanks to @DjangoUnchained for adding this problem and creating all test cases.
class Solution: def increasingTriplet(self, nums): tails = [-float("inf")] for x in nums: for i in range(len(tails) - 1, -1, -1): if x > tails[i]: if i == len(tails) - 1: tails.append(x) else: tails[i + 1] = x break if len(tails) == 4: return True return False
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_CALL VAR STRING FOR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. Formally the function should: Return true if there exists i, j, k such that arr[i] < arr[j] < arr[k] given 0 ≀ i < j < k ≀ n-1 else return false. Your algorithm should run in O(n) time complexity and O(1) space complexity. Examples: Given [1, 2, 3, 4, 5], return true. Given [5, 4, 3, 2, 1], return false. Credits:Special thanks to @DjangoUnchained for adding this problem and creating all test cases.
class Solution: def increasingTriplet(self, nums): if len(nums) < 3: return False tails = [-2147483647, nums[0]] for n in nums[1:]: for i in range(len(tails) - 1, -1, -1): if n > tails[i]: if len(tails) <= i + 1: tails.append(n) elif n < tails[i + 1]: tails[i + 1] = n if len(tails) == 4: return True print(tails) return False
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER VAR NUMBER FOR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. Formally the function should: Return true if there exists i, j, k such that arr[i] < arr[j] < arr[k] given 0 ≀ i < j < k ≀ n-1 else return false. Your algorithm should run in O(n) time complexity and O(1) space complexity. Examples: Given [1, 2, 3, 4, 5], return true. Given [5, 4, 3, 2, 1], return false. Credits:Special thanks to @DjangoUnchained for adding this problem and creating all test cases.
class Solution: def increasingTriplet(self, nums): a = b = None for i in nums: if a is None or a >= i: a = i elif b is None or b >= i: b = i else: return True return False
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NONE FOR VAR VAR IF VAR NONE VAR VAR ASSIGN VAR VAR IF VAR NONE VAR VAR ASSIGN VAR VAR RETURN NUMBER RETURN NUMBER
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. Formally the function should: Return true if there exists i, j, k such that arr[i] < arr[j] < arr[k] given 0 ≀ i < j < k ≀ n-1 else return false. Your algorithm should run in O(n) time complexity and O(1) space complexity. Examples: Given [1, 2, 3, 4, 5], return true. Given [5, 4, 3, 2, 1], return false. Credits:Special thanks to @DjangoUnchained for adding this problem and creating all test cases.
class Solution: def increasingTriplet(self, nums): a, b = float("inf"), float("inf") for n in nums: if n > b: return True elif a < n < b: b = n elif n < a: a = n return False
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING FOR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN NUMBER
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. Formally the function should: Return true if there exists i, j, k such that arr[i] < arr[j] < arr[k] given 0 ≀ i < j < k ≀ n-1 else return false. Your algorithm should run in O(n) time complexity and O(1) space complexity. Examples: Given [1, 2, 3, 4, 5], return true. Given [5, 4, 3, 2, 1], return false. Credits:Special thanks to @DjangoUnchained for adding this problem and creating all test cases.
class Solution: def increasingTriplet(self, nums): if len(nums) < 3: return False minN = nums[0] minSecond = None for i in range(1, len(nums)): if type(minSecond) == int and nums[i] > minSecond: return True if minN < nums[i]: if type(minSecond) == int and minSecond > nums[i]: minSecond = nums[i] elif not type(minSecond) == int: minSecond = nums[i] minN = min(minN, nums[i]) return False
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NONE FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN NUMBER
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. Formally the function should: Return true if there exists i, j, k such that arr[i] < arr[j] < arr[k] given 0 ≀ i < j < k ≀ n-1 else return false. Your algorithm should run in O(n) time complexity and O(1) space complexity. Examples: Given [1, 2, 3, 4, 5], return true. Given [5, 4, 3, 2, 1], return false. Credits:Special thanks to @DjangoUnchained for adding this problem and creating all test cases.
class Solution: def increasingTriplet(self, nums): if not nums: return False maxv = float("inf") minv = nums[0] for num in nums: if num > maxv: return True if num > minv: maxv = min(num, maxv) minv = min(num, minv) return False
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN NUMBER
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array. Formally the function should: Return true if there exists i, j, k such that arr[i] < arr[j] < arr[k] given 0 ≀ i < j < k ≀ n-1 else return false. Your algorithm should run in O(n) time complexity and O(1) space complexity. Examples: Given [1, 2, 3, 4, 5], return true. Given [5, 4, 3, 2, 1], return false. Credits:Special thanks to @DjangoUnchained for adding this problem and creating all test cases.
class Solution: def increasingTriplet(self, nums): if nums == []: return False min = nums[0] sec_min = 2**31 - 1 for i in nums: if i <= min: min = i elif i <= sec_min: sec_min = i else: return True return False
CLASS_DEF FUNC_DEF IF VAR LIST RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN NUMBER RETURN NUMBER
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. Input The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a1, a2, ..., an, where ai is 0 if the cow number i is facing left, and 1 if it is facing right. Output Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 Note In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
n = int(input()) lis = list(map(int, input().split())) ans = b = 0 for i in range(n - 1, -1, -1): if lis[i] == 1: ans += b else: b += 1 print(ans)
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 NUMBER NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Iahub helps his grandfather at the farm. Today he must milk the cows. There are n cows sitting in a row, numbered from 1 to n from left to right. Each cow is either facing to the left or facing to the right. When Iahub milks a cow, all the cows that see the current cow get scared and lose one unit of the quantity of milk that they can give. A cow facing left sees all the cows with lower indices than her index, and a cow facing right sees all the cows with higher indices than her index. A cow that got scared once can get scared again (and lose one more unit of milk). A cow that has been milked once cannot get scared and lose any more milk. You can assume that a cow never loses all the milk she can give (a cow gives an infinitely amount of milk). Iahub can decide the order in which he milks the cows. But he must milk each cow exactly once. Iahub wants to lose as little milk as possible. Print the minimum amount of milk that is lost. Input The first line contains an integer n (1 ≀ n ≀ 200000). The second line contains n integers a1, a2, ..., an, where ai is 0 if the cow number i is facing left, and 1 if it is facing right. Output Print a single integer, the minimum amount of lost milk. Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 4 0 0 1 0 Output 1 Input 5 1 0 1 0 1 Output 3 Note In the first sample Iahub milks the cows in the following order: cow 3, cow 4, cow 2, cow 1. When he milks cow 3, cow 4 loses 1 unit of milk. After that, no more milk is lost.
def cows(n, lst): zeros, result = 0, 0 for i in range(n - 1, -1, -1): if lst[i] == 0: zeros += 1 else: result += zeros return result m = int(input()) a = [int(j) for j in input().split()] print(cows(m, a))
FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR RETURN 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 FUNC_CALL VAR VAR VAR
Recently Maxim has found an array of n integers, needed by no one. He immediately come up with idea of changing it: he invented positive integer x and decided to add or subtract it from arbitrary array elements. Formally, by applying single operation Maxim chooses integer i (1 ≀ i ≀ n) and replaces the i-th element of array a_{i} either with a_{i} + x or with a_{i} - x. Please note that the operation may be applied more than once to the same position. Maxim is a curious minimalis, thus he wants to know what is the minimum value that the product of all array elements (i.e. $\prod_{i = 1}^{n} a_{i}$) can reach, if Maxim would apply no more than k operations to it. Please help him in that. -----Input----- The first line of the input contains three integers n, k and x (1 ≀ n, k ≀ 200 000, 1 ≀ x ≀ 10^9)Β β€” the number of elements in the array, the maximum number of operations and the number invented by Maxim, respectively. The second line contains n integers a_1, a_2, ..., a_{n} ($|a_{i}|\leq 10^{9}$)Β β€” the elements of the array found by Maxim. -----Output----- Print n integers b_1, b_2, ..., b_{n} in the only lineΒ β€” the array elements after applying no more than k operations to the array. In particular, $a_{i} \equiv b_{i} \operatorname{mod} x$ should stay true for every 1 ≀ i ≀ n, but the product of all array elements should be minimum possible. If there are multiple answers, print any of them. -----Examples----- Input 5 3 1 5 4 3 5 2 Output 5 4 3 5 -1 Input 5 3 1 5 4 3 5 5 Output 5 4 0 5 5 Input 5 3 1 5 4 4 5 5 Output 5 1 4 5 5 Input 3 2 7 5 4 2 Output 5 11 -5
__author__ = "Think" def respond(): if len(arr) > 0: if not arr[0][2]: print(-arr[0][0], end="") else: print(arr[0][0], end="") for i in arr[1:]: if i[2]: print(" " + str(i[0]), end="") else: print(" " + str(-i[0]), end="") n, k, x = [int(i) for i in input().split()] data = [int(i) for i in input().split()] arr = [] parity = 0 for i in range(n): if data[i] < 0: parity += 1 arr.append([abs(data[i]), i, data[i] >= 0]) ordered = sorted(arr, key=lambda n: abs(n[0])) if ordered[0][0] >= k * x and parity % 2 == 0: arr[ordered[0][1]][0] -= k * x respond() else: start = 0 if parity % 2 == 0: start = 1 c = ordered[0][0] ordered[0][0] = x * (c // x + 1) - c k -= c // x + 1 arr[ordered[0][1]][2] = not arr[ordered[0][1]][2] if k > 0: total = 0 prev = 0 broken = False for m in range(len(ordered)): a = ordered[m][0] // x if a == prev: total += a continue prev = a if m * a - total < k: total += a continue else: broken = True break if not broken: m += 1 base = (k + total) // m k -= m * base - total increm = sorted(ordered[:m], key=lambda n: n[0] % x) for i in increm: pos = i[1] if k > 0: arr[pos][0] = (base + 1) * x + arr[pos][0] % x k -= 1 else: arr[pos][0] = base * x + arr[pos][0] % x respond() else: respond()
ASSIGN VAR STRING FUNC_DEF IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER STRING EXPR FUNC_CALL VAR VAR NUMBER NUMBER STRING FOR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER NUMBER NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER VAR VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
def mergesort(l, r, arr, pos): if r - l == 1: return arr, pos m = (l + r) // 2 arr, pos = mergesort(l, m, arr, pos) arr, pos = mergesort(m, r, arr, pos) c = [(0) for i in range(r)] d = [(0) for i in range(r)] poi_a = l poi_b = m for i in range(l, r): if poi_a == m: c[i] = arr[poi_b] d[i] = pos[poi_b] poi_b += 1 elif poi_b == r: c[i] = arr[poi_a] d[i] = pos[poi_a] poi_a += 1 elif a[poi_a] > arr[poi_b]: c[i] = arr[poi_a] d[i] = pos[poi_a] poi_a += 1 else: c[i] = arr[poi_b] d[i] = pos[poi_b] poi_b += 1 for i in range(l, r): arr[i] = c[i] pos[i] = d[i] return arr, pos n = int(input()) a = list(map(int, input().split())) p = [i for i in range(n)] temp = a[:] a, p = mergesort(0, n, a, p) for m in range(int(input())): k, pos = map(int, input().split()) j = k while j < n and a[j - 1] == a[j]: j += 1 i = k - 1 l = 1 while i > 0 and a[i - 1] == a[i]: i -= 1 l += 1 m = sorted(p[i:j]) res = sorted(m[:l] + p[:i]) print(temp[res[pos - 1]])
FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN 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 VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) a = list(map(int, input().split())) d = [] for i in range(n): d.append([a[i], -i]) d.sort(reverse=True) e = [] for i in range(n): e.append([-d[i][1], d[i][0]]) m = int(input()) f = [] for i in range(m): b, c = map(int, input().split()) f = e[:b] f.sort() print(f[c - 1][1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) l = [int(j) for j in input().split()] m = int(input()) d = dict() for i in range(n): if l[i] in d: d[l[i]].append(i) else: d[l[i]] = [i] d = sorted(d.items(), reverse=True) for que in range(m): k, pos = [int(j) for j in input().split()] min_ = [] i = 0 j = 0 while k > 0: k -= 1 min_.append(d[i][1][j]) j += 1 if j == len(d[i][1]): i += 1 j = 0 min_.sort() print(l[min_[pos - 1]])
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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR 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 FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
import sys input = sys.stdin.buffer.readline mx = 2 * 10**5 + 1 bit = [0] * mx def add(idx): idx += 1 while idx < mx: bit[idx] += 1 idx += idx & -idx def lower_bound(val): pos = 0 tot = 0 for i in range(20, -1, -1): if pos + (1 << i) < mx and tot + bit[pos + (1 << i)] < val: tot += bit[pos + (1 << i)] pos += 1 << i return pos n = int(input()) a = list(map(int, input().split())) new_el = sorted(list(range(n)), key=lambda i: (-a[i], i)) m = int(input()) queries = [] answers = [0] * m for i in range(m): k, p = map(int, input().split()) queries.append([k, p, i]) queries.sort() curr_len = 0 for i in range(m): k, pos, query_idx = queries[i] while curr_len < k: add(new_el[curr_len]) curr_len += 1 answers[query_idx] = a[lower_bound(pos)] print(*answers)
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) a = [int(i) for i in input().split()] copy1 = a[:] a.sort() m = int(input()) for i in range(m): k, pos = map(int, input().split()) ans = [-1] copy = a[-k:] for i in copy1: if i in copy: copy.pop(copy.index(i)) ans.append(i) print(ans[pos])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
a = int(input()) b = list(map(int, input().split())) for i in range(a): b[i] = b[i], -i b.sort() k = int(input()) for q in range(k): a = list(map(int, input().split())) l = a[0] p = a[1] print(sorted(b[-l:], key=lambda x: -x[1])[p - 1][0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) a = list(map(int, input().split())) for i in range(n): a[i] = [a[i], n - i] a.sort() c = list(range(n)) for i in range(n): c[n - i - 1] = [n - a[i][1] + 1, a[i][0]] m = int(input()) for g in range(m): k, pos = list(map(int, input().split())) b = c[:k] b.sort() print(b[pos - 1][1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER LIST BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
def test(a, k, pos): r = list(reversed(sorted(a.copy()))) countLast = r[k - 1] s = 0 for i in range(k, len(r)): if r[i] == countLast: s += 1 ans = [] a = list(reversed(a)) for i in range(len(a)): if a[i] > countLast or a[i] == countLast and s == 0: ans.append(a[i]) elif a[i] == countLast: s -= 1 print(list(reversed(ans))[pos - 1]) n = int(input()) a = list(map(int, input().split())) m = int(input()) for i in range(m): k, pos = map(int, input().split()) test(a, k, pos)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) a = list(map(int, input().split())) m = int(input()) res = [] for i in range(m): k, pos = map(int, input().split()) mas1 = [] for j in range(n): mas1.append(a[j]) mas2 = sorted(mas1) mas1.reverse() for j in range(n - k): mas1.remove(mas2[j]) mas1.reverse() res.append(mas1[pos - 1]) for i in range(m): print(res[i])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) sequence = list(map(int, input().split())) array = list(sequence) array.sort(reverse=True) m = int(input()) for i in range(m): k, pos = map(int, input().split()) D = dict() for elem in array[:k]: if elem in D: D[elem] += 1 else: D[elem] = 1 for elem in sequence: if elem in D and D[elem] != 0: D[elem] -= 1 if pos == 1: print(elem) break else: pos -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
m = int(input()) line = [int(i) for i in input().split()] for i in range(int(input())): false_line = list(line) k, pos = map(int, input().split()) while len(false_line) > k: x = min(false_line) rang = iter(range(-1, -len(false_line) - 1, -1)) j = next(rang) while j > -len(false_line) - 1: if false_line[j] == x: del false_line[j] if len(false_line) == k: break else: try: j = next(rang) except StopIteration: break print(false_line[pos - 1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
n = int(input()) a = list(map(int, input().split())) m = int(input()) x = [[a[i], i] for i in range(n)] x.sort(key=lambda x: x[0], reverse=True) for _i in range(m): k, p = map(int, input().split()) print(sorted(x[:k], key=lambda x: x[1])[p - 1][0])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR 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 FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER
This is the easier version of the problem. In this version $1 \le n, m \le 100$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $a=[a_1,a_2,\dots,a_n]$ of length $n$. Its subsequence is obtained by removing zero or more elements from the sequence $a$ (they do not necessarily go consecutively). For example, for the sequence $a=[11,20,11,33,11,20,11]$: $[11,20,11,33,11,20,11]$, $[11,20,11,33,11,20]$, $[11,11,11,11]$, $[20]$, $[33,20]$ are subsequences (these are just some of the long list); $[40]$, $[33,33]$, $[33,20,20]$, $[20,20,11,11]$ are not subsequences. Suppose that an additional non-negative integer $k$ ($1 \le k \le n$) is given, then the subsequence is called optimal if: it has a length of $k$ and the sum of its elements is the maximum possible among all subsequences of length $k$; and among all subsequences of length $k$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $b=[b_1, b_2, \dots, b_k]$ is lexicographically smaller than the sequence $c=[c_1, c_2, \dots, c_k]$ if the first element (from the left) in which they differ less in the sequence $b$ than in $c$. Formally: there exists $t$ ($1 \le t \le k$) such that $b_1=c_1$, $b_2=c_2$, ..., $b_{t-1}=c_{t-1}$ and at the same time $b_t<c_t$. For example: $[10, 20, 20]$ lexicographically less than $[10, 21, 1]$, $[7, 99, 99]$ is lexicographically less than $[10, 21, 1]$, $[10, 21, 0]$ is lexicographically less than $[10, 21, 1]$. You are given a sequence of $a=[a_1,a_2,\dots,a_n]$ and $m$ requests, each consisting of two numbers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$). For each query, print the value that is in the index $pos_j$ of the optimal subsequence of the given sequence $a$ for $k=k_j$. For example, if $n=4$, $a=[10,20,30,20]$, $k_j=2$, then the optimal subsequence is $[20,30]$ β€” it is the minimum lexicographically among all subsequences of length $2$ with the maximum total sum of items. Thus, the answer to the request $k_j=2$, $pos_j=1$ is the number $20$, and the answer to the request $k_j=2$, $pos_j=2$ is the number $30$. -----Input----- The first line contains an integer $n$ ($1 \le n \le 100$) β€” the length of the sequence $a$. The second line contains elements of the sequence $a$: integer numbers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$). The third line contains an integer $m$ ($1 \le m \le 100$) β€” the number of requests. The following $m$ lines contain pairs of integers $k_j$ and $pos_j$ ($1 \le k \le n$, $1 \le pos_j \le k_j$) β€” the requests. -----Output----- Print $m$ integers $r_1, r_2, \dots, r_m$ ($1 \le r_j \le 10^9$) one per line: answers to the requests in the order they appear in the input. The value of $r_j$ should be equal to the value contained in the position $pos_j$ of the optimal subsequence for $k=k_j$. -----Examples----- Input 3 10 20 10 6 1 1 2 1 2 2 3 1 3 2 3 3 Output 20 10 20 10 20 10 Input 7 1 2 1 3 1 2 1 9 2 1 2 2 3 1 3 2 3 3 1 1 7 1 7 7 7 4 Output 2 3 2 3 2 3 1 1 3 -----Note----- In the first example, for $a=[10,20,10]$ the optimal subsequences are: for $k=1$: $[20]$, for $k=2$: $[10,20]$, for $k=3$: $[10,20,10]$.
def min_s(a, k): res = a.copy() for i in range(len(a) - k): m = min(res) res.reverse() res.remove(m) res.reverse() return res n = int(input()) a = [int(x) for x in input().split()] m = int(input()) for _ in range(m): k, pos = [int(x) for x in input().split()] l = min_s(a, k) print(l[pos - 1])
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN 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 FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER