description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset.
Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 β€ i β€ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets.
------ Constraints ------
$1 β€ T β€ 10$
$1 β€ N β€ 10^{5}$
$1 β€ l_{i} β€ r_{i} β€ 10^{9}$ for each valid $i$
------ Subtasks ------
Subtask #1 (10 points): $N = 16$
Subtask #2 (15 points): $N = 1,000$
Subtask #3 (15 points): $r_{i} β€ 10^{6}$ for each valid $i$
Subtask #4 (60 points): original constraints
----- Sample Input 1 ------
3
3
1 4
2 6
5 7
2
1 4
4 6
2
3 7
8 9
----- Sample Output 1 ------
1
-1
0
----- explanation 1 ------
Example case 1: It is enough to delete the range $[2, 6]$.
Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too.
Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset. | t = int(input())
while t > 0:
n = int(input())
arrival = []
depart = []
for i in range(n):
arr = list(map(int, input().split()))
arrival.append(arr[0])
depart.append(arr[1])
arrival.sort()
depart.sort()
merger = [[0, "a"] for c in range(2 * n)]
i = 0
j = 0
count = 0
point = 0
ans = 1000000
while i < len(arrival) and j < len(depart):
if arrival[i] <= depart[j]:
count = count + 1
merger[point][0] = count
point = point + 1
i = i + 1
else:
count = count - 1
merger[point][0] = count
merger[point][1] = "d"
point = point + 1
j = j + 1
while i < len(arrival):
count = count + 1
merger[point][0] = count
point = point + 1
i = i + 1
while j < len(depart):
count = count - 1
merger[point][0] = count
merger[point][1] = "d"
point = point + 1
j = j + 1
flag = False
for a, b in merger:
if b == "a":
if flag:
ans = min(ans, a - 1)
flag = True
else:
flag = True
if ans == 1000000:
print(-1)
else:
print(ans)
t = t - 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER STRING VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR STRING IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER |
Read problem statements in [Hindi], [Bengali], [Mandarin Chinese], [Russian], and [Vietnamese] as well.
Chefina is playing with ranges. There are $N$ ranges $[l_{1}, r_{1}], [l_{2}, r_{2}], \ldots, [l_{N}, r_{N}]$. Chefina wants to divide the ranges into two non-empty subsets in such a way that each range is in exactly one subset and whenever two ranges have a common point, they are in the same subset.
Since this could be difficult to achieve, Chefina can delete any number of ranges (possibly zero) and then divide the remaining ranges into such non-empty subsets. She wants to know whether it is possible to achieve her task after deleting some ranges and if it is possible, she also wants to know the minimum number of ranges she needs to delete to achieve it.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains a single integer $N$.
$N$ lines follow. For each $i$ ($1 β€ i β€ N$), the $i$-th of these lines contains two space-separated integers $l_{i}$ and $r_{i}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum number of ranges to delete, or $-1$ if it is impossible to delete some ranges and divide the remaining ranges into two non-empty subsets.
------ Constraints ------
$1 β€ T β€ 10$
$1 β€ N β€ 10^{5}$
$1 β€ l_{i} β€ r_{i} β€ 10^{9}$ for each valid $i$
------ Subtasks ------
Subtask #1 (10 points): $N = 16$
Subtask #2 (15 points): $N = 1,000$
Subtask #3 (15 points): $r_{i} β€ 10^{6}$ for each valid $i$
Subtask #4 (60 points): original constraints
----- Sample Input 1 ------
3
3
1 4
2 6
5 7
2
1 4
4 6
2
3 7
8 9
----- Sample Output 1 ------
1
-1
0
----- explanation 1 ------
Example case 1: It is enough to delete the range $[2, 6]$.
Example case 2: There is no way to divide these two ranges into two suitable non-empty subsets and deleting any range obviously makes it impossible too.
Example case 3: There is no need to delete any ranges, we can just put one range into one subset and the other range into the other subset. | for _ in range(int(input())):
n = int(input())
arr = []
brr = []
for i in range(n):
a, b = map(int, input().split())
x = min(a, b)
y = max(a, b)
arr.append([x, y])
brr.append(y)
arr.sort()
curr_max = arr[0][1]
ind = 0
rs = []
for i in range(1, n):
if arr[i][0] > curr_max:
rs.append(i - ind)
ind = i
curr_max = arr[i][1]
elif arr[i][1] > curr_max:
curr_max = arr[i][1]
rs.append(n - ind)
if len(rs) >= 2:
print(0)
else:
brr.sort()
i = 0
j = 0
result = -1
while i < n and j < n:
if arr[i][0] <= brr[j]:
if i != 0 and j != 0:
if result == -1:
result = i - j
rs_ind = i
elif result > i - j:
result = i - j
rs_ind = i
i += 1
else:
j += 1
print(result) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Tenten runs a weapon shop for ninjas. Today she is willing to sell $n$ shurikens which cost $1$, $2$, ..., $n$ ryo (local currency). During a day, Tenten will place the shurikens onto the showcase, which is empty at the beginning of the day. Her job is fairly simple: sometimes Tenten places another shuriken (from the available shurikens) on the showcase, and sometimes a ninja comes in and buys a shuriken from the showcase. Since ninjas are thrifty, they always buy the cheapest shuriken from the showcase.
Tenten keeps a record for all events, and she ends up with a list of the following types of records:
+ means that she placed another shuriken on the showcase; - x means that the shuriken of price $x$ was bought.
Today was a lucky day, and all shurikens were bought. Now Tenten wonders if her list is consistent, and what could be a possible order of placing the shurikens on the showcase. Help her to find this out!
-----Input-----
The first line contains the only integer $n$ ($1\leq n\leq 10^5$) standing for the number of shurikens.
The following $2n$ lines describe the events in the format described above. It's guaranteed that there are exactly $n$ events of the first type, and each price from $1$ to $n$ occurs exactly once in the events of the second type.
-----Output-----
If the list is consistent, print "YES". Otherwise (that is, if the list is contradictory and there is no valid order of shurikens placement), print "NO".
In the first case the second line must contain $n$ space-separated integers denoting the prices of shurikens in order they were placed. If there are multiple answers, print any.
-----Examples-----
Input
4
+
+
- 2
+
- 3
+
- 1
- 4
Output
YES
4 2 3 1
Input
1
- 1
+
Output
NO
Input
3
+
+
+
- 2
- 1
- 3
Output
NO
-----Note-----
In the first example Tenten first placed shurikens with prices $4$ and $2$. After this a customer came in and bought the cheapest shuriken which costed $2$. Next, Tenten added a shuriken with price $3$ on the showcase to the already placed $4$-ryo. Then a new customer bought this $3$-ryo shuriken. After this she added a $1$-ryo shuriken. Finally, the last two customers bought shurikens $1$ and $4$, respectively. Note that the order $[2, 4, 3, 1]$ is also valid.
In the second example the first customer bought a shuriken before anything was placed, which is clearly impossible.
In the third example Tenten put all her shurikens onto the showcase, after which a customer came in and bought a shuriken with price $2$. This is impossible since the shuriken was not the cheapest, we know that the $1$-ryo shuriken was also there. | n = int(input())
query = []
for i in range(2 * n):
a = input().split()
if a[0] == "+":
query.append(0)
else:
query.append(int(a[1]))
stack = []
out = []
ans = "YES"
if query[-1] == 0:
ans = "NO"
else:
stack.append(query.pop())
while query:
if not stack and query[-1] == 0 or stack and stack[-1] < query[-1]:
ans = "NO"
break
elif query[-1] > 0:
stack.append(query.pop())
else:
_ = query.pop()
out.append(stack.pop())
print(ans)
if ans == "YES":
print(*out[::-1]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR STRING IF VAR NUMBER NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR IF VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR NUMBER |
Tenten runs a weapon shop for ninjas. Today she is willing to sell $n$ shurikens which cost $1$, $2$, ..., $n$ ryo (local currency). During a day, Tenten will place the shurikens onto the showcase, which is empty at the beginning of the day. Her job is fairly simple: sometimes Tenten places another shuriken (from the available shurikens) on the showcase, and sometimes a ninja comes in and buys a shuriken from the showcase. Since ninjas are thrifty, they always buy the cheapest shuriken from the showcase.
Tenten keeps a record for all events, and she ends up with a list of the following types of records:
+ means that she placed another shuriken on the showcase; - x means that the shuriken of price $x$ was bought.
Today was a lucky day, and all shurikens were bought. Now Tenten wonders if her list is consistent, and what could be a possible order of placing the shurikens on the showcase. Help her to find this out!
-----Input-----
The first line contains the only integer $n$ ($1\leq n\leq 10^5$) standing for the number of shurikens.
The following $2n$ lines describe the events in the format described above. It's guaranteed that there are exactly $n$ events of the first type, and each price from $1$ to $n$ occurs exactly once in the events of the second type.
-----Output-----
If the list is consistent, print "YES". Otherwise (that is, if the list is contradictory and there is no valid order of shurikens placement), print "NO".
In the first case the second line must contain $n$ space-separated integers denoting the prices of shurikens in order they were placed. If there are multiple answers, print any.
-----Examples-----
Input
4
+
+
- 2
+
- 3
+
- 1
- 4
Output
YES
4 2 3 1
Input
1
- 1
+
Output
NO
Input
3
+
+
+
- 2
- 1
- 3
Output
NO
-----Note-----
In the first example Tenten first placed shurikens with prices $4$ and $2$. After this a customer came in and bought the cheapest shuriken which costed $2$. Next, Tenten added a shuriken with price $3$ on the showcase to the already placed $4$-ryo. Then a new customer bought this $3$-ryo shuriken. After this she added a $1$-ryo shuriken. Finally, the last two customers bought shurikens $1$ and $4$, respectively. Note that the order $[2, 4, 3, 1]$ is also valid.
In the second example the first customer bought a shuriken before anything was placed, which is clearly impossible.
In the third example Tenten put all her shurikens onto the showcase, after which a customer came in and bought a shuriken with price $2$. This is impossible since the shuriken was not the cheapest, we know that the $1$-ryo shuriken was also there. | n = int(input())
a = [input() for _ in range(2 * n)]
q = []
ans = []
for line in a[::-1]:
if line == "+":
if q == []:
print("NO")
exit(0)
else:
ans.append(q[-1])
q.pop()
else:
r = line.split()
r[1] = int(r[1])
if q != [] and r[1] > q[-1]:
print("NO")
exit(0)
else:
q.append(r[1])
print("YES")
print(*ans[::-1]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR NUMBER IF VAR STRING IF VAR LIST EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR LIST VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER |
Tenten runs a weapon shop for ninjas. Today she is willing to sell $n$ shurikens which cost $1$, $2$, ..., $n$ ryo (local currency). During a day, Tenten will place the shurikens onto the showcase, which is empty at the beginning of the day. Her job is fairly simple: sometimes Tenten places another shuriken (from the available shurikens) on the showcase, and sometimes a ninja comes in and buys a shuriken from the showcase. Since ninjas are thrifty, they always buy the cheapest shuriken from the showcase.
Tenten keeps a record for all events, and she ends up with a list of the following types of records:
+ means that she placed another shuriken on the showcase; - x means that the shuriken of price $x$ was bought.
Today was a lucky day, and all shurikens were bought. Now Tenten wonders if her list is consistent, and what could be a possible order of placing the shurikens on the showcase. Help her to find this out!
-----Input-----
The first line contains the only integer $n$ ($1\leq n\leq 10^5$) standing for the number of shurikens.
The following $2n$ lines describe the events in the format described above. It's guaranteed that there are exactly $n$ events of the first type, and each price from $1$ to $n$ occurs exactly once in the events of the second type.
-----Output-----
If the list is consistent, print "YES". Otherwise (that is, if the list is contradictory and there is no valid order of shurikens placement), print "NO".
In the first case the second line must contain $n$ space-separated integers denoting the prices of shurikens in order they were placed. If there are multiple answers, print any.
-----Examples-----
Input
4
+
+
- 2
+
- 3
+
- 1
- 4
Output
YES
4 2 3 1
Input
1
- 1
+
Output
NO
Input
3
+
+
+
- 2
- 1
- 3
Output
NO
-----Note-----
In the first example Tenten first placed shurikens with prices $4$ and $2$. After this a customer came in and bought the cheapest shuriken which costed $2$. Next, Tenten added a shuriken with price $3$ on the showcase to the already placed $4$-ryo. Then a new customer bought this $3$-ryo shuriken. After this she added a $1$-ryo shuriken. Finally, the last two customers bought shurikens $1$ and $4$, respectively. Note that the order $[2, 4, 3, 1]$ is also valid.
In the second example the first customer bought a shuriken before anything was placed, which is clearly impossible.
In the third example Tenten put all her shurikens onto the showcase, after which a customer came in and bought a shuriken with price $2$. This is impossible since the shuriken was not the cheapest, we know that the $1$-ryo shuriken was also there. | import sys
from sys import stdin
n = int(stdin.readline())
s = []
for i in range(2 * n):
S = stdin.readline()[:-1]
if S[0] == "+":
s.append(("+", None))
else:
t1, t2 = S.split()
t2 = int(t2)
s.append(("-", t2))
q = []
ans = []
for i in range(2 * n - 1, -1, -1):
t, num = s[i]
if t == "-":
if len(q) > 0 and q[-1] < num:
print("NO")
sys.exit()
else:
q.append(num)
elif len(q) == 0:
print("NO")
sys.exit()
else:
ans.append(q[-1])
del q[-1]
print("YES")
ans.reverse()
print(*ans) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR STRING NONE ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR STRING IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
Tenten runs a weapon shop for ninjas. Today she is willing to sell $n$ shurikens which cost $1$, $2$, ..., $n$ ryo (local currency). During a day, Tenten will place the shurikens onto the showcase, which is empty at the beginning of the day. Her job is fairly simple: sometimes Tenten places another shuriken (from the available shurikens) on the showcase, and sometimes a ninja comes in and buys a shuriken from the showcase. Since ninjas are thrifty, they always buy the cheapest shuriken from the showcase.
Tenten keeps a record for all events, and she ends up with a list of the following types of records:
+ means that she placed another shuriken on the showcase; - x means that the shuriken of price $x$ was bought.
Today was a lucky day, and all shurikens were bought. Now Tenten wonders if her list is consistent, and what could be a possible order of placing the shurikens on the showcase. Help her to find this out!
-----Input-----
The first line contains the only integer $n$ ($1\leq n\leq 10^5$) standing for the number of shurikens.
The following $2n$ lines describe the events in the format described above. It's guaranteed that there are exactly $n$ events of the first type, and each price from $1$ to $n$ occurs exactly once in the events of the second type.
-----Output-----
If the list is consistent, print "YES". Otherwise (that is, if the list is contradictory and there is no valid order of shurikens placement), print "NO".
In the first case the second line must contain $n$ space-separated integers denoting the prices of shurikens in order they were placed. If there are multiple answers, print any.
-----Examples-----
Input
4
+
+
- 2
+
- 3
+
- 1
- 4
Output
YES
4 2 3 1
Input
1
- 1
+
Output
NO
Input
3
+
+
+
- 2
- 1
- 3
Output
NO
-----Note-----
In the first example Tenten first placed shurikens with prices $4$ and $2$. After this a customer came in and bought the cheapest shuriken which costed $2$. Next, Tenten added a shuriken with price $3$ on the showcase to the already placed $4$-ryo. Then a new customer bought this $3$-ryo shuriken. After this she added a $1$-ryo shuriken. Finally, the last two customers bought shurikens $1$ and $4$, respectively. Note that the order $[2, 4, 3, 1]$ is also valid.
In the second example the first customer bought a shuriken before anything was placed, which is clearly impossible.
In the third example Tenten put all her shurikens onto the showcase, after which a customer came in and bought a shuriken with price $2$. This is impossible since the shuriken was not the cheapest, we know that the $1$-ryo shuriken was also there. | import sys
t = int(input())
c = [0] * t
def error():
print("NO")
raise SystemExit
def max2(a, b):
if a > b:
return a
return b
v = [[10000000000.0, -1]]
index = 0
for _ in range(t + t):
q = sys.stdin.readline()
if q[0] == "+":
v.append([0, index])
index += 1
continue
n = int(q.split()[1])
if v[-1][0] > n:
error()
c[v.pop()[1]] = n
v[-1][0] = max2(n, v[-1][0])
print("YES")
print(" ".join(list(map(lambda x: str(x), c)))) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF EXPR FUNC_CALL VAR STRING VAR FUNC_DEF IF VAR VAR RETURN VAR RETURN VAR ASSIGN VAR LIST LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR LIST NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Tenten runs a weapon shop for ninjas. Today she is willing to sell $n$ shurikens which cost $1$, $2$, ..., $n$ ryo (local currency). During a day, Tenten will place the shurikens onto the showcase, which is empty at the beginning of the day. Her job is fairly simple: sometimes Tenten places another shuriken (from the available shurikens) on the showcase, and sometimes a ninja comes in and buys a shuriken from the showcase. Since ninjas are thrifty, they always buy the cheapest shuriken from the showcase.
Tenten keeps a record for all events, and she ends up with a list of the following types of records:
+ means that she placed another shuriken on the showcase; - x means that the shuriken of price $x$ was bought.
Today was a lucky day, and all shurikens were bought. Now Tenten wonders if her list is consistent, and what could be a possible order of placing the shurikens on the showcase. Help her to find this out!
-----Input-----
The first line contains the only integer $n$ ($1\leq n\leq 10^5$) standing for the number of shurikens.
The following $2n$ lines describe the events in the format described above. It's guaranteed that there are exactly $n$ events of the first type, and each price from $1$ to $n$ occurs exactly once in the events of the second type.
-----Output-----
If the list is consistent, print "YES". Otherwise (that is, if the list is contradictory and there is no valid order of shurikens placement), print "NO".
In the first case the second line must contain $n$ space-separated integers denoting the prices of shurikens in order they were placed. If there are multiple answers, print any.
-----Examples-----
Input
4
+
+
- 2
+
- 3
+
- 1
- 4
Output
YES
4 2 3 1
Input
1
- 1
+
Output
NO
Input
3
+
+
+
- 2
- 1
- 3
Output
NO
-----Note-----
In the first example Tenten first placed shurikens with prices $4$ and $2$. After this a customer came in and bought the cheapest shuriken which costed $2$. Next, Tenten added a shuriken with price $3$ on the showcase to the already placed $4$-ryo. Then a new customer bought this $3$-ryo shuriken. After this she added a $1$-ryo shuriken. Finally, the last two customers bought shurikens $1$ and $4$, respectively. Note that the order $[2, 4, 3, 1]$ is also valid.
In the second example the first customer bought a shuriken before anything was placed, which is clearly impossible.
In the third example Tenten put all her shurikens onto the showcase, after which a customer came in and bought a shuriken with price $2$. This is impossible since the shuriken was not the cheapest, we know that the $1$-ryo shuriken was also there. | n = int(input())
ans = [0] * n
val = []
st = []
temp = "YES"
c = -1
for _ in range(0, 2 * n):
s = input().split()
if s[0] == "+":
c += 1
st.append(0)
val.append(c)
else:
tt = int(s[1])
if len(st) == 0:
temp = "NO"
else:
if st[-1] > tt:
temp = "NO"
ptr = val.pop()
ans[ptr] = tt
tt2 = st.pop()
if len(st) > 0:
st[-1] = max(st[-1], tt, tt2)
print(temp)
if temp == "YES":
print(*ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR |
Tenten runs a weapon shop for ninjas. Today she is willing to sell $n$ shurikens which cost $1$, $2$, ..., $n$ ryo (local currency). During a day, Tenten will place the shurikens onto the showcase, which is empty at the beginning of the day. Her job is fairly simple: sometimes Tenten places another shuriken (from the available shurikens) on the showcase, and sometimes a ninja comes in and buys a shuriken from the showcase. Since ninjas are thrifty, they always buy the cheapest shuriken from the showcase.
Tenten keeps a record for all events, and she ends up with a list of the following types of records:
+ means that she placed another shuriken on the showcase; - x means that the shuriken of price $x$ was bought.
Today was a lucky day, and all shurikens were bought. Now Tenten wonders if her list is consistent, and what could be a possible order of placing the shurikens on the showcase. Help her to find this out!
-----Input-----
The first line contains the only integer $n$ ($1\leq n\leq 10^5$) standing for the number of shurikens.
The following $2n$ lines describe the events in the format described above. It's guaranteed that there are exactly $n$ events of the first type, and each price from $1$ to $n$ occurs exactly once in the events of the second type.
-----Output-----
If the list is consistent, print "YES". Otherwise (that is, if the list is contradictory and there is no valid order of shurikens placement), print "NO".
In the first case the second line must contain $n$ space-separated integers denoting the prices of shurikens in order they were placed. If there are multiple answers, print any.
-----Examples-----
Input
4
+
+
- 2
+
- 3
+
- 1
- 4
Output
YES
4 2 3 1
Input
1
- 1
+
Output
NO
Input
3
+
+
+
- 2
- 1
- 3
Output
NO
-----Note-----
In the first example Tenten first placed shurikens with prices $4$ and $2$. After this a customer came in and bought the cheapest shuriken which costed $2$. Next, Tenten added a shuriken with price $3$ on the showcase to the already placed $4$-ryo. Then a new customer bought this $3$-ryo shuriken. After this she added a $1$-ryo shuriken. Finally, the last two customers bought shurikens $1$ and $4$, respectively. Note that the order $[2, 4, 3, 1]$ is also valid.
In the second example the first customer bought a shuriken before anything was placed, which is clearly impossible.
In the third example Tenten put all her shurikens onto the showcase, after which a customer came in and bought a shuriken with price $2$. This is impossible since the shuriken was not the cheapest, we know that the $1$-ryo shuriken was also there. | t = int(input())
c = [0] * t
def error():
print("NO")
raise SystemExit
v = []
index = 0
for _ in range(t + t):
q = input()
if q[0] == "+":
v.append([0, index])
index += 1
continue
n = int(q.split()[1])
if len(v) == 0:
error()
if v[-1][0] > n:
error()
c[v.pop()[1]] = n
if len(v) != 0:
v[-1][0] = max(n, v[-1][0])
print("YES")
print(" ".join(list(map(lambda x: str(x), c)))) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF EXPR FUNC_CALL VAR STRING VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR LIST NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Tenten runs a weapon shop for ninjas. Today she is willing to sell $n$ shurikens which cost $1$, $2$, ..., $n$ ryo (local currency). During a day, Tenten will place the shurikens onto the showcase, which is empty at the beginning of the day. Her job is fairly simple: sometimes Tenten places another shuriken (from the available shurikens) on the showcase, and sometimes a ninja comes in and buys a shuriken from the showcase. Since ninjas are thrifty, they always buy the cheapest shuriken from the showcase.
Tenten keeps a record for all events, and she ends up with a list of the following types of records:
+ means that she placed another shuriken on the showcase; - x means that the shuriken of price $x$ was bought.
Today was a lucky day, and all shurikens were bought. Now Tenten wonders if her list is consistent, and what could be a possible order of placing the shurikens on the showcase. Help her to find this out!
-----Input-----
The first line contains the only integer $n$ ($1\leq n\leq 10^5$) standing for the number of shurikens.
The following $2n$ lines describe the events in the format described above. It's guaranteed that there are exactly $n$ events of the first type, and each price from $1$ to $n$ occurs exactly once in the events of the second type.
-----Output-----
If the list is consistent, print "YES". Otherwise (that is, if the list is contradictory and there is no valid order of shurikens placement), print "NO".
In the first case the second line must contain $n$ space-separated integers denoting the prices of shurikens in order they were placed. If there are multiple answers, print any.
-----Examples-----
Input
4
+
+
- 2
+
- 3
+
- 1
- 4
Output
YES
4 2 3 1
Input
1
- 1
+
Output
NO
Input
3
+
+
+
- 2
- 1
- 3
Output
NO
-----Note-----
In the first example Tenten first placed shurikens with prices $4$ and $2$. After this a customer came in and bought the cheapest shuriken which costed $2$. Next, Tenten added a shuriken with price $3$ on the showcase to the already placed $4$-ryo. Then a new customer bought this $3$-ryo shuriken. After this she added a $1$-ryo shuriken. Finally, the last two customers bought shurikens $1$ and $4$, respectively. Note that the order $[2, 4, 3, 1]$ is also valid.
In the second example the first customer bought a shuriken before anything was placed, which is clearly impossible.
In the third example Tenten put all her shurikens onto the showcase, after which a customer came in and bought a shuriken with price $2$. This is impossible since the shuriken was not the cheapest, we know that the $1$-ryo shuriken was also there. | N = int(input())
Q = []
for i in range(N * 2):
Q.append(input())
Q = Q[::-1]
stack = []
ans = []
for q in Q:
if len(q) == 1:
if not stack:
print("NO")
exit()
a = stack.pop()
ans.append(a)
else:
m, a = q.split()
a = int(a)
if stack and stack[-1] < a:
print("NO")
exit()
stack.append(a)
print("YES")
print(*ans[::-1]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER |
Tenten runs a weapon shop for ninjas. Today she is willing to sell $n$ shurikens which cost $1$, $2$, ..., $n$ ryo (local currency). During a day, Tenten will place the shurikens onto the showcase, which is empty at the beginning of the day. Her job is fairly simple: sometimes Tenten places another shuriken (from the available shurikens) on the showcase, and sometimes a ninja comes in and buys a shuriken from the showcase. Since ninjas are thrifty, they always buy the cheapest shuriken from the showcase.
Tenten keeps a record for all events, and she ends up with a list of the following types of records:
+ means that she placed another shuriken on the showcase; - x means that the shuriken of price $x$ was bought.
Today was a lucky day, and all shurikens were bought. Now Tenten wonders if her list is consistent, and what could be a possible order of placing the shurikens on the showcase. Help her to find this out!
-----Input-----
The first line contains the only integer $n$ ($1\leq n\leq 10^5$) standing for the number of shurikens.
The following $2n$ lines describe the events in the format described above. It's guaranteed that there are exactly $n$ events of the first type, and each price from $1$ to $n$ occurs exactly once in the events of the second type.
-----Output-----
If the list is consistent, print "YES". Otherwise (that is, if the list is contradictory and there is no valid order of shurikens placement), print "NO".
In the first case the second line must contain $n$ space-separated integers denoting the prices of shurikens in order they were placed. If there are multiple answers, print any.
-----Examples-----
Input
4
+
+
- 2
+
- 3
+
- 1
- 4
Output
YES
4 2 3 1
Input
1
- 1
+
Output
NO
Input
3
+
+
+
- 2
- 1
- 3
Output
NO
-----Note-----
In the first example Tenten first placed shurikens with prices $4$ and $2$. After this a customer came in and bought the cheapest shuriken which costed $2$. Next, Tenten added a shuriken with price $3$ on the showcase to the already placed $4$-ryo. Then a new customer bought this $3$-ryo shuriken. After this she added a $1$-ryo shuriken. Finally, the last two customers bought shurikens $1$ and $4$, respectively. Note that the order $[2, 4, 3, 1]$ is also valid.
In the second example the first customer bought a shuriken before anything was placed, which is clearly impossible.
In the third example Tenten put all her shurikens onto the showcase, after which a customer came in and bought a shuriken with price $2$. This is impossible since the shuriken was not the cheapest, we know that the $1$-ryo shuriken was also there. | l = []
n = int(input())
for _ in range(2 * n):
l.append(input())
stk = []
visited = [False] * n
f = True
ans = []
for i in reversed(l):
if i[0] == "+":
if len(stk) == 0:
f = False
break
t = stk.pop()
ans.append(t)
if visited[t - 1]:
f = False
break
visited[t - 1] = True
else:
t = int(i[2:])
if visited[t - 1]:
f = False
break
if len(stk) > 0 and stk[-1] < t:
f = False
break
stk.append(t)
if len(stk) != 0:
f = False
if f:
print("YES")
print(*reversed(ans))
else:
print("NO") | ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
Tenten runs a weapon shop for ninjas. Today she is willing to sell $n$ shurikens which cost $1$, $2$, ..., $n$ ryo (local currency). During a day, Tenten will place the shurikens onto the showcase, which is empty at the beginning of the day. Her job is fairly simple: sometimes Tenten places another shuriken (from the available shurikens) on the showcase, and sometimes a ninja comes in and buys a shuriken from the showcase. Since ninjas are thrifty, they always buy the cheapest shuriken from the showcase.
Tenten keeps a record for all events, and she ends up with a list of the following types of records:
+ means that she placed another shuriken on the showcase; - x means that the shuriken of price $x$ was bought.
Today was a lucky day, and all shurikens were bought. Now Tenten wonders if her list is consistent, and what could be a possible order of placing the shurikens on the showcase. Help her to find this out!
-----Input-----
The first line contains the only integer $n$ ($1\leq n\leq 10^5$) standing for the number of shurikens.
The following $2n$ lines describe the events in the format described above. It's guaranteed that there are exactly $n$ events of the first type, and each price from $1$ to $n$ occurs exactly once in the events of the second type.
-----Output-----
If the list is consistent, print "YES". Otherwise (that is, if the list is contradictory and there is no valid order of shurikens placement), print "NO".
In the first case the second line must contain $n$ space-separated integers denoting the prices of shurikens in order they were placed. If there are multiple answers, print any.
-----Examples-----
Input
4
+
+
- 2
+
- 3
+
- 1
- 4
Output
YES
4 2 3 1
Input
1
- 1
+
Output
NO
Input
3
+
+
+
- 2
- 1
- 3
Output
NO
-----Note-----
In the first example Tenten first placed shurikens with prices $4$ and $2$. After this a customer came in and bought the cheapest shuriken which costed $2$. Next, Tenten added a shuriken with price $3$ on the showcase to the already placed $4$-ryo. Then a new customer bought this $3$-ryo shuriken. After this she added a $1$-ryo shuriken. Finally, the last two customers bought shurikens $1$ and $4$, respectively. Note that the order $[2, 4, 3, 1]$ is also valid.
In the second example the first customer bought a shuriken before anything was placed, which is clearly impossible.
In the third example Tenten put all her shurikens onto the showcase, after which a customer came in and bought a shuriken with price $2$. This is impossible since the shuriken was not the cheapest, we know that the $1$-ryo shuriken was also there. | import sys
input = sys.stdin.readline
n = int(input())
a = [0] * n
q = []
limit = []
cnt = 0
vio = False
query = []
for i in range(2 * n):
query.append(input())
for i in range(2 * n):
if len(query[i]) == 2:
q.append(cnt)
cnt += 1
if len(limit) > 0 and limit[-1][0] == 1:
limit[-1][1] += 1
else:
limit.append([1, 1])
else:
m, x = query[i].split()
if len(limit) == 0 or limit[-1][0] > int(x):
vio = True
break
p = q.pop()
a[p] = int(x)
limit[-1][1] -= 1
c = 0
while len(limit) > 0 and limit[-1][0] <= int(x) + 1:
c += limit[-1][1]
limit.pop()
if c != 0:
limit.append([int(x) + 1, c])
if vio:
print("NO")
else:
print("YES")
print(*a) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
Tenten runs a weapon shop for ninjas. Today she is willing to sell $n$ shurikens which cost $1$, $2$, ..., $n$ ryo (local currency). During a day, Tenten will place the shurikens onto the showcase, which is empty at the beginning of the day. Her job is fairly simple: sometimes Tenten places another shuriken (from the available shurikens) on the showcase, and sometimes a ninja comes in and buys a shuriken from the showcase. Since ninjas are thrifty, they always buy the cheapest shuriken from the showcase.
Tenten keeps a record for all events, and she ends up with a list of the following types of records:
+ means that she placed another shuriken on the showcase; - x means that the shuriken of price $x$ was bought.
Today was a lucky day, and all shurikens were bought. Now Tenten wonders if her list is consistent, and what could be a possible order of placing the shurikens on the showcase. Help her to find this out!
-----Input-----
The first line contains the only integer $n$ ($1\leq n\leq 10^5$) standing for the number of shurikens.
The following $2n$ lines describe the events in the format described above. It's guaranteed that there are exactly $n$ events of the first type, and each price from $1$ to $n$ occurs exactly once in the events of the second type.
-----Output-----
If the list is consistent, print "YES". Otherwise (that is, if the list is contradictory and there is no valid order of shurikens placement), print "NO".
In the first case the second line must contain $n$ space-separated integers denoting the prices of shurikens in order they were placed. If there are multiple answers, print any.
-----Examples-----
Input
4
+
+
- 2
+
- 3
+
- 1
- 4
Output
YES
4 2 3 1
Input
1
- 1
+
Output
NO
Input
3
+
+
+
- 2
- 1
- 3
Output
NO
-----Note-----
In the first example Tenten first placed shurikens with prices $4$ and $2$. After this a customer came in and bought the cheapest shuriken which costed $2$. Next, Tenten added a shuriken with price $3$ on the showcase to the already placed $4$-ryo. Then a new customer bought this $3$-ryo shuriken. After this she added a $1$-ryo shuriken. Finally, the last two customers bought shurikens $1$ and $4$, respectively. Note that the order $[2, 4, 3, 1]$ is also valid.
In the second example the first customer bought a shuriken before anything was placed, which is clearly impossible.
In the third example Tenten put all her shurikens onto the showcase, after which a customer came in and bought a shuriken with price $2$. This is impossible since the shuriken was not the cheapest, we know that the $1$-ryo shuriken was also there. | def solve():
n = int(input())
a = []
for i in range(2 * n):
s = input()
if s[0] == "-":
s = s.split()
a.append([s[0], int(s[1])])
else:
a.append(s)
st = []
ans = []
for i in a[::-1]:
if i[0] == "+":
if st:
ans.append(st.pop())
else:
print("NO")
return
elif not st or st[-1] > i[1]:
st.append(i[1])
else:
print("NO")
return
print("YES")
print(*ans[::-1])
def main():
t = 1
for _ in range(t):
solve()
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR NUMBER IF VAR NUMBER STRING IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING RETURN IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Tenten runs a weapon shop for ninjas. Today she is willing to sell $n$ shurikens which cost $1$, $2$, ..., $n$ ryo (local currency). During a day, Tenten will place the shurikens onto the showcase, which is empty at the beginning of the day. Her job is fairly simple: sometimes Tenten places another shuriken (from the available shurikens) on the showcase, and sometimes a ninja comes in and buys a shuriken from the showcase. Since ninjas are thrifty, they always buy the cheapest shuriken from the showcase.
Tenten keeps a record for all events, and she ends up with a list of the following types of records:
+ means that she placed another shuriken on the showcase; - x means that the shuriken of price $x$ was bought.
Today was a lucky day, and all shurikens were bought. Now Tenten wonders if her list is consistent, and what could be a possible order of placing the shurikens on the showcase. Help her to find this out!
-----Input-----
The first line contains the only integer $n$ ($1\leq n\leq 10^5$) standing for the number of shurikens.
The following $2n$ lines describe the events in the format described above. It's guaranteed that there are exactly $n$ events of the first type, and each price from $1$ to $n$ occurs exactly once in the events of the second type.
-----Output-----
If the list is consistent, print "YES". Otherwise (that is, if the list is contradictory and there is no valid order of shurikens placement), print "NO".
In the first case the second line must contain $n$ space-separated integers denoting the prices of shurikens in order they were placed. If there are multiple answers, print any.
-----Examples-----
Input
4
+
+
- 2
+
- 3
+
- 1
- 4
Output
YES
4 2 3 1
Input
1
- 1
+
Output
NO
Input
3
+
+
+
- 2
- 1
- 3
Output
NO
-----Note-----
In the first example Tenten first placed shurikens with prices $4$ and $2$. After this a customer came in and bought the cheapest shuriken which costed $2$. Next, Tenten added a shuriken with price $3$ on the showcase to the already placed $4$-ryo. Then a new customer bought this $3$-ryo shuriken. After this she added a $1$-ryo shuriken. Finally, the last two customers bought shurikens $1$ and $4$, respectively. Note that the order $[2, 4, 3, 1]$ is also valid.
In the second example the first customer bought a shuriken before anything was placed, which is clearly impossible.
In the third example Tenten put all her shurikens onto the showcase, after which a customer came in and bought a shuriken with price $2$. This is impossible since the shuriken was not the cheapest, we know that the $1$-ryo shuriken was also there. | def read_seq(v):
seq = []
for i in range(2 * v):
s = input().split()
if len(s) == 2:
s[1] = int(s[1])
seq.append(s)
return seq
def solve():
n = int(input())
pos = [-1] * (2 * n)
stack = []
seq = read_seq(n)
for i, act in enumerate(seq):
if act[0] == "+":
stack.append(i)
elif not stack:
return None
else:
v = stack.pop()
pos[v] = act[1]
if stack:
return None
stack = []
for i, act in enumerate(seq):
if act[0] == "+":
if stack and stack[-1] < pos[i]:
return None
else:
stack.append(pos[i])
else:
stack.pop()
return [x for x in pos if x != -1]
def main():
res = solve()
if res:
print("YES")
print(" ".join(map(str, res)))
else:
print("NO")
main() | FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR IF VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER IF VAR RETURN NONE ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING IF VAR VAR NUMBER VAR VAR RETURN NONE EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR |
Tenten runs a weapon shop for ninjas. Today she is willing to sell $n$ shurikens which cost $1$, $2$, ..., $n$ ryo (local currency). During a day, Tenten will place the shurikens onto the showcase, which is empty at the beginning of the day. Her job is fairly simple: sometimes Tenten places another shuriken (from the available shurikens) on the showcase, and sometimes a ninja comes in and buys a shuriken from the showcase. Since ninjas are thrifty, they always buy the cheapest shuriken from the showcase.
Tenten keeps a record for all events, and she ends up with a list of the following types of records:
+ means that she placed another shuriken on the showcase; - x means that the shuriken of price $x$ was bought.
Today was a lucky day, and all shurikens were bought. Now Tenten wonders if her list is consistent, and what could be a possible order of placing the shurikens on the showcase. Help her to find this out!
-----Input-----
The first line contains the only integer $n$ ($1\leq n\leq 10^5$) standing for the number of shurikens.
The following $2n$ lines describe the events in the format described above. It's guaranteed that there are exactly $n$ events of the first type, and each price from $1$ to $n$ occurs exactly once in the events of the second type.
-----Output-----
If the list is consistent, print "YES". Otherwise (that is, if the list is contradictory and there is no valid order of shurikens placement), print "NO".
In the first case the second line must contain $n$ space-separated integers denoting the prices of shurikens in order they were placed. If there are multiple answers, print any.
-----Examples-----
Input
4
+
+
- 2
+
- 3
+
- 1
- 4
Output
YES
4 2 3 1
Input
1
- 1
+
Output
NO
Input
3
+
+
+
- 2
- 1
- 3
Output
NO
-----Note-----
In the first example Tenten first placed shurikens with prices $4$ and $2$. After this a customer came in and bought the cheapest shuriken which costed $2$. Next, Tenten added a shuriken with price $3$ on the showcase to the already placed $4$-ryo. Then a new customer bought this $3$-ryo shuriken. After this she added a $1$-ryo shuriken. Finally, the last two customers bought shurikens $1$ and $4$, respectively. Note that the order $[2, 4, 3, 1]$ is also valid.
In the second example the first customer bought a shuriken before anything was placed, which is clearly impossible.
In the third example Tenten put all her shurikens onto the showcase, after which a customer came in and bought a shuriken with price $2$. This is impossible since the shuriken was not the cheapest, we know that the $1$-ryo shuriken was also there. | import sys
input = sys.stdin.readline
n = int(input())
events = []
for i in range(2 * n):
x = input()
if x == "+\n":
events.append([0])
else:
x, y = x.split()
events.append([1, int(y)])
poss = True
shelf = []
placed = []
while len(events) > 0:
ev = events.pop()
if ev[0] == 0:
if len(shelf) == 0:
poss = False
break
x = shelf.pop()
placed.append(x)
else:
x = ev[1]
if len(shelf) > 0 and shelf[-1] < x:
poss = False
break
shelf.append(x)
if poss:
cpl = [0] * n
for i in range(n):
cpl[i] = placed[-i - 1]
print("YES")
print(*cpl)
else:
print("NO") | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR LIST NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
The Little Elephant loves lucky strings. Everybody knows that the lucky string is a string of digits that contains only the lucky digits 4 and 7. For example, strings "47", "744", "4" are lucky while "5", "17", "467" are not.
The Little Elephant has the strings A and B of digits. These strings are of equal lengths, that is |A| = |B|. He wants to get some lucky string from them. For this he performs the following operations. At first he arbitrary reorders digits of A. Then he arbitrary reorders digits of B. After that he creates the string C such that its i-th digit is the maximum between the i-th digit of A and the i-th digit of B. In other words, C[i] = max{A[i], B[i]} for i from 1 to |A|. After that he removes from C all non-lucky digits saving the order of the remaining (lucky) digits. So C now becomes a lucky string. For example, if after reordering A = "754" and B = "873", then C is at first "874" and then it becomes "74".
The Little Elephant wants the resulting string to be as lucky as possible. The formal definition of this is that the resulting string should be the lexicographically greatest possible string among all the strings that can be obtained from the given strings A and B by the described process.
Notes
|A| denotes the length of the string A.
A[i] denotes the i-th digit of the string A. Here we numerate the digits starting from 1. So 1 β€ i β€ |A|.
The string A is called lexicographically greater than the string B if either there exists some index i such that A[i] > B[i] and for each j < i we have A[j] = B[j], or B is a proper prefix of A, that is, |A| > |B| and first |B| digits of A coincide with the corresponding digits of B.
------ Input ------
The first line of the input contains a single integer T, the number of test cases. T test cases follow. Each test case consists of two lines. The first line contains the string A. The second line contains the string B.
------ Output ------
For each test case output a single line containing the answer for the corresponding test case. Note, that the answer can be an empty string. In this case you should print an empty line for the corresponding test case.
------ Constraints ------
1 β€ T β€ 10000
1 β€ |A| β€ 20000
|A| = |B|
Each character of A and B is a digit.
Sum of |A| across all the tests in the input does not exceed 200000.
----- Sample Input 1 ------
4
4
7
435
479
7
8
1675475
9756417
----- Sample Output 1 ------
7
74
777744
----- explanation 1 ------
Case 1. In this case the only possible string C we can get is "7" and it is the lucky string.
Case 2. If we reorder A and B as A = "543" and B = "749" the string C will be at first "749" and then becomes "74". It can be shown that this is the lexicographically greatest string for the given A and B.
Case 3. In this case the only possible string C we can get is "8" and it becomes and empty string after removing of non-lucky digits.
Case 4. If we reorder A and B as A = "7765541" and B = "5697714" the string C will be at first "7797744" and then becomes "777744". Note that we can construct any lexicographically greater string for the given A and B since we have only four "sevens" and two "fours" among digits of both strings A and B as well the constructed string "777744". | for _ in range(int(input())):
a = input()
b = input()
a7 = 0
a4 = 0
a74 = 0
a40 = 0
b7 = 0
b4 = 0
b74 = 0
b40 = 0
for i in range(len(a)):
if a[i] == "7":
a7 += 1
elif a[i] == "4":
a4 += 1
elif "7" > a[i] > "4":
a74 += 1
elif a[i] < "4":
a40 += 1
if b[i] == "7":
b7 += 1
elif b[i] == "4":
b4 += 1
elif "7" > b[i] > "4":
b74 += 1
elif b[i] < "4":
b40 += 1
m = min(a7, b74)
s7 = m
a7 -= m
b74 -= m
m = min(a7, b40)
s7 += m
a7 -= m
b40 -= m
m = min(a7, b4)
s7 += m
a7 -= m
b4 -= m
m = min(b7, a74)
s7 += m
b7 -= m
a74 -= m
m = min(b7, a40)
s7 += m
b7 -= m
a40 -= m
m = min(b7, a4)
s7 += m
b7 -= m
a4 -= m
m = min(a4, b40)
s4 = m
a4 -= m
b40 -= m
m = min(b4, a40)
s4 += m
b4 -= m
a40 -= m
s7 += min(a7, b7)
s4 += min(a4, b4)
print("7" * s7 + "4" * s4) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF STRING VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF STRING VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR |
Devu is a class teacher of a class of n students. One day, in the morning prayer of the school, all the students of his class were standing in a line. You are given information of their arrangement by a string s. The string s consists of only letters 'B' and 'G', where 'B' represents a boy and 'G' represents a girl.
Devu wants inter-gender interaction among his class should to be maximum. So he does not like seeing two or more boys/girls standing nearby (i.e. continuous) in the line. e.g. he does not like the arrangements BBG and GBB, but he likes BG, GBG etc.
Now by seeing the initial arrangement s of students, Devu may get furious and now he wants to change this arrangement into a likable arrangement. For achieving that, he can swap positions of any two students (not necessary continuous). Let the cost of swapping people from position i with position j (i β j) be c(i, j). You are provided an integer variable type, then the cost of the the swap will be defined by c(i, j) = |j β i|type.
Please help Devu in finding minimum cost of swaps needed to convert the current arrangement into a likable one.
-----Input-----
The first line of input contains an integer T, denoting the number of test cases. Then T test cases are follow.
The first line of each test case contains an integer type, denoting the type of the cost function. Then the next line contains string s of length n, denoting the initial arrangement s of students.
Note that the integer n is not given explicitly in input.
-----Output-----
For each test case, print a single line containing the answer of the test case, that is, the minimum cost to convert the current arrangement into a likable one. If it is not possible to convert the current arrangement into a likable one, then print -1 instead of the minimum cost.
-----Constraints and Subtasks-----Subtask 1: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 0
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 2: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 1
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 3: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 2
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 4: 25 points
- 1 β€ T β€ 102
- 1 β€ n β€ 103
- type can be 0, 1 or 2, that is type β {0, 1, 2}.
-----Example-----
Input:
8
0
BB
0
BG
0
BBGG
1
BGG
1
BGGB
1
BBBGG
2
BBGG
2
BGB
Output:
-1
0
1
1
1
3
1
0
-----Explanation-----
Note type of the first 3 test cases is 0. So c(i, j) = 1. Hence we just have to count minimum number of swaps needed.
Example case 1. There is no way to make sure that both the boys does not stand nearby. So answer is -1.
Example case 2. Arrangement is already valid. No swap is needed. So answer is 0.
Example case 3. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is 1.
Now type of the next 3 test cases is 1. So c(i, j) = |j β i|, that is, the absolute value of the difference between i and j.
Example case 4. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBG which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 5. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBGB which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 6. Swap boy at position 1 with girl at position 4. After swap the arrangement will be BGBGB which is a valid arrangement. So answer is |4 - 1| = 3.
Then type of the last 2 test cases is 2. So c(i, j) = (j β i)2
Example case 7. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is (2 - 1)2 = 1.
Example case 8. Arrangement is already valid. No swap is needed. So answer is 0. | L = ["B", "G"]
nextBG = [[], []]
def swaps(letter, S, Type):
ptr = [0, 0]
cost = 0
constant = [(False if L[(i + letter) % 2] != S[i] else True) for i in range(len(S))]
for i in range(len(S)):
while constant[nextBG[letter][ptr[letter]]] and ptr[letter] + 1 < len(
nextBG[letter]
):
ptr[letter] += 1
if nextBG[letter][ptr[letter]] < i and ptr[letter] + 1 < len(nextBG[letter]):
ptr[letter] += 1
if L[letter] != S[i]:
cost += pow(nextBG[letter][ptr[letter]] - i, 1 if Type else 0)
S[i], S[nextBG[letter][ptr[letter]]] = S[nextBG[letter][ptr[letter]]], S[i]
if ptr[letter] + 1 < len(nextBG[letter]):
ptr[letter] += 1
letter = (letter + 1) % 2
return cost
for _ in range(int(input())):
Type = int(input())
S = input()
nextBG[1] = [i for i in range(len(S)) if S[i] == "G"]
nextBG[0] = [i for i in range(len(S)) if S[i] == "B"]
if len(nextBG[0]) - len(nextBG[1]) == 1:
print(swaps(0, list(S), Type))
elif len(nextBG[1]) - len(nextBG[0]) == 1:
print(swaps(1, list(S), Type))
elif len(nextBG[0]) == len(nextBG[1]):
print(min(swaps(0, list(S), Type), swaps(1, list(S), Type)))
else:
print(-1) | ASSIGN VAR LIST STRING STRING ASSIGN VAR LIST LIST LIST FUNC_DEF ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING ASSIGN VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER |
Devu is a class teacher of a class of n students. One day, in the morning prayer of the school, all the students of his class were standing in a line. You are given information of their arrangement by a string s. The string s consists of only letters 'B' and 'G', where 'B' represents a boy and 'G' represents a girl.
Devu wants inter-gender interaction among his class should to be maximum. So he does not like seeing two or more boys/girls standing nearby (i.e. continuous) in the line. e.g. he does not like the arrangements BBG and GBB, but he likes BG, GBG etc.
Now by seeing the initial arrangement s of students, Devu may get furious and now he wants to change this arrangement into a likable arrangement. For achieving that, he can swap positions of any two students (not necessary continuous). Let the cost of swapping people from position i with position j (i β j) be c(i, j). You are provided an integer variable type, then the cost of the the swap will be defined by c(i, j) = |j β i|type.
Please help Devu in finding minimum cost of swaps needed to convert the current arrangement into a likable one.
-----Input-----
The first line of input contains an integer T, denoting the number of test cases. Then T test cases are follow.
The first line of each test case contains an integer type, denoting the type of the cost function. Then the next line contains string s of length n, denoting the initial arrangement s of students.
Note that the integer n is not given explicitly in input.
-----Output-----
For each test case, print a single line containing the answer of the test case, that is, the minimum cost to convert the current arrangement into a likable one. If it is not possible to convert the current arrangement into a likable one, then print -1 instead of the minimum cost.
-----Constraints and Subtasks-----Subtask 1: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 0
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 2: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 1
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 3: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 2
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 4: 25 points
- 1 β€ T β€ 102
- 1 β€ n β€ 103
- type can be 0, 1 or 2, that is type β {0, 1, 2}.
-----Example-----
Input:
8
0
BB
0
BG
0
BBGG
1
BGG
1
BGGB
1
BBBGG
2
BBGG
2
BGB
Output:
-1
0
1
1
1
3
1
0
-----Explanation-----
Note type of the first 3 test cases is 0. So c(i, j) = 1. Hence we just have to count minimum number of swaps needed.
Example case 1. There is no way to make sure that both the boys does not stand nearby. So answer is -1.
Example case 2. Arrangement is already valid. No swap is needed. So answer is 0.
Example case 3. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is 1.
Now type of the next 3 test cases is 1. So c(i, j) = |j β i|, that is, the absolute value of the difference between i and j.
Example case 4. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBG which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 5. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBGB which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 6. Swap boy at position 1 with girl at position 4. After swap the arrangement will be BGBGB which is a valid arrangement. So answer is |4 - 1| = 3.
Then type of the last 2 test cases is 2. So c(i, j) = (j β i)2
Example case 7. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is (2 - 1)2 = 1.
Example case 8. Arrangement is already valid. No swap is needed. So answer is 0. | t = int(input())
for k in range(t):
type = int(input())
a = input()
str1 = "B"
str2 = "G"
b1 = []
g1 = []
b2 = []
g2 = []
for i in range(1, len(a), 1):
if str1[i - 1] == "B":
str1 += "G"
elif str2[i - 1] == "B":
str2 += "G"
if str1[i - 1] == "G":
str1 += "B"
elif str2[i - 1] == "G":
str2 += "B"
for i in range(len(a)):
if str1[i] != a[i] and a[i] == "B":
b1.append(i)
elif str1[i] != a[i] and a[i] == "G":
g1.append(i)
if str2[i] != a[i] and a[i] == "B":
b2.append(i)
elif str2[i] != a[i] and a[i] == "G":
g2.append(i)
cost1 = 0
cost2 = 0
while len(b1) > 0 and len(g1) > 0:
no1 = b1.pop()
no2 = g1.pop()
if type == 0:
cost1 += 1
else:
cost1 += abs(no2 - no1)
if len(b1) != 0 or len(g1) != 0:
cost1 = 10000000000000000000000000007
while len(b2) > 0 and len(g2) > 0:
no1 = b2.pop()
no2 = g2.pop()
if type == 0:
cost2 += 1
else:
cost2 += abs(no2 - no1)
if len(b2) > 0 or len(g2) > 0:
cost2 = 100000000000000000000000000007
if (len(b1) > 0 or len(g1) > 0) and (len(b2) > 0 or len(g2) > 0):
print("-1")
continue
print(min(cost1, cost2)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR STRING IF VAR BIN_OP VAR NUMBER STRING VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Devu is a class teacher of a class of n students. One day, in the morning prayer of the school, all the students of his class were standing in a line. You are given information of their arrangement by a string s. The string s consists of only letters 'B' and 'G', where 'B' represents a boy and 'G' represents a girl.
Devu wants inter-gender interaction among his class should to be maximum. So he does not like seeing two or more boys/girls standing nearby (i.e. continuous) in the line. e.g. he does not like the arrangements BBG and GBB, but he likes BG, GBG etc.
Now by seeing the initial arrangement s of students, Devu may get furious and now he wants to change this arrangement into a likable arrangement. For achieving that, he can swap positions of any two students (not necessary continuous). Let the cost of swapping people from position i with position j (i β j) be c(i, j). You are provided an integer variable type, then the cost of the the swap will be defined by c(i, j) = |j β i|type.
Please help Devu in finding minimum cost of swaps needed to convert the current arrangement into a likable one.
-----Input-----
The first line of input contains an integer T, denoting the number of test cases. Then T test cases are follow.
The first line of each test case contains an integer type, denoting the type of the cost function. Then the next line contains string s of length n, denoting the initial arrangement s of students.
Note that the integer n is not given explicitly in input.
-----Output-----
For each test case, print a single line containing the answer of the test case, that is, the minimum cost to convert the current arrangement into a likable one. If it is not possible to convert the current arrangement into a likable one, then print -1 instead of the minimum cost.
-----Constraints and Subtasks-----Subtask 1: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 0
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 2: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 1
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 3: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 2
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 4: 25 points
- 1 β€ T β€ 102
- 1 β€ n β€ 103
- type can be 0, 1 or 2, that is type β {0, 1, 2}.
-----Example-----
Input:
8
0
BB
0
BG
0
BBGG
1
BGG
1
BGGB
1
BBBGG
2
BBGG
2
BGB
Output:
-1
0
1
1
1
3
1
0
-----Explanation-----
Note type of the first 3 test cases is 0. So c(i, j) = 1. Hence we just have to count minimum number of swaps needed.
Example case 1. There is no way to make sure that both the boys does not stand nearby. So answer is -1.
Example case 2. Arrangement is already valid. No swap is needed. So answer is 0.
Example case 3. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is 1.
Now type of the next 3 test cases is 1. So c(i, j) = |j β i|, that is, the absolute value of the difference between i and j.
Example case 4. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBG which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 5. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBGB which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 6. Swap boy at position 1 with girl at position 4. After swap the arrangement will be BGBGB which is a valid arrangement. So answer is |4 - 1| = 3.
Then type of the last 2 test cases is 2. So c(i, j) = (j β i)2
Example case 7. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is (2 - 1)2 = 1.
Example case 8. Arrangement is already valid. No swap is needed. So answer is 0. | def sums(a, b):
return sum([abs(a[i] - b[i]) for i in range(0, len(a))])
def cost(s, t, pt=False):
lb = []
lg = []
rb = []
rg = []
nb = ng = 0
for i in range(0, len(s)):
if s[i] == "B":
nb += 1
if i % 2:
lb.append(i)
else:
rb.append(i)
else:
ng += 1
if i % 2 == 0:
lg.append(i)
else:
rg.append(i)
if pt:
print(s, t, nb, ng, lb, lg, rb, rg)
if nb - ng > 1 or nb - ng < -1:
return -1
if nb - ng == -1 or len(rb) == 0:
lb = rb
lg = rg
if pt:
print(s, t, nb, ng, lb, lg, rb, rg)
assert len(lb) == len(lg)
if t == 0:
if nb == ng:
return min(len(lb), len(rb))
else:
return len(lb)
elif len(rb) == len(rg):
return min(sums(lb, lg), sums(rb, rg))
else:
return sums(lb, lg)
for i in range(int(input())):
t = int(input())
s = input()
print(cost(s, t)) | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FUNC_DEF NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Devu is a class teacher of a class of n students. One day, in the morning prayer of the school, all the students of his class were standing in a line. You are given information of their arrangement by a string s. The string s consists of only letters 'B' and 'G', where 'B' represents a boy and 'G' represents a girl.
Devu wants inter-gender interaction among his class should to be maximum. So he does not like seeing two or more boys/girls standing nearby (i.e. continuous) in the line. e.g. he does not like the arrangements BBG and GBB, but he likes BG, GBG etc.
Now by seeing the initial arrangement s of students, Devu may get furious and now he wants to change this arrangement into a likable arrangement. For achieving that, he can swap positions of any two students (not necessary continuous). Let the cost of swapping people from position i with position j (i β j) be c(i, j). You are provided an integer variable type, then the cost of the the swap will be defined by c(i, j) = |j β i|type.
Please help Devu in finding minimum cost of swaps needed to convert the current arrangement into a likable one.
-----Input-----
The first line of input contains an integer T, denoting the number of test cases. Then T test cases are follow.
The first line of each test case contains an integer type, denoting the type of the cost function. Then the next line contains string s of length n, denoting the initial arrangement s of students.
Note that the integer n is not given explicitly in input.
-----Output-----
For each test case, print a single line containing the answer of the test case, that is, the minimum cost to convert the current arrangement into a likable one. If it is not possible to convert the current arrangement into a likable one, then print -1 instead of the minimum cost.
-----Constraints and Subtasks-----Subtask 1: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 0
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 2: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 1
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 3: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 2
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 4: 25 points
- 1 β€ T β€ 102
- 1 β€ n β€ 103
- type can be 0, 1 or 2, that is type β {0, 1, 2}.
-----Example-----
Input:
8
0
BB
0
BG
0
BBGG
1
BGG
1
BGGB
1
BBBGG
2
BBGG
2
BGB
Output:
-1
0
1
1
1
3
1
0
-----Explanation-----
Note type of the first 3 test cases is 0. So c(i, j) = 1. Hence we just have to count minimum number of swaps needed.
Example case 1. There is no way to make sure that both the boys does not stand nearby. So answer is -1.
Example case 2. Arrangement is already valid. No swap is needed. So answer is 0.
Example case 3. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is 1.
Now type of the next 3 test cases is 1. So c(i, j) = |j β i|, that is, the absolute value of the difference between i and j.
Example case 4. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBG which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 5. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBGB which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 6. Swap boy at position 1 with girl at position 4. After swap the arrangement will be BGBGB which is a valid arrangement. So answer is |4 - 1| = 3.
Then type of the last 2 test cases is 2. So c(i, j) = (j β i)2
Example case 7. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is (2 - 1)2 = 1.
Example case 8. Arrangement is already valid. No swap is needed. So answer is 0. | def actual_generator(a, N, start):
result = []
if a == "B":
result = ["B", "G"] * (N // 2)
result.append("B")
elif a == "G":
result = ["G", "B"] * (N // 2)
result.append("G")
elif start == "B":
result = ["B", "G"] * (N // 2)
elif start == "G":
result = ["G", "B"] * (N // 2)
return result
for _ in range(int(input())):
typ = int(input())
s = input()
N = len(s)
array = [s[i] for i in range(N)]
countG = array.count("G")
countB = array.count("B")
if abs(countG - countB) > 1:
print(-1)
else:
defectB = []
defectG = []
if countG == countB:
actual1 = actual_generator("X", N, "B")
actual2 = actual_generator("X", N, "G")
defect1B = []
defect1G = []
defect2B = []
defect2G = []
for i in range(N):
if actual1[i] != array[i] and actual1[i] == "B":
defect1B.append(i)
elif actual1[i] != array[i] and actual1[i] == "G":
defect1G.append(i)
for i in range(N):
if actual2[i] != array[i] and actual2[i] == "B":
defect2B.append(i)
elif actual2[i] != array[i] and actual2[i] == "G":
defect2G.append(i)
if len(defect1B) > len(defect2B):
defectG = defect2G
defectB = defect2B
else:
defectG = defect1G
defectB = defect1B
elif countG > countB:
actual = actual_generator("G", N, "X")
defectB = []
defectG = []
for i in range(N):
if actual[i] != array[i] and actual[i] == "B":
defectB.append(i)
elif actual[i] != array[i] and actual[i] == "G":
defectG.append(i)
elif countB > countG:
actual = actual_generator("B", N, "X")
defectB = []
defectG = []
for i in range(N):
if actual[i] != array[i] and actual[i] == "B":
defectB.append(i)
elif actual[i] != array[i] and actual[i] == "G":
defectG.append(i)
if typ == 0:
print(len(defectB))
elif typ == 1:
print(abs(sum(defectG) - sum(defectB))) | FUNC_DEF ASSIGN VAR LIST IF VAR STRING ASSIGN VAR BIN_OP LIST STRING STRING BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR STRING ASSIGN VAR BIN_OP LIST STRING STRING BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR STRING ASSIGN VAR BIN_OP LIST STRING STRING BIN_OP VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP LIST STRING STRING BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST IF VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR STRING ASSIGN VAR FUNC_CALL VAR STRING VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Devu is a class teacher of a class of n students. One day, in the morning prayer of the school, all the students of his class were standing in a line. You are given information of their arrangement by a string s. The string s consists of only letters 'B' and 'G', where 'B' represents a boy and 'G' represents a girl.
Devu wants inter-gender interaction among his class should to be maximum. So he does not like seeing two or more boys/girls standing nearby (i.e. continuous) in the line. e.g. he does not like the arrangements BBG and GBB, but he likes BG, GBG etc.
Now by seeing the initial arrangement s of students, Devu may get furious and now he wants to change this arrangement into a likable arrangement. For achieving that, he can swap positions of any two students (not necessary continuous). Let the cost of swapping people from position i with position j (i β j) be c(i, j). You are provided an integer variable type, then the cost of the the swap will be defined by c(i, j) = |j β i|type.
Please help Devu in finding minimum cost of swaps needed to convert the current arrangement into a likable one.
-----Input-----
The first line of input contains an integer T, denoting the number of test cases. Then T test cases are follow.
The first line of each test case contains an integer type, denoting the type of the cost function. Then the next line contains string s of length n, denoting the initial arrangement s of students.
Note that the integer n is not given explicitly in input.
-----Output-----
For each test case, print a single line containing the answer of the test case, that is, the minimum cost to convert the current arrangement into a likable one. If it is not possible to convert the current arrangement into a likable one, then print -1 instead of the minimum cost.
-----Constraints and Subtasks-----Subtask 1: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 0
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 2: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 1
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 3: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 2
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 4: 25 points
- 1 β€ T β€ 102
- 1 β€ n β€ 103
- type can be 0, 1 or 2, that is type β {0, 1, 2}.
-----Example-----
Input:
8
0
BB
0
BG
0
BBGG
1
BGG
1
BGGB
1
BBBGG
2
BBGG
2
BGB
Output:
-1
0
1
1
1
3
1
0
-----Explanation-----
Note type of the first 3 test cases is 0. So c(i, j) = 1. Hence we just have to count minimum number of swaps needed.
Example case 1. There is no way to make sure that both the boys does not stand nearby. So answer is -1.
Example case 2. Arrangement is already valid. No swap is needed. So answer is 0.
Example case 3. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is 1.
Now type of the next 3 test cases is 1. So c(i, j) = |j β i|, that is, the absolute value of the difference between i and j.
Example case 4. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBG which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 5. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBGB which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 6. Swap boy at position 1 with girl at position 4. After swap the arrangement will be BGBGB which is a valid arrangement. So answer is |4 - 1| = 3.
Then type of the last 2 test cases is 2. So c(i, j) = (j β i)2
Example case 7. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is (2 - 1)2 = 1.
Example case 8. Arrangement is already valid. No swap is needed. So answer is 0. | def solve(s, c):
bcount = s.count("B")
gcount = s.count("G")
if abs(bcount - gcount) > 1:
return -1
B = []
G = []
start = "B"
cost = 0
if bcount < gcount:
start = "G"
iterate = 1
temp = start
if bcount == gcount:
iterate = 2
for y in range(iterate):
for i in range(len(s)):
if s[i] != start:
if s[i] == "B":
if len(G) > 0:
t = G.pop(0)
cost += abs(t - i) ** c
else:
B.append(i)
elif len(B) > 0:
t = B.pop(0)
cost += pow(abs(t - i), c, 10**6)
else:
G.append(i)
if start == "B":
start = "G"
else:
start = "B"
if temp == "B":
start = "G"
else:
start = "B"
if y == 1:
break
eqcost = cost
cost = 0
B = []
G = []
if bcount == gcount:
return min(eqcost, cost)
return eqcost
for t in range(int(input())):
c = int(input())
s = input()
print(solve(s, c)) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR STRING IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING IF VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST IF VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Devu is a class teacher of a class of n students. One day, in the morning prayer of the school, all the students of his class were standing in a line. You are given information of their arrangement by a string s. The string s consists of only letters 'B' and 'G', where 'B' represents a boy and 'G' represents a girl.
Devu wants inter-gender interaction among his class should to be maximum. So he does not like seeing two or more boys/girls standing nearby (i.e. continuous) in the line. e.g. he does not like the arrangements BBG and GBB, but he likes BG, GBG etc.
Now by seeing the initial arrangement s of students, Devu may get furious and now he wants to change this arrangement into a likable arrangement. For achieving that, he can swap positions of any two students (not necessary continuous). Let the cost of swapping people from position i with position j (i β j) be c(i, j). You are provided an integer variable type, then the cost of the the swap will be defined by c(i, j) = |j β i|type.
Please help Devu in finding minimum cost of swaps needed to convert the current arrangement into a likable one.
-----Input-----
The first line of input contains an integer T, denoting the number of test cases. Then T test cases are follow.
The first line of each test case contains an integer type, denoting the type of the cost function. Then the next line contains string s of length n, denoting the initial arrangement s of students.
Note that the integer n is not given explicitly in input.
-----Output-----
For each test case, print a single line containing the answer of the test case, that is, the minimum cost to convert the current arrangement into a likable one. If it is not possible to convert the current arrangement into a likable one, then print -1 instead of the minimum cost.
-----Constraints and Subtasks-----Subtask 1: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 0
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 2: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 1
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 3: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 2
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 4: 25 points
- 1 β€ T β€ 102
- 1 β€ n β€ 103
- type can be 0, 1 or 2, that is type β {0, 1, 2}.
-----Example-----
Input:
8
0
BB
0
BG
0
BBGG
1
BGG
1
BGGB
1
BBBGG
2
BBGG
2
BGB
Output:
-1
0
1
1
1
3
1
0
-----Explanation-----
Note type of the first 3 test cases is 0. So c(i, j) = 1. Hence we just have to count minimum number of swaps needed.
Example case 1. There is no way to make sure that both the boys does not stand nearby. So answer is -1.
Example case 2. Arrangement is already valid. No swap is needed. So answer is 0.
Example case 3. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is 1.
Now type of the next 3 test cases is 1. So c(i, j) = |j β i|, that is, the absolute value of the difference between i and j.
Example case 4. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBG which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 5. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBGB which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 6. Swap boy at position 1 with girl at position 4. After swap the arrangement will be BGBGB which is a valid arrangement. So answer is |4 - 1| = 3.
Then type of the last 2 test cases is 2. So c(i, j) = (j β i)2
Example case 7. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is (2 - 1)2 = 1.
Example case 8. Arrangement is already valid. No swap is needed. So answer is 0. | t = int(input())
while t > 0:
k = int(input())
s = input()
b = s.count("B")
g = s.count("G")
m = []
n = []
c = []
d = []
if b == g + 1:
s = s + "G"
i = 0
while i < len(s):
if s[i] != "B":
m.append(i)
if s[i + 1] != "G":
n.append(i + 1)
i = i + 2
if k == 0:
print(len(m))
elif k == 1 or k == 2:
y = 0
for i in range(len(m)):
y = y + abs(m[i] - n[i])
print(y)
elif g == b + 1:
s = s + "B"
i = 0
while i < len(s):
if s[i] != "G":
n.append(i)
if s[i + 1] != "B":
m.append(i + 1)
i = i + 2
if k == 0:
print(len(m))
elif k == 1 or k == 2:
y = 0
for i in range(len(m)):
y = y + abs(m[i] - n[i])
print(y)
elif b == g:
i = 0
while i < len(s):
if s[i] != "B":
m.append(i)
if s[i + 1] != "G":
n.append(i + 1)
i = i + 2
i = 0
while i < len(s):
if s[i] != "G":
c.append(i)
if s[i + 1] != "B":
d.append(i + 1)
i = i + 2
if k == 0:
print(min(len(c), len(m)))
elif k == 1 or k == 2:
y = 0
z = 0
for i in range(len(m)):
y = y + abs(m[i] - n[i])
for i in range(len(c)):
z = z + abs(c[i] - d[i])
print(min(z, y))
else:
print("-1")
t = t - 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER |
Devu is a class teacher of a class of n students. One day, in the morning prayer of the school, all the students of his class were standing in a line. You are given information of their arrangement by a string s. The string s consists of only letters 'B' and 'G', where 'B' represents a boy and 'G' represents a girl.
Devu wants inter-gender interaction among his class should to be maximum. So he does not like seeing two or more boys/girls standing nearby (i.e. continuous) in the line. e.g. he does not like the arrangements BBG and GBB, but he likes BG, GBG etc.
Now by seeing the initial arrangement s of students, Devu may get furious and now he wants to change this arrangement into a likable arrangement. For achieving that, he can swap positions of any two students (not necessary continuous). Let the cost of swapping people from position i with position j (i β j) be c(i, j). You are provided an integer variable type, then the cost of the the swap will be defined by c(i, j) = |j β i|type.
Please help Devu in finding minimum cost of swaps needed to convert the current arrangement into a likable one.
-----Input-----
The first line of input contains an integer T, denoting the number of test cases. Then T test cases are follow.
The first line of each test case contains an integer type, denoting the type of the cost function. Then the next line contains string s of length n, denoting the initial arrangement s of students.
Note that the integer n is not given explicitly in input.
-----Output-----
For each test case, print a single line containing the answer of the test case, that is, the minimum cost to convert the current arrangement into a likable one. If it is not possible to convert the current arrangement into a likable one, then print -1 instead of the minimum cost.
-----Constraints and Subtasks-----Subtask 1: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 0
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 2: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 1
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 3: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 2
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 4: 25 points
- 1 β€ T β€ 102
- 1 β€ n β€ 103
- type can be 0, 1 or 2, that is type β {0, 1, 2}.
-----Example-----
Input:
8
0
BB
0
BG
0
BBGG
1
BGG
1
BGGB
1
BBBGG
2
BBGG
2
BGB
Output:
-1
0
1
1
1
3
1
0
-----Explanation-----
Note type of the first 3 test cases is 0. So c(i, j) = 1. Hence we just have to count minimum number of swaps needed.
Example case 1. There is no way to make sure that both the boys does not stand nearby. So answer is -1.
Example case 2. Arrangement is already valid. No swap is needed. So answer is 0.
Example case 3. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is 1.
Now type of the next 3 test cases is 1. So c(i, j) = |j β i|, that is, the absolute value of the difference between i and j.
Example case 4. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBG which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 5. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBGB which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 6. Swap boy at position 1 with girl at position 4. After swap the arrangement will be BGBGB which is a valid arrangement. So answer is |4 - 1| = 3.
Then type of the last 2 test cases is 2. So c(i, j) = (j β i)2
Example case 7. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is (2 - 1)2 = 1.
Example case 8. Arrangement is already valid. No swap is needed. So answer is 0. | t = int(input())
for i in range(1, t + 1):
type1 = int(input())
s = input()
count1 = count2 = 0
countB = countG = 0
if type1 == 0:
for j in range(0, len(s)):
if s[j] == "B":
countB += 1
else:
countG += 1
if j % 2 == 0:
if s[j] != "B":
count1 += 1
else:
count2 += 1
elif s[j] != "G":
count1 += 1
else:
count2 += 1
if abs(countB - countG) <= 1:
if count1 == 0 or count2 == 0:
print("0")
elif len(s) % 2 == 1:
if countB > countG:
print(count1 // 2)
else:
print(count2 // 2)
else:
print(min(count1, count2) // 2)
else:
print("-1")
else:
Bmis1 = []
Bmis2 = []
Gmis1 = []
Gmis2 = []
for j in range(0, len(s)):
if s[j] == "B":
countB += 1
else:
countG += 1
if j % 2 == 0:
if s[j] != "B":
Gmis1.append(j)
if Bmis1:
count1 = count1 + abs(Gmis1.pop() - Bmis1.pop())
else:
Bmis2.append(j)
if Gmis2:
count2 = count2 + abs(Gmis2.pop() - Bmis2.pop())
elif s[j] != "G":
Bmis1.append(j)
if Gmis1:
count1 = count1 + abs(Gmis1.pop() - Bmis1.pop())
else:
Gmis2.append(j)
if Bmis2:
count2 = count2 + abs(Gmis2.pop() - Bmis2.pop())
if abs(countB - countG) <= 1:
if (
count1 == 0
and (Bmis1 == [] and Gmis1 == [])
or count2 == 0
and (Bmis2 == [] and Gmis2 == [])
):
print("0")
elif len(s) % 2 == 1:
if countB > countG:
print(count1)
else:
print(count2)
else:
print(min(count1, count2))
else:
print("-1") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR LIST VAR LIST VAR NUMBER VAR LIST VAR LIST EXPR FUNC_CALL VAR STRING IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING |
Devu is a class teacher of a class of n students. One day, in the morning prayer of the school, all the students of his class were standing in a line. You are given information of their arrangement by a string s. The string s consists of only letters 'B' and 'G', where 'B' represents a boy and 'G' represents a girl.
Devu wants inter-gender interaction among his class should to be maximum. So he does not like seeing two or more boys/girls standing nearby (i.e. continuous) in the line. e.g. he does not like the arrangements BBG and GBB, but he likes BG, GBG etc.
Now by seeing the initial arrangement s of students, Devu may get furious and now he wants to change this arrangement into a likable arrangement. For achieving that, he can swap positions of any two students (not necessary continuous). Let the cost of swapping people from position i with position j (i β j) be c(i, j). You are provided an integer variable type, then the cost of the the swap will be defined by c(i, j) = |j β i|type.
Please help Devu in finding minimum cost of swaps needed to convert the current arrangement into a likable one.
-----Input-----
The first line of input contains an integer T, denoting the number of test cases. Then T test cases are follow.
The first line of each test case contains an integer type, denoting the type of the cost function. Then the next line contains string s of length n, denoting the initial arrangement s of students.
Note that the integer n is not given explicitly in input.
-----Output-----
For each test case, print a single line containing the answer of the test case, that is, the minimum cost to convert the current arrangement into a likable one. If it is not possible to convert the current arrangement into a likable one, then print -1 instead of the minimum cost.
-----Constraints and Subtasks-----Subtask 1: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 0
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 2: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 1
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 3: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 2
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 4: 25 points
- 1 β€ T β€ 102
- 1 β€ n β€ 103
- type can be 0, 1 or 2, that is type β {0, 1, 2}.
-----Example-----
Input:
8
0
BB
0
BG
0
BBGG
1
BGG
1
BGGB
1
BBBGG
2
BBGG
2
BGB
Output:
-1
0
1
1
1
3
1
0
-----Explanation-----
Note type of the first 3 test cases is 0. So c(i, j) = 1. Hence we just have to count minimum number of swaps needed.
Example case 1. There is no way to make sure that both the boys does not stand nearby. So answer is -1.
Example case 2. Arrangement is already valid. No swap is needed. So answer is 0.
Example case 3. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is 1.
Now type of the next 3 test cases is 1. So c(i, j) = |j β i|, that is, the absolute value of the difference between i and j.
Example case 4. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBG which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 5. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBGB which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 6. Swap boy at position 1 with girl at position 4. After swap the arrangement will be BGBGB which is a valid arrangement. So answer is |4 - 1| = 3.
Then type of the last 2 test cases is 2. So c(i, j) = (j β i)2
Example case 7. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is (2 - 1)2 = 1.
Example case 8. Arrangement is already valid. No swap is needed. So answer is 0. | for _ in range(int(input())):
t = int(input())
s = input()
n = len(s)
b = 0
for i in s:
if i == "B":
b += 1
g = n - b
if abs(b - g) > 1:
print("-1")
elif b == 1 and g == 1:
print("0")
elif b > g:
BG = "BG" * (n // 2)
if n & 1:
BG += "B"
B, G = [], []
for i in range(n):
if s[i] != BG[i]:
if s[i] == "B":
B.append(i)
else:
G.append(i)
if t == 0:
print(len(B))
else:
print(sum([abs(B[i] - G[i]) for i in range(len(B))]))
elif b < g:
BG = "GB" * (n // 2)
if n & 1:
BG += "G"
B, G = [], []
for i in range(n):
if s[i] != BG[i]:
if s[i] == "B":
B.append(i)
else:
G.append(i)
if t == 0:
print(len(B))
else:
print(sum([abs(B[i] - G[i]) for i in range(len(B))]))
else:
BG = "GB" * (n // 2)
if n & 1:
BG += "G"
B, G = [], []
for i in range(n):
if s[i] != BG[i]:
if s[i] == "B":
B.append(i)
else:
G.append(i)
if t == 0:
v1 = len(B)
else:
v1 = sum([abs(B[i] - G[i]) for i in range(len(B))])
BG = "BG" * (n // 2)
if n & 1:
BG += "B"
B, G = [], []
for i in range(n):
if s[i] != BG[i]:
if s[i] == "B":
B.append(i)
else:
G.append(i)
if t == 0:
v2 = len(B)
else:
v2 = sum([abs(B[i] - G[i]) for i in range(len(B))])
print(min(v1, v2)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR BIN_OP STRING BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR STRING ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP STRING BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR STRING ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR STRING ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP STRING BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR STRING ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Devu is a class teacher of a class of n students. One day, in the morning prayer of the school, all the students of his class were standing in a line. You are given information of their arrangement by a string s. The string s consists of only letters 'B' and 'G', where 'B' represents a boy and 'G' represents a girl.
Devu wants inter-gender interaction among his class should to be maximum. So he does not like seeing two or more boys/girls standing nearby (i.e. continuous) in the line. e.g. he does not like the arrangements BBG and GBB, but he likes BG, GBG etc.
Now by seeing the initial arrangement s of students, Devu may get furious and now he wants to change this arrangement into a likable arrangement. For achieving that, he can swap positions of any two students (not necessary continuous). Let the cost of swapping people from position i with position j (i β j) be c(i, j). You are provided an integer variable type, then the cost of the the swap will be defined by c(i, j) = |j β i|type.
Please help Devu in finding minimum cost of swaps needed to convert the current arrangement into a likable one.
-----Input-----
The first line of input contains an integer T, denoting the number of test cases. Then T test cases are follow.
The first line of each test case contains an integer type, denoting the type of the cost function. Then the next line contains string s of length n, denoting the initial arrangement s of students.
Note that the integer n is not given explicitly in input.
-----Output-----
For each test case, print a single line containing the answer of the test case, that is, the minimum cost to convert the current arrangement into a likable one. If it is not possible to convert the current arrangement into a likable one, then print -1 instead of the minimum cost.
-----Constraints and Subtasks-----Subtask 1: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 0
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 2: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 1
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 3: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 2
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 4: 25 points
- 1 β€ T β€ 102
- 1 β€ n β€ 103
- type can be 0, 1 or 2, that is type β {0, 1, 2}.
-----Example-----
Input:
8
0
BB
0
BG
0
BBGG
1
BGG
1
BGGB
1
BBBGG
2
BBGG
2
BGB
Output:
-1
0
1
1
1
3
1
0
-----Explanation-----
Note type of the first 3 test cases is 0. So c(i, j) = 1. Hence we just have to count minimum number of swaps needed.
Example case 1. There is no way to make sure that both the boys does not stand nearby. So answer is -1.
Example case 2. Arrangement is already valid. No swap is needed. So answer is 0.
Example case 3. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is 1.
Now type of the next 3 test cases is 1. So c(i, j) = |j β i|, that is, the absolute value of the difference between i and j.
Example case 4. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBG which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 5. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBGB which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 6. Swap boy at position 1 with girl at position 4. After swap the arrangement will be BGBGB which is a valid arrangement. So answer is |4 - 1| = 3.
Then type of the last 2 test cases is 2. So c(i, j) = (j β i)2
Example case 7. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is (2 - 1)2 = 1.
Example case 8. Arrangement is already valid. No swap is needed. So answer is 0. | t = int(input())
for test in range(t):
k, s, ans = int(input()), input(), 0
cnt, odd, even, even_cnt = [0, 0], [0, 0], [0, 0], [0, 0]
for i, ch in enumerate(s):
index = 0 if ch == "B" else 1
odd[index] += abs(i - cnt[index] * 2)
even[index] += abs(i - cnt[index] * 2 - 1)
cnt[index] += 1
even_cnt[index] += int(i % 2)
if cnt[0] == cnt[1]:
ans = min(even_cnt[0], cnt[0] - even_cnt[0]) if k == 0 else min(odd[0], even[0])
elif cnt[0] == cnt[1] - 1:
ans = even[0] if k else even_cnt[1]
elif cnt[0] == cnt[1] + 1:
ans = even[1] if k else even_cnt[0]
else:
ans = -1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR STRING NUMBER NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
Devu is a class teacher of a class of n students. One day, in the morning prayer of the school, all the students of his class were standing in a line. You are given information of their arrangement by a string s. The string s consists of only letters 'B' and 'G', where 'B' represents a boy and 'G' represents a girl.
Devu wants inter-gender interaction among his class should to be maximum. So he does not like seeing two or more boys/girls standing nearby (i.e. continuous) in the line. e.g. he does not like the arrangements BBG and GBB, but he likes BG, GBG etc.
Now by seeing the initial arrangement s of students, Devu may get furious and now he wants to change this arrangement into a likable arrangement. For achieving that, he can swap positions of any two students (not necessary continuous). Let the cost of swapping people from position i with position j (i β j) be c(i, j). You are provided an integer variable type, then the cost of the the swap will be defined by c(i, j) = |j β i|type.
Please help Devu in finding minimum cost of swaps needed to convert the current arrangement into a likable one.
-----Input-----
The first line of input contains an integer T, denoting the number of test cases. Then T test cases are follow.
The first line of each test case contains an integer type, denoting the type of the cost function. Then the next line contains string s of length n, denoting the initial arrangement s of students.
Note that the integer n is not given explicitly in input.
-----Output-----
For each test case, print a single line containing the answer of the test case, that is, the minimum cost to convert the current arrangement into a likable one. If it is not possible to convert the current arrangement into a likable one, then print -1 instead of the minimum cost.
-----Constraints and Subtasks-----Subtask 1: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 0
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 2: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 1
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 3: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 2
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 4: 25 points
- 1 β€ T β€ 102
- 1 β€ n β€ 103
- type can be 0, 1 or 2, that is type β {0, 1, 2}.
-----Example-----
Input:
8
0
BB
0
BG
0
BBGG
1
BGG
1
BGGB
1
BBBGG
2
BBGG
2
BGB
Output:
-1
0
1
1
1
3
1
0
-----Explanation-----
Note type of the first 3 test cases is 0. So c(i, j) = 1. Hence we just have to count minimum number of swaps needed.
Example case 1. There is no way to make sure that both the boys does not stand nearby. So answer is -1.
Example case 2. Arrangement is already valid. No swap is needed. So answer is 0.
Example case 3. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is 1.
Now type of the next 3 test cases is 1. So c(i, j) = |j β i|, that is, the absolute value of the difference between i and j.
Example case 4. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBG which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 5. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBGB which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 6. Swap boy at position 1 with girl at position 4. After swap the arrangement will be BGBGB which is a valid arrangement. So answer is |4 - 1| = 3.
Then type of the last 2 test cases is 2. So c(i, j) = (j β i)2
Example case 7. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is (2 - 1)2 = 1.
Example case 8. Arrangement is already valid. No swap is needed. So answer is 0. | __author__ = "sanjay"
def min_count(string, tp):
B, G = [], []
for i in range(len(string)):
if i % 2 != 0:
G.append("B")
B.append("G")
else:
G.append("G")
B.append("B")
count = string.count("B") - string.count("G")
if abs(count) > 1:
return -1
elif count > 0:
b, g = [], []
for i in range(len(string)):
if string[i] != B[i]:
if i % 2 != 0:
b.append(i)
else:
g.append(i)
ans = 0
for i in range(len(b)):
ans += pow(abs(b[i] - g[i]), tp)
return ans
elif count < 0:
b, g = [], []
for i in range(len(string)):
if string[i] != G[i]:
if i % 2 != 0:
g.append(i)
else:
b.append(i)
ans = 0
for i in range(len(b)):
ans += pow(abs(b[i] - g[i]), tp)
return ans
elif count == 0:
b, g = [], []
for i in range(len(string)):
if string[i] != G[i]:
if i % 2 != 0:
g.append(i)
else:
b.append(i)
ans1 = 0
for i in range(len(b)):
ans1 += pow(abs(b[i] - g[i]), tp)
b, g = [], []
for i in range(len(string)):
if string[i] != B[i]:
if i % 2 != 0:
b.append(i)
else:
g.append(i)
ans2 = 0
for i in range(len(b)):
ans2 += pow(abs(b[i] - g[i]), tp)
return min(ans1, ans2)
test = int(input())
while test:
test -= 1
tp = int(input())
if tp == 2:
tp = 1
string = input()
print(min_count(string, tp)) | ASSIGN VAR STRING FUNC_DEF ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR IF VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR IF VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Devu is a class teacher of a class of n students. One day, in the morning prayer of the school, all the students of his class were standing in a line. You are given information of their arrangement by a string s. The string s consists of only letters 'B' and 'G', where 'B' represents a boy and 'G' represents a girl.
Devu wants inter-gender interaction among his class should to be maximum. So he does not like seeing two or more boys/girls standing nearby (i.e. continuous) in the line. e.g. he does not like the arrangements BBG and GBB, but he likes BG, GBG etc.
Now by seeing the initial arrangement s of students, Devu may get furious and now he wants to change this arrangement into a likable arrangement. For achieving that, he can swap positions of any two students (not necessary continuous). Let the cost of swapping people from position i with position j (i β j) be c(i, j). You are provided an integer variable type, then the cost of the the swap will be defined by c(i, j) = |j β i|type.
Please help Devu in finding minimum cost of swaps needed to convert the current arrangement into a likable one.
-----Input-----
The first line of input contains an integer T, denoting the number of test cases. Then T test cases are follow.
The first line of each test case contains an integer type, denoting the type of the cost function. Then the next line contains string s of length n, denoting the initial arrangement s of students.
Note that the integer n is not given explicitly in input.
-----Output-----
For each test case, print a single line containing the answer of the test case, that is, the minimum cost to convert the current arrangement into a likable one. If it is not possible to convert the current arrangement into a likable one, then print -1 instead of the minimum cost.
-----Constraints and Subtasks-----Subtask 1: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 0
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 2: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 1
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 3: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 2
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 4: 25 points
- 1 β€ T β€ 102
- 1 β€ n β€ 103
- type can be 0, 1 or 2, that is type β {0, 1, 2}.
-----Example-----
Input:
8
0
BB
0
BG
0
BBGG
1
BGG
1
BGGB
1
BBBGG
2
BBGG
2
BGB
Output:
-1
0
1
1
1
3
1
0
-----Explanation-----
Note type of the first 3 test cases is 0. So c(i, j) = 1. Hence we just have to count minimum number of swaps needed.
Example case 1. There is no way to make sure that both the boys does not stand nearby. So answer is -1.
Example case 2. Arrangement is already valid. No swap is needed. So answer is 0.
Example case 3. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is 1.
Now type of the next 3 test cases is 1. So c(i, j) = |j β i|, that is, the absolute value of the difference between i and j.
Example case 4. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBG which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 5. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBGB which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 6. Swap boy at position 1 with girl at position 4. After swap the arrangement will be BGBGB which is a valid arrangement. So answer is |4 - 1| = 3.
Then type of the last 2 test cases is 2. So c(i, j) = (j β i)2
Example case 7. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is (2 - 1)2 = 1.
Example case 8. Arrangement is already valid. No swap is needed. So answer is 0. | def outOfIndex(boys, girls, COST):
if COST == 0:
return len(boys)
else:
total_cost = [abs(x - y) for x, y in zip(boys, girls)]
total_cost = sum(total_cost)
return total_cost
for _ in range(int(input())):
COST = int(input())
queue = input()
B = queue.count("B")
G = queue.count("G")
boys = []
girls = []
if abs(B - G) > 1:
print(-1)
elif B > G:
for c in range(len(queue)):
if c % 2 != 0 and queue[c] == "B":
boys.append(c)
if c % 2 == 0 and queue[c] == "G":
girls.append(c)
print(outOfIndex(boys, girls, COST))
boys.clear()
girls.clear()
elif B < G:
for c in range(len(queue)):
if c % 2 != 0 and queue[c] == "G":
girls.append(c)
if c % 2 == 0 and queue[c] == "B":
boys.append(c)
print(outOfIndex(boys, girls, COST))
boys.clear()
girls.clear()
else:
for c in range(len(queue)):
if c % 2 != 0 and queue[c] == "B":
boys.append(c)
if c % 2 == 0 and queue[c] == "G":
girls.append(c)
attempt1 = outOfIndex(boys, girls, COST)
boys.clear()
girls.clear()
for c in range(len(queue)):
if c % 2 != 0 and queue[c] == "G":
girls.append(c)
if c % 2 == 0 and queue[c] == "B":
boys.append(c)
attempt2 = outOfIndex(boys, girls, COST)
print(min(attempt1, attempt2))
boys.clear()
girls.clear() | FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Devu is a class teacher of a class of n students. One day, in the morning prayer of the school, all the students of his class were standing in a line. You are given information of their arrangement by a string s. The string s consists of only letters 'B' and 'G', where 'B' represents a boy and 'G' represents a girl.
Devu wants inter-gender interaction among his class should to be maximum. So he does not like seeing two or more boys/girls standing nearby (i.e. continuous) in the line. e.g. he does not like the arrangements BBG and GBB, but he likes BG, GBG etc.
Now by seeing the initial arrangement s of students, Devu may get furious and now he wants to change this arrangement into a likable arrangement. For achieving that, he can swap positions of any two students (not necessary continuous). Let the cost of swapping people from position i with position j (i β j) be c(i, j). You are provided an integer variable type, then the cost of the the swap will be defined by c(i, j) = |j β i|type.
Please help Devu in finding minimum cost of swaps needed to convert the current arrangement into a likable one.
-----Input-----
The first line of input contains an integer T, denoting the number of test cases. Then T test cases are follow.
The first line of each test case contains an integer type, denoting the type of the cost function. Then the next line contains string s of length n, denoting the initial arrangement s of students.
Note that the integer n is not given explicitly in input.
-----Output-----
For each test case, print a single line containing the answer of the test case, that is, the minimum cost to convert the current arrangement into a likable one. If it is not possible to convert the current arrangement into a likable one, then print -1 instead of the minimum cost.
-----Constraints and Subtasks-----Subtask 1: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 0
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 2: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 1
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 3: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 2
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 4: 25 points
- 1 β€ T β€ 102
- 1 β€ n β€ 103
- type can be 0, 1 or 2, that is type β {0, 1, 2}.
-----Example-----
Input:
8
0
BB
0
BG
0
BBGG
1
BGG
1
BGGB
1
BBBGG
2
BBGG
2
BGB
Output:
-1
0
1
1
1
3
1
0
-----Explanation-----
Note type of the first 3 test cases is 0. So c(i, j) = 1. Hence we just have to count minimum number of swaps needed.
Example case 1. There is no way to make sure that both the boys does not stand nearby. So answer is -1.
Example case 2. Arrangement is already valid. No swap is needed. So answer is 0.
Example case 3. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is 1.
Now type of the next 3 test cases is 1. So c(i, j) = |j β i|, that is, the absolute value of the difference between i and j.
Example case 4. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBG which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 5. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBGB which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 6. Swap boy at position 1 with girl at position 4. After swap the arrangement will be BGBGB which is a valid arrangement. So answer is |4 - 1| = 3.
Then type of the last 2 test cases is 2. So c(i, j) = (j β i)2
Example case 7. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is (2 - 1)2 = 1.
Example case 8. Arrangement is already valid. No swap is needed. So answer is 0. | for _ in range(int(input())):
t = int(input())
s = input()
b = 0
g = 0
for i in s:
if i == "B":
b += 1
else:
g += 1
if abs(g - b) > 1:
print("-1")
elif g - b > 0:
gl = []
bl = []
for i in range(len(s)):
if s[i] == "B" and i % 2 == 0:
bl.append(i)
elif s[i] == "G" and i % 2 == 1:
gl.append(i)
c = 0
for i in range(len(bl)):
if t == 0:
c += 1
else:
c += abs(gl[i] - bl[i])
print(c)
elif b - g > 0:
gl = []
bl = []
for i in range(len(s)):
if s[i] == "B" and i % 2 == 1:
bl.append(i)
elif s[i] == "G" and i % 2 == 0:
gl.append(i)
c = 0
for i in range(len(bl)):
if t == 0:
c += 1
else:
c += abs(gl[i] - bl[i])
print(c)
else:
gl = []
bl = []
for i in range(len(s)):
if s[i] == "B" and i % 2 == 0:
bl.append(i)
elif s[i] == "G" and i % 2 == 1:
gl.append(i)
c1 = 0
for i in range(len(bl)):
if t == 0:
c1 += 1
else:
c1 += abs(gl[i] - bl[i])
gl = []
bl = []
for i in range(len(s)):
if s[i] == "B" and i % 2 == 1:
bl.append(i)
elif s[i] == "G" and i % 2 == 0:
gl.append(i)
c2 = 0
for i in range(len(bl)):
if t == 0:
c2 += 1
else:
c2 += abs(gl[i] - bl[i])
if c2 < c1:
print(c2)
else:
print(c1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR STRING BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR STRING BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR STRING BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR STRING BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Devu is a class teacher of a class of n students. One day, in the morning prayer of the school, all the students of his class were standing in a line. You are given information of their arrangement by a string s. The string s consists of only letters 'B' and 'G', where 'B' represents a boy and 'G' represents a girl.
Devu wants inter-gender interaction among his class should to be maximum. So he does not like seeing two or more boys/girls standing nearby (i.e. continuous) in the line. e.g. he does not like the arrangements BBG and GBB, but he likes BG, GBG etc.
Now by seeing the initial arrangement s of students, Devu may get furious and now he wants to change this arrangement into a likable arrangement. For achieving that, he can swap positions of any two students (not necessary continuous). Let the cost of swapping people from position i with position j (i β j) be c(i, j). You are provided an integer variable type, then the cost of the the swap will be defined by c(i, j) = |j β i|type.
Please help Devu in finding minimum cost of swaps needed to convert the current arrangement into a likable one.
-----Input-----
The first line of input contains an integer T, denoting the number of test cases. Then T test cases are follow.
The first line of each test case contains an integer type, denoting the type of the cost function. Then the next line contains string s of length n, denoting the initial arrangement s of students.
Note that the integer n is not given explicitly in input.
-----Output-----
For each test case, print a single line containing the answer of the test case, that is, the minimum cost to convert the current arrangement into a likable one. If it is not possible to convert the current arrangement into a likable one, then print -1 instead of the minimum cost.
-----Constraints and Subtasks-----Subtask 1: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 0
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 2: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 1
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 3: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 2
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 4: 25 points
- 1 β€ T β€ 102
- 1 β€ n β€ 103
- type can be 0, 1 or 2, that is type β {0, 1, 2}.
-----Example-----
Input:
8
0
BB
0
BG
0
BBGG
1
BGG
1
BGGB
1
BBBGG
2
BBGG
2
BGB
Output:
-1
0
1
1
1
3
1
0
-----Explanation-----
Note type of the first 3 test cases is 0. So c(i, j) = 1. Hence we just have to count minimum number of swaps needed.
Example case 1. There is no way to make sure that both the boys does not stand nearby. So answer is -1.
Example case 2. Arrangement is already valid. No swap is needed. So answer is 0.
Example case 3. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is 1.
Now type of the next 3 test cases is 1. So c(i, j) = |j β i|, that is, the absolute value of the difference between i and j.
Example case 4. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBG which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 5. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBGB which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 6. Swap boy at position 1 with girl at position 4. After swap the arrangement will be BGBGB which is a valid arrangement. So answer is |4 - 1| = 3.
Then type of the last 2 test cases is 2. So c(i, j) = (j β i)2
Example case 7. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is (2 - 1)2 = 1.
Example case 8. Arrangement is already valid. No swap is needed. So answer is 0. | for _ in range(int(input())):
t = int(input())
s = input()
b = []
a = []
c = 0
lb = 0
la = 0
r = []
ls = len(s)
for i in s:
c += 1
if i == "B":
lb += 1
else:
la += 1
if la - lb == 0 or abs(la - lb) == 1:
if la < lb:
for i in range(ls):
if i % 2 == 1 and s[i] == "B":
b.append(i)
if i % 2 == 0 and s[i] == "G":
a.append(i)
elif la > lb:
for i in range(ls):
if i % 2 == 0 and s[i] == "B":
b.append(i)
if i % 2 == 1 and s[i] == "G":
a.append(i)
cs = 0
sw = 0
for i in range(len(a)):
cs += abs(a[i] - b[i])
if t == 0:
cs = len(a)
if la == lb:
for i in range(len(s)):
if i % 2 == 0 and s[i] == "B":
b.append(i)
if i % 2 == 1 and s[i] == "G":
a.append(i)
cs = 0
sw = 0
for i in range(len(a)):
cs += abs(a[i] - b[i])
if t == 0:
cs = len(a)
x = cs
b = []
a = []
for i in range(len(s)):
if i % 2 == 1 and s[i] == "B":
b.append(i)
if i % 2 == 0 and s[i] == "G":
a.append(i)
cs = 0
sw = 0
for i in range(len(a)):
if i < len(b):
cs += abs(a[i] - b[i])
if t == 0:
cs = len(a)
cs = min(x, cs)
print(cs)
else:
print(-1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Devu is a class teacher of a class of n students. One day, in the morning prayer of the school, all the students of his class were standing in a line. You are given information of their arrangement by a string s. The string s consists of only letters 'B' and 'G', where 'B' represents a boy and 'G' represents a girl.
Devu wants inter-gender interaction among his class should to be maximum. So he does not like seeing two or more boys/girls standing nearby (i.e. continuous) in the line. e.g. he does not like the arrangements BBG and GBB, but he likes BG, GBG etc.
Now by seeing the initial arrangement s of students, Devu may get furious and now he wants to change this arrangement into a likable arrangement. For achieving that, he can swap positions of any two students (not necessary continuous). Let the cost of swapping people from position i with position j (i β j) be c(i, j). You are provided an integer variable type, then the cost of the the swap will be defined by c(i, j) = |j β i|type.
Please help Devu in finding minimum cost of swaps needed to convert the current arrangement into a likable one.
-----Input-----
The first line of input contains an integer T, denoting the number of test cases. Then T test cases are follow.
The first line of each test case contains an integer type, denoting the type of the cost function. Then the next line contains string s of length n, denoting the initial arrangement s of students.
Note that the integer n is not given explicitly in input.
-----Output-----
For each test case, print a single line containing the answer of the test case, that is, the minimum cost to convert the current arrangement into a likable one. If it is not possible to convert the current arrangement into a likable one, then print -1 instead of the minimum cost.
-----Constraints and Subtasks-----Subtask 1: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 0
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 2: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 1
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 3: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 2
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 4: 25 points
- 1 β€ T β€ 102
- 1 β€ n β€ 103
- type can be 0, 1 or 2, that is type β {0, 1, 2}.
-----Example-----
Input:
8
0
BB
0
BG
0
BBGG
1
BGG
1
BGGB
1
BBBGG
2
BBGG
2
BGB
Output:
-1
0
1
1
1
3
1
0
-----Explanation-----
Note type of the first 3 test cases is 0. So c(i, j) = 1. Hence we just have to count minimum number of swaps needed.
Example case 1. There is no way to make sure that both the boys does not stand nearby. So answer is -1.
Example case 2. Arrangement is already valid. No swap is needed. So answer is 0.
Example case 3. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is 1.
Now type of the next 3 test cases is 1. So c(i, j) = |j β i|, that is, the absolute value of the difference between i and j.
Example case 4. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBG which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 5. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBGB which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 6. Swap boy at position 1 with girl at position 4. After swap the arrangement will be BGBGB which is a valid arrangement. So answer is |4 - 1| = 3.
Then type of the last 2 test cases is 2. So c(i, j) = (j β i)2
Example case 7. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is (2 - 1)2 = 1.
Example case 8. Arrangement is already valid. No swap is needed. So answer is 0. | def compute(s, c):
bcount = s.count("B")
gcount = s.count("G")
if abs(bcount - gcount) > 1:
return -1
stack = [(0, s[0])]
bans = 0
gans = 0
if c != 0:
c = 1
for i in range(1, len(s)):
if len(stack) > 0 and stack[len(stack) - 1][1] != s[i]:
idx, char = stack[len(stack) - 1]
if idx % 2 and s[i] == "B" or i % 2 and char == "B":
bans += abs(i - idx) ** c
elif idx % 2 and s[i] == "G" or i % 2 and char == "G":
gans += abs(i - idx) ** c
stack.pop(len(stack) - 1)
else:
stack.append((i, s[i]))
if gcount == bcount:
return min(bans, gans)
elif gcount > bcount:
return bans
return gans
for t in range(int(input())):
c = int(input())
print(compute(input(), c)) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR STRING BIN_OP VAR NUMBER VAR STRING VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR STRING BIN_OP VAR NUMBER VAR STRING VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR IF VAR VAR RETURN VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR |
Devu is a class teacher of a class of n students. One day, in the morning prayer of the school, all the students of his class were standing in a line. You are given information of their arrangement by a string s. The string s consists of only letters 'B' and 'G', where 'B' represents a boy and 'G' represents a girl.
Devu wants inter-gender interaction among his class should to be maximum. So he does not like seeing two or more boys/girls standing nearby (i.e. continuous) in the line. e.g. he does not like the arrangements BBG and GBB, but he likes BG, GBG etc.
Now by seeing the initial arrangement s of students, Devu may get furious and now he wants to change this arrangement into a likable arrangement. For achieving that, he can swap positions of any two students (not necessary continuous). Let the cost of swapping people from position i with position j (i β j) be c(i, j). You are provided an integer variable type, then the cost of the the swap will be defined by c(i, j) = |j β i|type.
Please help Devu in finding minimum cost of swaps needed to convert the current arrangement into a likable one.
-----Input-----
The first line of input contains an integer T, denoting the number of test cases. Then T test cases are follow.
The first line of each test case contains an integer type, denoting the type of the cost function. Then the next line contains string s of length n, denoting the initial arrangement s of students.
Note that the integer n is not given explicitly in input.
-----Output-----
For each test case, print a single line containing the answer of the test case, that is, the minimum cost to convert the current arrangement into a likable one. If it is not possible to convert the current arrangement into a likable one, then print -1 instead of the minimum cost.
-----Constraints and Subtasks-----Subtask 1: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 0
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 2: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 1
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 3: 25 points
- 1 β€ T β€ 105
- 1 β€ n β€ 105
- type = 2
- Sum of n over all the test cases in one test file does not exceed 106.
Subtask 4: 25 points
- 1 β€ T β€ 102
- 1 β€ n β€ 103
- type can be 0, 1 or 2, that is type β {0, 1, 2}.
-----Example-----
Input:
8
0
BB
0
BG
0
BBGG
1
BGG
1
BGGB
1
BBBGG
2
BBGG
2
BGB
Output:
-1
0
1
1
1
3
1
0
-----Explanation-----
Note type of the first 3 test cases is 0. So c(i, j) = 1. Hence we just have to count minimum number of swaps needed.
Example case 1. There is no way to make sure that both the boys does not stand nearby. So answer is -1.
Example case 2. Arrangement is already valid. No swap is needed. So answer is 0.
Example case 3. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is 1.
Now type of the next 3 test cases is 1. So c(i, j) = |j β i|, that is, the absolute value of the difference between i and j.
Example case 4. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBG which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 5. Swap boy at position 0 with girl at position 1. After swap the arrangement will be GBGB which is a valid arrangement. So answer is |1 - 0| = 1.
Example case 6. Swap boy at position 1 with girl at position 4. After swap the arrangement will be BGBGB which is a valid arrangement. So answer is |4 - 1| = 3.
Then type of the last 2 test cases is 2. So c(i, j) = (j β i)2
Example case 7. Swap boy at position 1 with girl at position 2. After swap the arrangement will be BGBG which is a valid arrangement. So answer is (2 - 1)2 = 1.
Example case 8. Arrangement is already valid. No swap is needed. So answer is 0. | t = int(input())
for _ in range(t):
type = int(input())
global c
c = 0
c1 = 0
c2 = 0
s = input()
b1 = []
g1 = []
b2 = []
g2 = []
b = s.count("B")
g = s.count("G")
if abs(g - b) > 1:
print("-1")
else:
if b > g:
for k in range(len(s)):
if k % 2 == 0 and s[k] != "B":
b1.append(k)
if k % 2 != 0 and s[k] != "G":
g1.append(k)
for i in range(len(b1)):
sb = abs(g1[i] - b1[i])
if type != 2:
c = c + sb**type
else:
c = c + sb
if g > b:
for k in range(len(s)):
if k % 2 == 0 and s[k] != "G":
g1.append(k)
if k % 2 != 0 and s[k] != "B":
b1.append(k)
for i in range(len(g1)):
sb = abs(g1[i] - b1[i])
if type != 2:
c = c + sb**type
else:
c = c + sb
if g == b:
if True:
for k in range(len(s)):
if k % 2 == 0 and s[k] != "B":
b1.append(k)
if k % 2 != 0 and s[k] != "G":
g1.append(k)
for i in range(len(b1)):
sb = abs(g1[i] - b1[i])
if type != 2:
c1 = c1 + sb**type
else:
c1 = c1 + sb
if True:
for k in range(len(s)):
if k % 2 == 0 and s[k] != "G":
g2.append(k)
if k % 2 != 0 and s[k] != "B":
b2.append(k)
for i in range(len(g2)):
sb = abs(g2[i] - b2[i])
if type != 2:
c2 = c2 + sb**type
else:
c2 = c2 + sb
if c1 > c2:
c = c2
else:
c = c1
print(int(c)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
There are N children standing in a line. Each child is assigned a rating value given in the integer array ratings.
You are giving candies to these children subjected to the following requirements:
Each child must have atleast one candy.
Children with a higher rating get more candies than neighbors.
Return the minimum number of candies you need to have to distribute the candies to the children.
Example 1:
Input:
N = 3
ratings [ ] = {1, 0, 2}
Output: 5
Explanation:
You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
Example 2:
Input:
N = 3
ratings [ ] = {1, 2, 2}
Output: 4
Explanation:
You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
The third child gets 1 candy because it statisfies the above two conditions.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minCandy() which takes the interger N and integer array ratings[ ] as parameters and returns the minimum number of candies you need to have to distribute the candies to the children.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 5*10^{4}
0 β€ ratings_{i} β€ 10^{5} | class Solution:
def minCandy(self, N, rat):
l2r = [1] * N
r2l = [1] * N
for i in range(1, N):
if rat[i] > rat[i - 1]:
l2r[i] = l2r[i - 1] + 1
for i in range(N - 2, -1, -1):
if rat[i] > rat[i + 1]:
r2l[i] = r2l[i + 1] + 1
total = 0
for i in range(N):
total += max(l2r[i], r2l[i])
return total | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR |
There are N children standing in a line. Each child is assigned a rating value given in the integer array ratings.
You are giving candies to these children subjected to the following requirements:
Each child must have atleast one candy.
Children with a higher rating get more candies than neighbors.
Return the minimum number of candies you need to have to distribute the candies to the children.
Example 1:
Input:
N = 3
ratings [ ] = {1, 0, 2}
Output: 5
Explanation:
You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
Example 2:
Input:
N = 3
ratings [ ] = {1, 2, 2}
Output: 4
Explanation:
You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
The third child gets 1 candy because it statisfies the above two conditions.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minCandy() which takes the interger N and integer array ratings[ ] as parameters and returns the minimum number of candies you need to have to distribute the candies to the children.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 5*10^{4}
0 β€ ratings_{i} β€ 10^{5} | class Solution:
def minCandy(self, N, ratings):
left = [1] * N
right = [1] * N
for i in range(1, N):
if ratings[i] > ratings[i - 1]:
left[i] = left[i - 1] + 1
if ratings[N - 1 - i] > ratings[N - i]:
right[N - 1 - i] = right[N - i] + 1
s = 0
for i in range(N):
if left[i] > right[i]:
s += left[i]
else:
s += right[i] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR |
There are N children standing in a line. Each child is assigned a rating value given in the integer array ratings.
You are giving candies to these children subjected to the following requirements:
Each child must have atleast one candy.
Children with a higher rating get more candies than neighbors.
Return the minimum number of candies you need to have to distribute the candies to the children.
Example 1:
Input:
N = 3
ratings [ ] = {1, 0, 2}
Output: 5
Explanation:
You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
Example 2:
Input:
N = 3
ratings [ ] = {1, 2, 2}
Output: 4
Explanation:
You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
The third child gets 1 candy because it statisfies the above two conditions.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minCandy() which takes the interger N and integer array ratings[ ] as parameters and returns the minimum number of candies you need to have to distribute the candies to the children.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 5*10^{4}
0 β€ ratings_{i} β€ 10^{5} | class Solution:
def minCandy(self, N, ratings):
arr = ratings
n = N
sum = 0
ans = [1] * n
if n == 1:
return 1
for i in range(n - 1):
if arr[i + 1] > arr[i]:
ans[i + 1] = ans[i] + 1
for i in range(n - 2, -1, -1):
if arr[i] > arr[i + 1] and ans[i] <= ans[i + 1]:
ans[i] = ans[i + 1] + 1
sum += ans[i]
sum += ans[n - 1]
return sum | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR |
There are N children standing in a line. Each child is assigned a rating value given in the integer array ratings.
You are giving candies to these children subjected to the following requirements:
Each child must have atleast one candy.
Children with a higher rating get more candies than neighbors.
Return the minimum number of candies you need to have to distribute the candies to the children.
Example 1:
Input:
N = 3
ratings [ ] = {1, 0, 2}
Output: 5
Explanation:
You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
Example 2:
Input:
N = 3
ratings [ ] = {1, 2, 2}
Output: 4
Explanation:
You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
The third child gets 1 candy because it statisfies the above two conditions.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minCandy() which takes the interger N and integer array ratings[ ] as parameters and returns the minimum number of candies you need to have to distribute the candies to the children.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 5*10^{4}
0 β€ ratings_{i} β€ 10^{5} | class Solution:
def minCandy(self, N, ratings):
L = [1] * len(ratings)
R = [1] * len(ratings)
for i in range(1, len(ratings)):
if ratings[i] > ratings[i - 1]:
L[i] += L[i - 1]
for i in range(len(ratings) - 2, -1, -1):
if ratings[i] > ratings[i + 1]:
R[i] += R[i + 1]
cnd = 0
for i in range(len(ratings)):
cnd += max(L[i], R[i])
return cnd | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR |
There are N children standing in a line. Each child is assigned a rating value given in the integer array ratings.
You are giving candies to these children subjected to the following requirements:
Each child must have atleast one candy.
Children with a higher rating get more candies than neighbors.
Return the minimum number of candies you need to have to distribute the candies to the children.
Example 1:
Input:
N = 3
ratings [ ] = {1, 0, 2}
Output: 5
Explanation:
You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
Example 2:
Input:
N = 3
ratings [ ] = {1, 2, 2}
Output: 4
Explanation:
You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
The third child gets 1 candy because it statisfies the above two conditions.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minCandy() which takes the interger N and integer array ratings[ ] as parameters and returns the minimum number of candies you need to have to distribute the candies to the children.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 5*10^{4}
0 β€ ratings_{i} β€ 10^{5} | class Solution:
def minCandy(self, N, ratings):
candies = [1] * N
prev_r = float("inf")
for i, r in enumerate(ratings):
if r > prev_r:
candies[i] = candies[i - 1] + 1
prev_r = r
prev_r = float("inf")
for i in reversed(range(N)):
r = ratings[i]
if r > prev_r:
candies[i] = max(candies[i], candies[i + 1] + 1)
prev_r = r
return sum(candies) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR |
There are N children standing in a line. Each child is assigned a rating value given in the integer array ratings.
You are giving candies to these children subjected to the following requirements:
Each child must have atleast one candy.
Children with a higher rating get more candies than neighbors.
Return the minimum number of candies you need to have to distribute the candies to the children.
Example 1:
Input:
N = 3
ratings [ ] = {1, 0, 2}
Output: 5
Explanation:
You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
Example 2:
Input:
N = 3
ratings [ ] = {1, 2, 2}
Output: 4
Explanation:
You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
The third child gets 1 candy because it statisfies the above two conditions.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minCandy() which takes the interger N and integer array ratings[ ] as parameters and returns the minimum number of candies you need to have to distribute the candies to the children.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 5*10^{4}
0 β€ ratings_{i} β€ 10^{5} | class Solution:
def minCandy(self, N, ratings):
candies = [(1) for i in range(len(ratings))]
for i in range(1, len(ratings)):
if ratings[i] > ratings[i - 1]:
candies[i] = candies[i - 1] + 1
for i in range(len(ratings) - 2, -1, -1):
if ratings[i] > ratings[i + 1]:
candies[i] = max(candies[i], candies[i + 1] + 1)
ans = sum(candies)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR |
There are N children standing in a line. Each child is assigned a rating value given in the integer array ratings.
You are giving candies to these children subjected to the following requirements:
Each child must have atleast one candy.
Children with a higher rating get more candies than neighbors.
Return the minimum number of candies you need to have to distribute the candies to the children.
Example 1:
Input:
N = 3
ratings [ ] = {1, 0, 2}
Output: 5
Explanation:
You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
Example 2:
Input:
N = 3
ratings [ ] = {1, 2, 2}
Output: 4
Explanation:
You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
The third child gets 1 candy because it statisfies the above two conditions.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minCandy() which takes the interger N and integer array ratings[ ] as parameters and returns the minimum number of candies you need to have to distribute the candies to the children.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 5*10^{4}
0 β€ ratings_{i} β€ 10^{5} | class Solution:
def minCandy(self, N, ratings):
left = [1] * N
for i in range(1, N):
if ratings[i] > ratings[i - 1]:
left[i] += left[i - 1]
right = [1] * N
for i in range(N - 2, -1, -1):
if ratings[i] > ratings[i + 1]:
right[i] += right[i + 1]
cand = [0] * N
s = 0
for i in range(N):
cand[i] = max(left[i], right[i])
s += cand[i]
return s | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR |
There are N children standing in a line. Each child is assigned a rating value given in the integer array ratings.
You are giving candies to these children subjected to the following requirements:
Each child must have atleast one candy.
Children with a higher rating get more candies than neighbors.
Return the minimum number of candies you need to have to distribute the candies to the children.
Example 1:
Input:
N = 3
ratings [ ] = {1, 0, 2}
Output: 5
Explanation:
You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
Example 2:
Input:
N = 3
ratings [ ] = {1, 2, 2}
Output: 4
Explanation:
You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
The third child gets 1 candy because it statisfies the above two conditions.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minCandy() which takes the interger N and integer array ratings[ ] as parameters and returns the minimum number of candies you need to have to distribute the candies to the children.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 5*10^{4}
0 β€ ratings_{i} β€ 10^{5} | class Solution:
def minCandy(self, N, ratings):
left_candy = [(1) for i in range(N)]
for i in range(1, N):
if ratings[i] > ratings[i - 1]:
left_candy[i] = left_candy[i - 1] + 1
right_candy = 1
for j in range(N - 2, -1, -1):
if ratings[j] > ratings[j + 1]:
right_candy += 1
left_candy[j] = max(left_candy[j], right_candy)
else:
right_candy = 1
sum1 = 0
for i in range(0, N):
sum1 += left_candy[i]
return sum1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR RETURN VAR |
There are N children standing in a line. Each child is assigned a rating value given in the integer array ratings.
You are giving candies to these children subjected to the following requirements:
Each child must have atleast one candy.
Children with a higher rating get more candies than neighbors.
Return the minimum number of candies you need to have to distribute the candies to the children.
Example 1:
Input:
N = 3
ratings [ ] = {1, 0, 2}
Output: 5
Explanation:
You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
Example 2:
Input:
N = 3
ratings [ ] = {1, 2, 2}
Output: 4
Explanation:
You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
The third child gets 1 candy because it statisfies the above two conditions.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minCandy() which takes the interger N and integer array ratings[ ] as parameters and returns the minimum number of candies you need to have to distribute the candies to the children.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 5*10^{4}
0 β€ ratings_{i} β€ 10^{5} | class Solution:
def minCandy(self, N, ratings):
left = [1] * N
right = [1] * N
res = [0] * N
for i in range(1, N):
if ratings[i] > ratings[i - 1]:
left[i] = 1 + left[i - 1]
for i in range(N - 2, -1, -1):
if ratings[i] > ratings[i + 1]:
right[i] = 1 + right[i + 1]
for i in range(N):
res[i] = max(left[i], right[i])
return sum(res) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR |
There are N children standing in a line. Each child is assigned a rating value given in the integer array ratings.
You are giving candies to these children subjected to the following requirements:
Each child must have atleast one candy.
Children with a higher rating get more candies than neighbors.
Return the minimum number of candies you need to have to distribute the candies to the children.
Example 1:
Input:
N = 3
ratings [ ] = {1, 0, 2}
Output: 5
Explanation:
You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
Example 2:
Input:
N = 3
ratings [ ] = {1, 2, 2}
Output: 4
Explanation:
You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
The third child gets 1 candy because it statisfies the above two conditions.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minCandy() which takes the interger N and integer array ratings[ ] as parameters and returns the minimum number of candies you need to have to distribute the candies to the children.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 5*10^{4}
0 β€ ratings_{i} β€ 10^{5} | class Solution:
def minCandy(self, N, ratings):
L2R = [(1) for i in range(len(ratings))]
R2L = [(1) for i in range(len(ratings))]
ratings2 = ratings[::-1]
total = 0
for i in range(1, len(ratings)):
if ratings[i] > ratings[i - 1]:
L2R[i] = L2R[i - 1] + 1
if ratings2[i] > ratings2[i - 1]:
R2L[i] = R2L[i - 1] + 1
R2L = R2L[::-1]
for i in range(len(ratings)):
total += max(L2R[i], R2L[i])
return total | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR |
There are N children standing in a line. Each child is assigned a rating value given in the integer array ratings.
You are giving candies to these children subjected to the following requirements:
Each child must have atleast one candy.
Children with a higher rating get more candies than neighbors.
Return the minimum number of candies you need to have to distribute the candies to the children.
Example 1:
Input:
N = 3
ratings [ ] = {1, 0, 2}
Output: 5
Explanation:
You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
Example 2:
Input:
N = 3
ratings [ ] = {1, 2, 2}
Output: 4
Explanation:
You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
The third child gets 1 candy because it statisfies the above two conditions.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minCandy() which takes the interger N and integer array ratings[ ] as parameters and returns the minimum number of candies you need to have to distribute the candies to the children.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 5*10^{4}
0 β€ ratings_{i} β€ 10^{5} | class Solution:
def minCandy(self, N, ratings):
candies = [1] * N
for i in range(1, N):
if ratings[i] > ratings[i - 1] and candies[i] <= candies[i - 1]:
candies[i] = candies[i - 1] + 1
for i in range(N - 2, -1, -1):
if ratings[i] > ratings[i + 1] and candies[i] <= candies[i + 1]:
candies[i] = candies[i + 1] + 1
return sum(candies) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR |
There are N children standing in a line. Each child is assigned a rating value given in the integer array ratings.
You are giving candies to these children subjected to the following requirements:
Each child must have atleast one candy.
Children with a higher rating get more candies than neighbors.
Return the minimum number of candies you need to have to distribute the candies to the children.
Example 1:
Input:
N = 3
ratings [ ] = {1, 0, 2}
Output: 5
Explanation:
You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
Example 2:
Input:
N = 3
ratings [ ] = {1, 2, 2}
Output: 4
Explanation:
You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
The third child gets 1 candy because it statisfies the above two conditions.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minCandy() which takes the interger N and integer array ratings[ ] as parameters and returns the minimum number of candies you need to have to distribute the candies to the children.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 5*10^{4}
0 β€ ratings_{i} β€ 10^{5} | class Solution:
def minCandy(self, N, ratings):
c = [1] * N
for i in range(1, N):
if ratings[i] > ratings[i - 1]:
c[i] = c[i - 1] + 1
for i in range(N - 2, -1, -1):
if ratings[i] > ratings[i + 1]:
c[i] = max(c[i], c[i + 1] + 1)
res = 0
for t in c:
res += t
return res | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR RETURN VAR |
There are N children standing in a line. Each child is assigned a rating value given in the integer array ratings.
You are giving candies to these children subjected to the following requirements:
Each child must have atleast one candy.
Children with a higher rating get more candies than neighbors.
Return the minimum number of candies you need to have to distribute the candies to the children.
Example 1:
Input:
N = 3
ratings [ ] = {1, 0, 2}
Output: 5
Explanation:
You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
Example 2:
Input:
N = 3
ratings [ ] = {1, 2, 2}
Output: 4
Explanation:
You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
The third child gets 1 candy because it statisfies the above two conditions.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minCandy() which takes the interger N and integer array ratings[ ] as parameters and returns the minimum number of candies you need to have to distribute the candies to the children.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 5*10^{4}
0 β€ ratings_{i} β€ 10^{5} | class Solution:
def minCandy(self, N, ratings):
l = [(1) for _ in range(N)]
r = [(1) for _ in range(N)]
for i in range(1, N):
if ratings[i] > ratings[i - 1] and l[i] <= l[i - 1]:
l[i] = l[i - 1] + 1
for i in range(N - 2, -1, -1):
if ratings[i] > ratings[i + 1] and r[i] <= r[i + 1]:
r[i] = r[i + 1] + 1
ans = 0
for i in range(N):
ans += max(l[i], r[i])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR |
There are N children standing in a line. Each child is assigned a rating value given in the integer array ratings.
You are giving candies to these children subjected to the following requirements:
Each child must have atleast one candy.
Children with a higher rating get more candies than neighbors.
Return the minimum number of candies you need to have to distribute the candies to the children.
Example 1:
Input:
N = 3
ratings [ ] = {1, 0, 2}
Output: 5
Explanation:
You can allocate to the first, second and third child with 2, 1, 2 candies respectively.
Example 2:
Input:
N = 3
ratings [ ] = {1, 2, 2}
Output: 4
Explanation:
You can allocate to the first, second and third child with 1, 2, 1 candies respectively.
The third child gets 1 candy because it statisfies the above two conditions.
Your Task:
You don't need to read input or print anything. Your task is to complete the function minCandy() which takes the interger N and integer array ratings[ ] as parameters and returns the minimum number of candies you need to have to distribute the candies to the children.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 5*10^{4}
0 β€ ratings_{i} β€ 10^{5} | class Solution:
def minCandy(self, N, ratings):
arr = [(1) for i in range(N)]
for i in range(1, N):
if ratings[i] > ratings[i - 1]:
arr[i] = max(arr[i], arr[i - 1] + 1)
for i in range(N - 2, -1, -1):
if ratings[i] > ratings[i + 1]:
arr[i] = max(arr[i], arr[i + 1] + 1)
return sum(arr) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR |
Monocarp has drawn a tree (an undirected connected acyclic graph) and then has given each vertex an index. All indices are distinct numbers from $1$ to $n$. For every edge $e$ of this tree, Monocarp has written two numbers: the maximum indices of the vertices of the two components formed if the edge $e$ (and only this edge) is erased from the tree.
Monocarp has given you a list of $n - 1$ pairs of numbers. He wants you to provide an example of a tree that will produce the said list if this tree exists. If such tree does not exist, say so.
-----Input-----
The first line contains one integer $n$ ($2 \le n \le 1\,000$)Β β the number of vertices in the tree.
Each of the next $n-1$ lines contains two integers $a_i$ and $b_i$ each ($1 \le a_i < b_i \le n$)Β β the maximal indices of vertices in the components formed if the $i$-th edge is removed.
-----Output-----
If there is no such tree that can produce the given list of pairs, print "NO" (without quotes).
Otherwise print "YES" (without quotes) in the first line and the edges of the tree in the next $n - 1$ lines. Each of the last $n - 1$ lines should contain two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$)Β β vertices connected by an edge.
Note: The numeration of edges doesn't matter for this task. Your solution will be considered correct if your tree produces the same pairs as given in the input file (possibly reordered). That means that you can print the edges of the tree you reconstructed in any order.
-----Examples-----
Input
4
3 4
1 4
3 4
Output
YES
1 3
3 2
2 4
Input
3
1 3
1 3
Output
NO
Input
3
1 2
2 3
Output
NO
-----Note-----
Possible tree from the first example. Dotted lines show edges you need to remove to get appropriate pairs. [Image] | from sys import stdin, stdout
def three_reconstruction(n, ab_a):
res = []
cnt = [0] * (n + 1)
for ab in ab_a:
a, b = ab
if a != n and b != n or a == b:
return [False]
cnt[min(a, b)] += 1
cur = 0
for i in range(1, n + 1):
cur += cnt[i]
if cur > i:
return [False]
hs = set()
for i in range(1, n + 1):
hs.add(i)
last = -1
for i in range(1, n + 1):
if cnt[i] > 0:
hs.remove(i)
if last != -1:
res.append([last, i])
last = i
cnt[i] -= 1
while cnt[i] > 0:
v = min(hs)
res.append([last, v])
last = v
cnt[i] -= 1
hs.remove(v)
res.append([last, n])
return [True, res]
n = int(stdin.readline())
ab_a = []
for _ in range(n - 1):
ab_a.append(list(map(int, stdin.readline().split())))
res = three_reconstruction(n, ab_a)
if res[0]:
stdout.write("YES\n")
for p in res[1]:
stdout.write(str(p[0]) + " " + str(p[1]) + "\n")
else:
stdout.write("NO\n") | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR VAR RETURN LIST NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR RETURN LIST NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR VAR VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER STRING FUNC_CALL VAR VAR NUMBER STRING EXPR FUNC_CALL VAR STRING |
Monocarp has drawn a tree (an undirected connected acyclic graph) and then has given each vertex an index. All indices are distinct numbers from $1$ to $n$. For every edge $e$ of this tree, Monocarp has written two numbers: the maximum indices of the vertices of the two components formed if the edge $e$ (and only this edge) is erased from the tree.
Monocarp has given you a list of $n - 1$ pairs of numbers. He wants you to provide an example of a tree that will produce the said list if this tree exists. If such tree does not exist, say so.
-----Input-----
The first line contains one integer $n$ ($2 \le n \le 1\,000$)Β β the number of vertices in the tree.
Each of the next $n-1$ lines contains two integers $a_i$ and $b_i$ each ($1 \le a_i < b_i \le n$)Β β the maximal indices of vertices in the components formed if the $i$-th edge is removed.
-----Output-----
If there is no such tree that can produce the given list of pairs, print "NO" (without quotes).
Otherwise print "YES" (without quotes) in the first line and the edges of the tree in the next $n - 1$ lines. Each of the last $n - 1$ lines should contain two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$)Β β vertices connected by an edge.
Note: The numeration of edges doesn't matter for this task. Your solution will be considered correct if your tree produces the same pairs as given in the input file (possibly reordered). That means that you can print the edges of the tree you reconstructed in any order.
-----Examples-----
Input
4
3 4
1 4
3 4
Output
YES
1 3
3 2
2 4
Input
3
1 3
1 3
Output
NO
Input
3
1 2
2 3
Output
NO
-----Note-----
Possible tree from the first example. Dotted lines show edges you need to remove to get appropriate pairs. [Image] | n = int(input())
V = []
for _ in range(n - 1):
a, b = map(int, input().split())
V.append(a)
if b < n:
print("NO")
quit()
V.sort()
for i in range(n - 1):
if V[i] <= i:
print("NO")
quit()
used = [False] * (n + 1)
tree = []
for i in range(n - 1):
v = V[i]
if not used[v]:
tree.append(v)
used[v] = True
else:
for j in range(1, n + 1):
if not used[j]:
tree.append(j)
used[j] = True
break
tree.append(n)
print("YES")
for i in range(n - 1):
print(tree[i], tree[i + 1]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER |
Monocarp has drawn a tree (an undirected connected acyclic graph) and then has given each vertex an index. All indices are distinct numbers from $1$ to $n$. For every edge $e$ of this tree, Monocarp has written two numbers: the maximum indices of the vertices of the two components formed if the edge $e$ (and only this edge) is erased from the tree.
Monocarp has given you a list of $n - 1$ pairs of numbers. He wants you to provide an example of a tree that will produce the said list if this tree exists. If such tree does not exist, say so.
-----Input-----
The first line contains one integer $n$ ($2 \le n \le 1\,000$)Β β the number of vertices in the tree.
Each of the next $n-1$ lines contains two integers $a_i$ and $b_i$ each ($1 \le a_i < b_i \le n$)Β β the maximal indices of vertices in the components formed if the $i$-th edge is removed.
-----Output-----
If there is no such tree that can produce the given list of pairs, print "NO" (without quotes).
Otherwise print "YES" (without quotes) in the first line and the edges of the tree in the next $n - 1$ lines. Each of the last $n - 1$ lines should contain two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$)Β β vertices connected by an edge.
Note: The numeration of edges doesn't matter for this task. Your solution will be considered correct if your tree produces the same pairs as given in the input file (possibly reordered). That means that you can print the edges of the tree you reconstructed in any order.
-----Examples-----
Input
4
3 4
1 4
3 4
Output
YES
1 3
3 2
2 4
Input
3
1 3
1 3
Output
NO
Input
3
1 2
2 3
Output
NO
-----Note-----
Possible tree from the first example. Dotted lines show edges you need to remove to get appropriate pairs. [Image] | from sys import stdin
def bad():
print("NO")
exit()
all_in = stdin.readlines()
n = int(all_in[0])
pair = list(map(lambda x: tuple(map(int, x.split())), all_in[1:]))
f = True
for i in range(n - 1):
if pair[i][0] == n - 1:
f = False
if pair[i][1] != n:
bad()
if f:
bad()
pair.sort(key=lambda x: x[0])
p = list(map(lambda x: x[0], pair))
ans = [(0) for i in range(n)]
st = set(range(1, n))
ans[0] = p[0]
st.remove(p[0])
a, b = 0, p[0]
for i in range(1, n - 1):
a, b = b, p[i]
if b != a:
ans[i] = b
st.remove(b)
ans[-1] = n
max_ = 0
for i in range(n):
el = ans[i]
max_ = max(max_, el)
if not el:
m = min(st)
if m > max_:
bad()
ans[i] = m
st.remove(m)
print("YES")
print("\n".join(map(lambda i: f"{ans[i - 1]} {ans[i]}", range(1, n)))) | FUNC_DEF EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING VAR VAR FUNC_CALL VAR NUMBER VAR |
Monocarp has drawn a tree (an undirected connected acyclic graph) and then has given each vertex an index. All indices are distinct numbers from $1$ to $n$. For every edge $e$ of this tree, Monocarp has written two numbers: the maximum indices of the vertices of the two components formed if the edge $e$ (and only this edge) is erased from the tree.
Monocarp has given you a list of $n - 1$ pairs of numbers. He wants you to provide an example of a tree that will produce the said list if this tree exists. If such tree does not exist, say so.
-----Input-----
The first line contains one integer $n$ ($2 \le n \le 1\,000$)Β β the number of vertices in the tree.
Each of the next $n-1$ lines contains two integers $a_i$ and $b_i$ each ($1 \le a_i < b_i \le n$)Β β the maximal indices of vertices in the components formed if the $i$-th edge is removed.
-----Output-----
If there is no such tree that can produce the given list of pairs, print "NO" (without quotes).
Otherwise print "YES" (without quotes) in the first line and the edges of the tree in the next $n - 1$ lines. Each of the last $n - 1$ lines should contain two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$)Β β vertices connected by an edge.
Note: The numeration of edges doesn't matter for this task. Your solution will be considered correct if your tree produces the same pairs as given in the input file (possibly reordered). That means that you can print the edges of the tree you reconstructed in any order.
-----Examples-----
Input
4
3 4
1 4
3 4
Output
YES
1 3
3 2
2 4
Input
3
1 3
1 3
Output
NO
Input
3
1 2
2 3
Output
NO
-----Note-----
Possible tree from the first example. Dotted lines show edges you need to remove to get appropriate pairs. [Image] | def solve():
n = int(input())
d = {}
for _ in range(n - 1):
u, v = map(int, input().split())
min_ = min(u, v)
max_ = max(u, v)
if max_ != n:
return False, None
if min_ not in d:
d[min_] = 0
d[min_] += 1
if sum(list(d.values())) + 1 != n:
return False, None
edge = []
used = {i: (False) for i in range(1, n + 1)}
for k in sorted(list(d.keys())):
used[k] = True
mid = [n]
for i in range(k - 1, 0, -1):
if len(mid) == d[k]:
break
if used[i] == False:
used[i] = True
mid.append(i)
if len(mid) < d[k]:
return False, None
mid.append(k)
for u, v in zip(mid[:-1], mid[1:]):
edge.append([u, v])
return True, edge
ans, arr = solve()
if ans == False:
print("NO")
else:
print("YES")
for u, v in arr:
print(str(u) + " " + str(v)) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN NUMBER NONE IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR RETURN NUMBER NONE ASSIGN VAR LIST ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER NONE EXPR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR RETURN NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR |
Monocarp has drawn a tree (an undirected connected acyclic graph) and then has given each vertex an index. All indices are distinct numbers from $1$ to $n$. For every edge $e$ of this tree, Monocarp has written two numbers: the maximum indices of the vertices of the two components formed if the edge $e$ (and only this edge) is erased from the tree.
Monocarp has given you a list of $n - 1$ pairs of numbers. He wants you to provide an example of a tree that will produce the said list if this tree exists. If such tree does not exist, say so.
-----Input-----
The first line contains one integer $n$ ($2 \le n \le 1\,000$)Β β the number of vertices in the tree.
Each of the next $n-1$ lines contains two integers $a_i$ and $b_i$ each ($1 \le a_i < b_i \le n$)Β β the maximal indices of vertices in the components formed if the $i$-th edge is removed.
-----Output-----
If there is no such tree that can produce the given list of pairs, print "NO" (without quotes).
Otherwise print "YES" (without quotes) in the first line and the edges of the tree in the next $n - 1$ lines. Each of the last $n - 1$ lines should contain two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$)Β β vertices connected by an edge.
Note: The numeration of edges doesn't matter for this task. Your solution will be considered correct if your tree produces the same pairs as given in the input file (possibly reordered). That means that you can print the edges of the tree you reconstructed in any order.
-----Examples-----
Input
4
3 4
1 4
3 4
Output
YES
1 3
3 2
2 4
Input
3
1 3
1 3
Output
NO
Input
3
1 2
2 3
Output
NO
-----Note-----
Possible tree from the first example. Dotted lines show edges you need to remove to get appropriate pairs. [Image] | n = int(input())
a = []
fr = [0] * 10000
for i in range(n - 1):
x, y = map(int, input().split())
if x == y or x < n and y < n:
print("NO")
exit(0)
if x == n:
a.append(y)
else:
a.append(x)
b = []
for i in range(1, n + 1):
if i not in a:
b.append(i)
for i in a:
if fr[i] == 0:
fr[i] = n
continue
tep = 0
record = 0
for j in range(len(b)):
if b[j] and b[j] < i:
if b[j] > tep:
tep = b[j]
record = j
b[record] = 0
if tep == 0:
print("NO")
exit(0)
fr[tep] = fr[i]
fr[i] = tep
print("YES")
for i in range(1, n):
print(i, fr[i]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR |
Monocarp has drawn a tree (an undirected connected acyclic graph) and then has given each vertex an index. All indices are distinct numbers from $1$ to $n$. For every edge $e$ of this tree, Monocarp has written two numbers: the maximum indices of the vertices of the two components formed if the edge $e$ (and only this edge) is erased from the tree.
Monocarp has given you a list of $n - 1$ pairs of numbers. He wants you to provide an example of a tree that will produce the said list if this tree exists. If such tree does not exist, say so.
-----Input-----
The first line contains one integer $n$ ($2 \le n \le 1\,000$)Β β the number of vertices in the tree.
Each of the next $n-1$ lines contains two integers $a_i$ and $b_i$ each ($1 \le a_i < b_i \le n$)Β β the maximal indices of vertices in the components formed if the $i$-th edge is removed.
-----Output-----
If there is no such tree that can produce the given list of pairs, print "NO" (without quotes).
Otherwise print "YES" (without quotes) in the first line and the edges of the tree in the next $n - 1$ lines. Each of the last $n - 1$ lines should contain two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$)Β β vertices connected by an edge.
Note: The numeration of edges doesn't matter for this task. Your solution will be considered correct if your tree produces the same pairs as given in the input file (possibly reordered). That means that you can print the edges of the tree you reconstructed in any order.
-----Examples-----
Input
4
3 4
1 4
3 4
Output
YES
1 3
3 2
2 4
Input
3
1 3
1 3
Output
NO
Input
3
1 2
2 3
Output
NO
-----Note-----
Possible tree from the first example. Dotted lines show edges you need to remove to get appropriate pairs. [Image] | input = __import__("sys").stdin.readline
n = int(input())
lis = []
fre = [0] * 10005
c1 = 0
ans = [[0, 0]]
has = [0] * 10005
for i in range(n - 1):
a, b = map(int, input().split())
if b < n:
print("NO")
exit()
lis.append(a)
fre[a] += 1
lis.sort()
for i in range(n - 1):
if lis[i] <= i:
print("NO")
exit()
else:
zer = 1
for i in range(1, n):
if fre[i] == 1:
ans.append([i, n])
elif fre[i] > 1:
co = 0
while co != fre[i] - 1:
if fre[zer] == 0 and co == 0:
co += 1
ans.append([zer, n])
elif fre[zer] == 0:
co += 1
ans.append([zer, ans[-1][0]])
zer += 1
ans.append([i, ans[-1][0]])
print("YES")
for i in ans[1:]:
print(*i) | ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Monocarp has drawn a tree (an undirected connected acyclic graph) and then has given each vertex an index. All indices are distinct numbers from $1$ to $n$. For every edge $e$ of this tree, Monocarp has written two numbers: the maximum indices of the vertices of the two components formed if the edge $e$ (and only this edge) is erased from the tree.
Monocarp has given you a list of $n - 1$ pairs of numbers. He wants you to provide an example of a tree that will produce the said list if this tree exists. If such tree does not exist, say so.
-----Input-----
The first line contains one integer $n$ ($2 \le n \le 1\,000$)Β β the number of vertices in the tree.
Each of the next $n-1$ lines contains two integers $a_i$ and $b_i$ each ($1 \le a_i < b_i \le n$)Β β the maximal indices of vertices in the components formed if the $i$-th edge is removed.
-----Output-----
If there is no such tree that can produce the given list of pairs, print "NO" (without quotes).
Otherwise print "YES" (without quotes) in the first line and the edges of the tree in the next $n - 1$ lines. Each of the last $n - 1$ lines should contain two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$)Β β vertices connected by an edge.
Note: The numeration of edges doesn't matter for this task. Your solution will be considered correct if your tree produces the same pairs as given in the input file (possibly reordered). That means that you can print the edges of the tree you reconstructed in any order.
-----Examples-----
Input
4
3 4
1 4
3 4
Output
YES
1 3
3 2
2 4
Input
3
1 3
1 3
Output
NO
Input
3
1 2
2 3
Output
NO
-----Note-----
Possible tree from the first example. Dotted lines show edges you need to remove to get appropriate pairs. [Image] | import sys
n = int(input())
mentioned = [0] * (n - 1)
for _ in range(n - 1):
a, b = map(int, input().split())
if b != n:
print("NO")
sys.exit()
else:
mentioned[a - 1] += 1
if mentioned[a - 1] > a:
print("NO")
sys.exit()
if mentioned[-1] == 0:
print("NO")
sys.exit()
cnt = 0
for i in range(n - 2, -1, -1):
cnt += mentioned[i] - 1
if cnt < 0:
print("NO")
sys.exit()
print("YES")
end = [i for i in range(n - 1) if mentioned[i] > 0]
mid = [i for i in range(n - 1) if mentioned[i] == 0]
branch_sum = [mentioned[end[0]] - 1]
for i in range(1, len(end)):
branch_sum.append(branch_sum[-1] + mentioned[end[i]] - 1)
branch = [[] for _ in range(len(end))]
branch[0] = mid[: branch_sum[0]]
for i in range(1, len(end)):
branch[i] = mid[branch_sum[i - 1] : branch_sum[i]]
for i in range(len(end)):
ans = [end[i] + 1]
ans += [(j + 1) for j in branch[i]]
ans.append(n)
for j in range(len(ans) - 1):
print(ans[j], ans[j + 1]) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER |
Monocarp has drawn a tree (an undirected connected acyclic graph) and then has given each vertex an index. All indices are distinct numbers from $1$ to $n$. For every edge $e$ of this tree, Monocarp has written two numbers: the maximum indices of the vertices of the two components formed if the edge $e$ (and only this edge) is erased from the tree.
Monocarp has given you a list of $n - 1$ pairs of numbers. He wants you to provide an example of a tree that will produce the said list if this tree exists. If such tree does not exist, say so.
-----Input-----
The first line contains one integer $n$ ($2 \le n \le 1\,000$)Β β the number of vertices in the tree.
Each of the next $n-1$ lines contains two integers $a_i$ and $b_i$ each ($1 \le a_i < b_i \le n$)Β β the maximal indices of vertices in the components formed if the $i$-th edge is removed.
-----Output-----
If there is no such tree that can produce the given list of pairs, print "NO" (without quotes).
Otherwise print "YES" (without quotes) in the first line and the edges of the tree in the next $n - 1$ lines. Each of the last $n - 1$ lines should contain two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$)Β β vertices connected by an edge.
Note: The numeration of edges doesn't matter for this task. Your solution will be considered correct if your tree produces the same pairs as given in the input file (possibly reordered). That means that you can print the edges of the tree you reconstructed in any order.
-----Examples-----
Input
4
3 4
1 4
3 4
Output
YES
1 3
3 2
2 4
Input
3
1 3
1 3
Output
NO
Input
3
1 2
2 3
Output
NO
-----Note-----
Possible tree from the first example. Dotted lines show edges you need to remove to get appropriate pairs. [Image] | n = int(input())
indices = []
for i in range(n - 1):
x, y = map(int, input().split())
indices.append((x, y))
try:
not_seen = set(list(range(1, n)))
seen = {}
for x, y in indices:
assert x == n or y == n
seen.setdefault(min(x, y), 0)
seen[min(x, y)] += 1
if min(x, y) in not_seen:
not_seen.remove(min(x, y))
not_seen = sorted(list(not_seen), reverse=True)
seen_list = list(seen.keys())
seen_list.sort(reverse=True)
edges = []
while seen_list:
cur_elem = seen_list.pop(0)
prev = n
while seen[cur_elem] != 1:
assert not_seen[0] < cur_elem
next_elem = not_seen.pop(0)
edges.append((prev, next_elem))
prev = next_elem
seen[cur_elem] -= 1
edges.append((prev, cur_elem))
print("YES")
for x, y in edges:
print(x, y)
except AssertionError:
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR DICT FOR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING |
Given an nΒ x nΒ binary grid, in one step you can choose two adjacent rows of the grid and swap them.
A grid is said to be valid if all the cells above the main diagonal are zeros.
Return the minimum number of steps needed to make the grid valid, or -1 if the grid cannot be valid.
The main diagonal of a grid is the diagonal that starts at cell (1, 1) and ends at cell (n, n).
Β
Example 1:
Input: grid = [[0,0,1],[1,1,0],[1,0,0]]
Output: 3
Example 2:
Input: grid = [[0,1,1,0],[0,1,1,0],[0,1,1,0],[0,1,1,0]]
Output: -1
Explanation: All rows are similar, swaps have no effect on the grid.
Example 3:
Input: grid = [[1,0,0],[1,1,0],[1,1,1]]
Output: 0
Β
Constraints:
n == grid.length
n == grid[i].length
1 <= nΒ <= 200
grid[i][j] is 0 or 1 | class Solution:
def minSwaps(self, grid: List[List[int]]) -> int:
for i in range(len(grid)):
j = len(grid[i]) - 1
while j >= 0 and grid[i][j] == 0:
j -= 1
grid[i] = j
count = 0
for i in range(len(grid)):
if grid[i] <= i:
continue
j = i + 1
while j < len(grid) and grid[j] > i:
j += 1
if j == len(grid):
return -1
while j > i:
grid[j], grid[j - 1] = grid[j - 1], grid[j]
count += 1
j -= 1
return count | CLASS_DEF FUNC_DEF VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR |
Given an nΒ x nΒ binary grid, in one step you can choose two adjacent rows of the grid and swap them.
A grid is said to be valid if all the cells above the main diagonal are zeros.
Return the minimum number of steps needed to make the grid valid, or -1 if the grid cannot be valid.
The main diagonal of a grid is the diagonal that starts at cell (1, 1) and ends at cell (n, n).
Β
Example 1:
Input: grid = [[0,0,1],[1,1,0],[1,0,0]]
Output: 3
Example 2:
Input: grid = [[0,1,1,0],[0,1,1,0],[0,1,1,0],[0,1,1,0]]
Output: -1
Explanation: All rows are similar, swaps have no effect on the grid.
Example 3:
Input: grid = [[1,0,0],[1,1,0],[1,1,1]]
Output: 0
Β
Constraints:
n == grid.length
n == grid[i].length
1 <= nΒ <= 200
grid[i][j] is 0 or 1 | class Solution:
def minSwaps(self, grid: List[List[int]]) -> int:
def helper(row):
for i in range(len(row) - 1, -1, -1):
if row[i] == 1:
return len(row) - i - 1
return len(row)
arr = list(map(helper, grid))
count = 0
target = len(grid[0]) - 1
for i in range(len(arr)):
if arr[i] < target:
found = False
for j in range(i + 1, len(arr)):
if arr[j] >= target:
count += j - i
arr.insert(i, arr.pop(j))
found = True
break
if not found:
return -1
target -= 1
return count | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR RETURN NUMBER VAR NUMBER RETURN VAR VAR |
Given an nΒ x nΒ binary grid, in one step you can choose two adjacent rows of the grid and swap them.
A grid is said to be valid if all the cells above the main diagonal are zeros.
Return the minimum number of steps needed to make the grid valid, or -1 if the grid cannot be valid.
The main diagonal of a grid is the diagonal that starts at cell (1, 1) and ends at cell (n, n).
Β
Example 1:
Input: grid = [[0,0,1],[1,1,0],[1,0,0]]
Output: 3
Example 2:
Input: grid = [[0,1,1,0],[0,1,1,0],[0,1,1,0],[0,1,1,0]]
Output: -1
Explanation: All rows are similar, swaps have no effect on the grid.
Example 3:
Input: grid = [[1,0,0],[1,1,0],[1,1,1]]
Output: 0
Β
Constraints:
n == grid.length
n == grid[i].length
1 <= nΒ <= 200
grid[i][j] is 0 or 1 | class Solution:
def minSwaps(self, grid: List[List[int]]) -> int:
start = 1
swap = 0
n = len(grid)
zeros_ingrid = n - 1
while zeros_ingrid > 0:
swapped_grid = False
for i in range(len(grid)):
if sum(grid[i][start:]) == 0:
swap += i
grid.remove(grid[i])
swapped_grid = True
zeros_ingrid -= 1
start += 1
break
if not swapped_grid:
return -1
return swap | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR RETURN NUMBER RETURN VAR VAR |
Given an nΒ x nΒ binary grid, in one step you can choose two adjacent rows of the grid and swap them.
A grid is said to be valid if all the cells above the main diagonal are zeros.
Return the minimum number of steps needed to make the grid valid, or -1 if the grid cannot be valid.
The main diagonal of a grid is the diagonal that starts at cell (1, 1) and ends at cell (n, n).
Β
Example 1:
Input: grid = [[0,0,1],[1,1,0],[1,0,0]]
Output: 3
Example 2:
Input: grid = [[0,1,1,0],[0,1,1,0],[0,1,1,0],[0,1,1,0]]
Output: -1
Explanation: All rows are similar, swaps have no effect on the grid.
Example 3:
Input: grid = [[1,0,0],[1,1,0],[1,1,1]]
Output: 0
Β
Constraints:
n == grid.length
n == grid[i].length
1 <= nΒ <= 200
grid[i][j] is 0 or 1 | class Solution:
dbg = False
def minSwaps(self, grid) -> int:
grid_ints = []
for row in grid:
enc = 0
for val in reversed(row):
enc = enc << 1
if val % 2 == 1:
enc += 1
grid_ints.append(enc)
if self.dbg:
print(grid_ints)
bar = 1
swaps = 0
for i in range(len(grid[0])):
if grid_ints[i] > bar:
j = i
while grid_ints[j] > bar:
j += 1
if j >= len(grid[0]):
return -1
while j > i:
grid_ints[j], grid_ints[j - 1] = grid_ints[j - 1], grid_ints[j]
swaps += 1
j -= 1
bar = (bar << 1) + 1
return swaps | CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN VAR VAR |
Given an nΒ x nΒ binary grid, in one step you can choose two adjacent rows of the grid and swap them.
A grid is said to be valid if all the cells above the main diagonal are zeros.
Return the minimum number of steps needed to make the grid valid, or -1 if the grid cannot be valid.
The main diagonal of a grid is the diagonal that starts at cell (1, 1) and ends at cell (n, n).
Β
Example 1:
Input: grid = [[0,0,1],[1,1,0],[1,0,0]]
Output: 3
Example 2:
Input: grid = [[0,1,1,0],[0,1,1,0],[0,1,1,0],[0,1,1,0]]
Output: -1
Explanation: All rows are similar, swaps have no effect on the grid.
Example 3:
Input: grid = [[1,0,0],[1,1,0],[1,1,1]]
Output: 0
Β
Constraints:
n == grid.length
n == grid[i].length
1 <= nΒ <= 200
grid[i][j] is 0 or 1 | class Solution:
def minSwaps(self, grid: List[List[int]]) -> int:
n = len(grid[0])
def countzeros(row):
zeros = 0
while row:
new = row.pop()
if new == 0:
zeros += 1
else:
return zeros
return zeros
zeros = [countzeros(i) for i in grid]
print(zeros)
row = 0
ops = 0
while len(zeros):
found = False
for i in range(len(zeros)):
if zeros[i] >= n - 1 - row:
ops += i
zeros.remove(zeros[i])
row += 1
found = True
break
if not found:
return -1
return ops | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER RETURN VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR RETURN NUMBER RETURN VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | for _ in range(int(input())):
n, k, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a = [(a[i] - b[i]) for i in range(n)]
w = list(map(int, input().split()))
bl = list(map(int, input().split()))
c = w + bl
u = 0
c.sort(reverse=True)
a.sort(reverse=True)
i = 0
for j in c:
if j <= a[i]:
u += j
i += 1
print(sum(a) - u) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | t = int(input())
for _ in range(t):
n, k, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
d = list(map(int, input().split()))
temp = sorted(c + d, reverse=True)
e = []
for i in range(n):
e.append(a[i] - b[i])
e.sort(reverse=True)
ans = 0
j = 0
for i in e:
flag = False
while j < len(temp):
if temp[j] <= i:
ans += i - temp[j]
j += 1
flag = True
break
j += 1
if not flag:
ans += i
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | test = int(input())
for _ in range(test):
n, k, m = map(int, input().split())
task_arr = [int(i) for i in input().split()]
done_arr = [int(i) for i in input().split()]
w_arr = [int(i) for i in input().split()]
b_arr = [int(i) for i in input().split()]
diff_arr = [(task_arr[i] - done_arr[i]) for i in range(n)]
diff_arr = sorted(diff_arr, reverse=True)
w_arr = sorted(w_arr, reverse=True)
b_arr = sorted(b_arr, reverse=True)
w, b = 0, 0
for i in range(n):
while w < k and w_arr[w] > diff_arr[i]:
w += 1
while b < m and b_arr[b] > diff_arr[i]:
b += 1
if w == k and b == m:
break
if w == k:
diff_arr[i] -= b_arr[b]
b += 1
elif b == m:
diff_arr[i] -= w_arr[w]
w += 1
elif w_arr[w] > b_arr[b]:
diff_arr[i] -= w_arr[w]
w += 1
else:
diff_arr[i] -= b_arr[b]
b += 1
minsum = 0
for i in range(n):
minsum += diff_arr[i]
print(minsum) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | for x in range(int(input())):
n, k, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
wt = list(map(int, input().split()))
bk = list(map(int, input().split()))
c = [(a[i] - b[i]) for i in range(n)]
c.sort()
wt.sort()
bk.sort()
ans = 0
while c:
top = c[-1]
c.pop()
while wt:
if wt and wt[-1] > top:
wt.pop()
else:
break
while bk:
if bk and bk[-1] > top:
bk.pop()
else:
break
if not wt and not bk:
ans += top
elif not wt:
ans += top - bk[-1]
bk.pop()
elif not bk:
ans += top - wt[-1]
wt.pop()
elif wt[-1] > bk[-1]:
ans += top - wt[-1]
wt.pop()
else:
ans += top - bk[-1]
bk.pop()
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR WHILE VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR IF VAR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | t = int(input())
while t:
n, k, m = map(int, input().split())
arr = list(map(int, input().split()))
brr = list(map(int, input().split()))
diff = []
for a, b in zip(arr, brr):
diff.append(abs(a - b))
wbrr = list(map(int, input().split()))
bbrr = list(map(int, input().split()))
btn = wbrr + bbrr
btn.sort(reverse=True)
diff.sort(reverse=True)
i = 0
top = 0
while top < k + m and i < n:
if btn[top] <= diff[i]:
diff[i] -= btn[top]
i += 1
top += 1
print(sum(diff))
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | t = int(input())
for _ in range(t):
n, k, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
d = list(map(int, input().split()))
c.extend(d)
diff = [(0) for i in range(n)]
for i in range(n):
diff[i] = a[i] - b[i]
diff.sort(reverse=True)
c.sort(reverse=True)
bid = 0
for j in range(n):
while c[bid] > diff[j]:
bid += 1
if bid >= k + m:
break
if bid >= k + m:
break
else:
diff[j] -= c[bid]
bid += 1
if bid >= k + m:
break
print(sum(diff)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | T = int(input())
for i in range(T):
NKM = str(input()).split(" ")
N = int(NKM[0])
K = int(NKM[1])
M = int(NKM[2])
A = str(input()).split(" ")
B = str(input()).split(" ")
C = str(input()).split(" ")
D = str(input()).split(" ")
mix = []
for j in range(N):
mix.append((int(A[j]), int(B[j])))
for j in range(K):
C[j] = int(C[j])
for j in range(M):
D[j] = int(D[j])
mix.sort(key=lambda x: x[0] - x[1], reverse=True)
C.sort(reverse=True)
D.sort(reverse=True)
left = 0
ccur = 0
dcur = 0
for j in range(N):
if mix[j][0] - mix[j][1]:
flag = 1
while flag and (ccur < K or dcur < M):
if ccur < K:
if dcur < M:
if C[ccur] > D[dcur]:
if C[ccur] <= mix[j][0] - mix[j][1]:
left = left + (mix[j][0] - mix[j][1]) - C[ccur]
ccur = ccur + 1
flag = 0
else:
ccur = ccur + 1
elif D[dcur] <= mix[j][0] - mix[j][1]:
left = left + (mix[j][0] - mix[j][1]) - D[dcur]
dcur = dcur + 1
flag = 0
else:
dcur = dcur + 1
elif C[ccur] <= mix[j][0] - mix[j][1]:
left = left + (mix[j][0] - mix[j][1]) - C[ccur]
ccur = ccur + 1
flag = 0
else:
ccur = ccur + 1
elif dcur < M:
if D[dcur] <= mix[j][0] - mix[j][1]:
left = left + (mix[j][0] - mix[j][1]) - D[dcur]
dcur = dcur + 1
flag = 0
else:
dcur = dcur + 1
if flag:
left = left + (mix[j][0] - mix[j][1])
print(left) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR IF VAR VAR IF VAR VAR IF VAR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | for _ in range(int(input())):
n, k, m = map(int, input().split())
arr1 = list(map(int, input().split()))
arr2 = list(map(int, input().split()))
for i in range(n):
arr1[i] = arr1[i] - arr2[i]
arr2 = list(map(int, (input() + " " + input()).split()))
arr1.sort()
arr2.sort()
i = n - 1
inc = 0
for j in range(k + m - 1, -1, -1):
if arr1[i] >= arr2[j]:
inc += arr1[i] - arr2[j]
i -= 1
while i >= 0:
inc += arr1[i]
i -= 1
print(inc) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL BIN_OP BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | t = int(input())
for i in range(t):
n, k, m = list(map(int, input().split()))
task_count = list(map(int, input().split()))
completed_task_count = list(map(int, input().split()))
white_values = list(map(int, input().split()))
black_values = list(map(int, input().split()))
diff_task = [(a_i - b_i) for a_i, b_i in zip(task_count, completed_task_count)]
diff_task = sorted(diff_task, reverse=True)
buttons = sorted(white_values + black_values, reverse=True)
x = 0
for idx, element in enumerate(diff_task):
if element == 0:
continue
else:
while buttons[x] > element:
x += 1
diff_task[idx] -= buttons[x]
x += 1
if x >= len(buttons):
break
print(sum(diff_task)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | for _ in range(int(input())):
n, k, m = map(int, input().split())
B = list(map(int, input().split()))
D = list(map(int, input().split()))
L = list(map(int, input().split()))
N = list(map(int, input().split()))
L += N
L.sort(reverse=True)
D = [(a - b) for a, b in zip(B, D)]
D.sort(reverse=True)
incr = sum(D)
j = 0
for i in D:
if i == 0:
continue
else:
while L[j] > i:
j += 1
incr -= L[j]
j += 1
if j > k + m - 1:
break
print(incr) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | t = int(input())
for i in range(t):
n, k, m = list(map(int, input().split()))
plan = list(map(int, input().split()))
comp = list(map(int, input().split()))
white = list(map(int, input().split()))
black = list(map(int, input().split()))
rem = []
p = 0
machine = []
count_rem = 0
count_machine = 0
for j in range(n):
rem.append(plan[j] - comp[j])
count_rem += sum(rem)
rem.sort(reverse=True)
machine.extend(white)
machine.extend(black)
machine.sort(reverse=True)
count_machine += sum(machine)
if machine[0] <= rem[-1]:
count_rem -= count_machine
elif machine[-1] > rem[0]:
pass
else:
for j, val in enumerate(machine):
while rem != []:
if val <= rem[p]:
count_rem -= val
rem.pop(p)
break
else:
break
print(count_rem) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR WHILE VAR LIST IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | for _ in range(int(input())):
N, K, M = map(int, input().split())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
buttons = [int(x) for x in input().split()]
buttons += [int(x) for x in input().split()]
diff = [(A[i] - B[i]) for i in range(N)]
diff.sort(reverse=True)
buttons.sort(reverse=True)
accumulated = sum(diff)
i = 0
for ele in diff:
while i < K + M:
if ele >= buttons[i]:
accumulated -= buttons[i]
i += 1
break
else:
i += 1
print(accumulated) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | for _ in range(int(input())):
n, k, m = map(int, input().split())
lc = list(map(int, input().split()))
cu = list(map(int, input().split()))
w = list(map(int, input().split()))
b = list(map(int, input().split()))
for i in range(n):
lc[i] -= cu[i]
buttons = w + b
lc.sort()
buttons.sort()
j = len(buttons) - 1
for i in range(n - 1, -1, -1):
if j >= 0 and i >= 0:
while buttons[j] > lc[i]:
j -= 1
if j < 0:
break
if buttons[j] <= lc[i]:
lc[i] -= buttons[j]
j -= 1
total = 0
for i in range(n):
total += lc[i]
print(total) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | t = int(input())
while t:
N, K, M = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
k = list(map(int, input().split()))
m = list(map(int, input().split()))
d = []
for i in range(N):
d.append(A[i] - B[i])
d.sort(reverse=True)
k.extend(m)
k.sort(reverse=True)
j = 0
sum = 0
for i in range(len(k)):
if j < len(d):
if k[i] <= d[j]:
sum += d[j] - k[i]
i += 1
j += 1
else:
i += 1
else:
break
while j < len(d):
sum += d[j]
j += 1
print(sum)
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | t = int(input())
for i in range(t):
n, k, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
d = list(map(int, input().split()))
z = sorted(c + d, reverse=True)
s = [0] * n
for j in range(n):
s[j] = a[j] - b[j]
x = 0
w = len(z)
s = sorted(s, reverse=True)
for j, v in enumerate(s):
if v == 0:
continue
else:
while z[x] > v:
x += 1
s[j] -= z[x]
x += 1
if x >= w:
break
print(sum(s)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | t = int(input())
for _ in range(t):
n = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
d = list(map(int, input().split()))
for i in range(len(a)):
a[i] = abs(a[i] - b[i])
c = c + d
a.sort()
c.sort()
a = a[::-1]
c = c[::-1]
p1 = 0
p2 = 0
counter = 0
for i in range(n[1] + n[2]):
if p1 < n[0]:
if a[p1] >= c[p2]:
counter += a[p1] - c[p2]
p1 += 1
p2 += 1
else:
p2 += 1
if p1 < n[0]:
for j in range(p1, n[0]):
counter += a[j]
print(counter) | 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | for _ in range(int(input())):
N, K, M = map(int, input().split())
A, B, C, D = (
list(map(int, input().split())),
list(map(int, input().split())),
list(map(int, input().split())),
list(map(int, input().split())),
)
it, W, S, cnt = (
0,
sorted([(x - y) for x, y in zip(A, B)], reverse=True),
sorted(C + D, reverse=True),
0,
)
for j, i in enumerate(W):
while it < len(S) and S[it] > i:
it += 1
if it >= len(S):
break
cnt += i - S[it]
it += 1
print(cnt + sum(W[j:])) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | T = int(input())
for z in range(T):
N, K, M = map(int, input().split())
lst1 = list(map(int, input().split()))
lst2 = list(map(int, input().split()))
lst = [(lst1[i] - lst2[i]) for i in range(N) if lst1[i] - lst2[i] > 0]
lst.sort(reverse=True)
button = list(map(int, input().split()))
button += list(map(int, input().split()))
button.sort(reverse=True)
res = sum(lst)
i = 0
for elem in lst:
while i < K + M:
if elem >= button[i]:
res -= button[i]
i += 1
break
else:
i += 1
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | T = int(input())
output = []
for _ in range(T):
N, K, M = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = [(x, "white") for x in list(map(int, input().split()))]
D = [(x, "black") for x in list(map(int, input().split()))]
buttons = C + D
buttons.sort(key=lambda x: x[0])
diffs = [(a - b) for a, b in zip(A, B)]
diffs.sort()
uncompleted = 0
while len(diffs) > 0:
d = diffs.pop()
while len(buttons) > 0 and d < buttons[-1][0]:
buttons.pop()
if len(buttons) > 0:
b = buttons.pop()
d -= b[0]
uncompleted += d
output.append(uncompleted)
for o in output:
print(o) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | for _ in range(int(input())):
n, k, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
d = list(map(int, input().split()))
a = [(a[i] - b[i]) for i in range(n)]
e = c + d
e.sort(reverse=True)
a.sort(reverse=True)
x, y = 0, 0
while y < len(e):
if e[y] <= a[x]:
a[x] -= e[y]
x += 1
e.pop(y)
else:
y += 1
print(sum(a)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | for _ in range(int(input())):
n, w, b = map(int, input().strip().split())
A = list(map(int, input().strip().split()))
B = [(x - y) for x, y in zip(A, list(map(int, input().strip().split())))]
C = list(map(int, input().strip().split()))
C.extend(list(map(int, input().strip().split())))
B.sort(reverse=True)
C.sort(reverse=True)
cnt = 0
ans = 0
total = len(C)
for day in B:
while cnt < total and C[cnt] > day:
cnt += 1
if cnt < total:
ans += day - C[cnt]
cnt += 1
else:
ans += day
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | def call(A, B, buy, n):
diff = []
for i in range(n):
diff.append(A[i] - B[i])
diff.sort(reverse=True)
buy.sort(reverse=True)
i, j = 0, 0
while i < n and j < len(buy):
if diff[i] >= buy[j]:
diff[i] -= buy[j]
i += 1
j += 1
else:
j += 1
return sum(diff)
t = int(input())
while t > 0:
n, m, k = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))
D = list(map(int, input().split()))
print(call(A, B, C + D, n))
t -= 1 | FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | for _ in range(int(input())):
n, k, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split())) + list(map(int, input().split()))
c.sort(reverse=True)
diff = [(a[i] - b[i]) for i in range(n) if a[i] - b[i] > 0]
diff.sort(reverse=True)
t = 0
l = len(c)
ans = sum(diff)
for i in diff:
for j in range(t, l):
if c[j] <= i:
ans -= c[j]
t = j + 1
break
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | for q in range(int(input())):
n, wn, bn = [int(e) for e in input().split()]
a1 = [int(e) for e in input().split()]
a2 = [int(e) for e in input().split()]
a = []
for i in range(n):
a.append(abs(a1[i] - a2[i]))
a.sort(reverse=True)
w = [int(e) for e in input().split()]
b = [int(e) for e in input().split()]
w.extend(b)
w.sort(reverse=True)
j = 0
i = 0
l = len(w)
while i < n:
if j >= l:
break
elif a[i] >= w[j]:
a[i] -= w[j]
i += 1
j += 1
print(sum(a)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR 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 FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | t = int(input())
for x in range(0, t):
n, k, m = map(int, input().split())
a = [int(j) for j in input().split()]
b = [int(w) for w in input().split()]
c = [int(e) for e in input().split()]
d = [int(r) for r in input().split()]
lis = c + d
li = []
for i in range(0, n):
li.append(a[i] - b[i])
li.sort(reverse=True)
lis.sort(reverse=True)
q = 0
sum = 0
z = 0
y = 0
un = 0
while z < m + k and y < n:
if li[y] >= lis[z]:
li[y] -= lis[z]
y += 1
z += 1
else:
z += 1
r = 0
while r < n:
un += li[r]
r += 1
print(un) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | t = int(input())
while t:
n_k_m = [int(i) for i in input().split()]
n = n_k_m[0]
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
k = [int(i) for i in input().split()]
m = [int(i) for i in input().split()]
inactive_dict = {}
res = 0
diff = [abs(a[i] - b[i]) for i in range(n)]
rev_diff = sorted(diff, reverse=True)
rev = sorted(k + m, reverse=True)
aa = sum(diff)
i = 0
j = 0
while i < len(rev) and j < len(rev_diff):
if rev[i] <= rev_diff[j]:
res += rev[i]
i += 1
j += 1
else:
i += 1
print(aa - res)
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | tc = int(input())
for i in range(tc):
n, k, m = list(map(int, input().split()))
pt = list(map(int, input().split()))
ct = list(map(int, input().split()))
wb = list(map(int, input().split()))
bb = list(map(int, input().split()))
rm = [(x1 - x2) for x1, x2 in zip(pt, ct)]
rm.sort(reverse=True)
full_list = wb[:]
full_list.extend(bb)
full_list.sort(reverse=True)
k1 = 0
rem = 0
ind_done = []
while k1 < len(rm):
if len(ind_done) == 0:
j = 0
else:
j = ind_done[-1]
while j < len(full_list) and full_list[j] > rm[k1]:
j += 1
if j == len(full_list):
rem += rm[k1]
rm.pop(k1)
else:
rem += rm[k1] - full_list[j]
rm.pop(k1)
full_list.pop(j)
ind_done.append(j)
print(rem) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | test_cases = int(input())
for _ in range(test_cases):
numdays, white_buttons, black_buttons = map(int, input().split())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
K = [int(x) for x in input().split()]
M = [int(x) for x in input().split()]
C = [(A[i] - B[i]) for i in range(numdays)]
C.sort(reverse=True)
buttons = sorted(K + M, reverse=True)
i = 0
j = 0
while i < len(C) and j < len(buttons):
if buttons[j] <= C[i]:
C[i] -= buttons[j]
j += 1
i += 1
else:
j += 1
print(sum(C)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | for _ in range(int(input())):
n, k, m = map(int, input().split())
arr1 = list(map(int, input().split()))
arr2 = list(map(int, input().split()))
val = sorted([(arr1[i] - arr2[i]) for i in range(n)])[::-1]
w = list(map(int, input().split()))
b = list(map(int, input().split()))
f = sorted(w + b)[::-1]
i = j = 0
while j < k + m and i < n:
if val[i] >= f[j]:
val[i] -= f[j]
j += 1
i += 1
else:
j += 1
print(sum(val)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | for _ in range(int(input())):
n, k, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))
d = list(map(int, input().split()))
tmp = sorted(c + d)
res = []
s = 0
for i in range(n):
res.append(a[i] - b[i])
res.sort()
ib, il = len(tmp) - 1, len(res) - 1
while il >= 0 and ib >= 0:
while ib >= 0 and tmp[ib] > res[il]:
ib -= 1
res[il] -= tmp[ib]
s += res[il]
il -= 1
ib -= 1
while il >= 0:
s += res[il]
il -= 1
print(s) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | t = int(input())
def get_params(inp):
inp = inp.split()
inp = [int(i) for i in inp]
return inp
for k in range(t):
params = str(input())
n, k, m = get_params(params)
planned = str(input())
planned = get_params(planned)
done = str(input())
done = get_params(done)
white = get_params(str(input()))
black = get_params(str(input()))
total = white + black
total = sorted(total)[::-1]
remain = [(planned[i] - done[i]) for i in range(len(planned))]
remain = sorted(remain)[::-1]
p1, p2 = 0, 0
ans = sum(remain)
while True:
r = remain[p1]
s = total[p2]
if s > r:
p2 = p2 + 1
else:
ans = ans - s
p1 = p1 + 1
p2 = p2 + 1
if p2 == len(total) or p1 == len(remain):
break
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | def minTasks(diff, buttons):
i, j = 0, 0
incompleteTasks = 0
while i < len(diff) and j < len(buttons):
if diff[i] >= buttons[j]:
incompleteTasks += diff[i] - buttons[j]
i += 1
j += 1
else:
j += 1
while i < len(diff):
incompleteTasks += diff[i]
i += 1
return incompleteTasks
t = int(input())
while t > 0:
n, k, m = map(int, input().strip().split())
planned = list(map(int, input().strip().split()))
completed = list(map(int, input().strip().split()))
white = list(map(int, input().strip().split()))
black = list(map(int, input().strip().split()))
buttons = white + black
buttons.sort(reverse=True)
diff = []
for i in range(n):
diff.append(planned[i] - completed[i])
diff.sort(reverse=True)
print(minTasks(diff, buttons))
t -= 1 | FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | def ti():
return tuple(map(int, input().split()))
def li():
return list(map(int, input().split()))
def si():
return input().split()
def ii():
return int(input())
def ip():
return input()
for tastcas in range(int(input())):
n, k, m = li()
r = k + m
a = li()
d = li()
w = li()
w += li()
for i in range(n):
a[i] -= d[i]
a.sort()
w.sort()
i = n - 1
j = r - 1
while i >= 0 and j >= 0:
while j >= 0 and w[j] > a[i]:
j -= 1
if i >= 0:
a[i] -= w[j]
i -= 1
j -= 1
print(sum(a)) | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | for _ in range(int(input())):
n, w, b = map(int, input().split())
arr = list(map(int, input().split()))
arr1 = list(map(int, input().split()))
new = []
for i in range(len(arr)):
new.append(abs(arr[i] - arr1[i]))
res = []
white = list(map(int, input().split()))
black = list(map(int, input().split()))
new.sort(reverse=True)
for i in white:
res.append(i)
for i in black:
res.append(i)
res.sort(reverse=True)
i = 0
j = 0
un = 0
while j < w + b and i < n:
if new[i] >= res[j]:
new[i] -= res[j]
i += 1
j += 1
else:
j += 1
i = 0
while i < n:
un += new[i]
i += 1
print(un) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Chef spent N days working really hard! He planned loads of tasks: as many as A_{i} tasks to do on the i_{th} day! Chef's work was brutal, so he only managed to finish B_{i} tasks on the i_{th} day.
The good news is that Chef has a Time Machine!
The Time Machine has K white buttons and M black buttons. Each button has a positive integer printed on it. Now Chef goes through all N days consequently and presses buttons. Each day Chef can only press one button (either white or black). After using a button once, it becomes inactive.
Pressing a white button with integer x printed on it reduces the number of planned tasks on the day it was pressed by exactly x. Note that this white button can only be pressed if number of planned tasks on the day are greater than or equal to x.
Pressing a black button with integer x printed on it increases the number of completed tasks on the day it was pressed by exactly x. Note that this black button can only be pressed if after pressing it, number of completed tasks don't exceed the number of tasks.
Chef is interested in finding the minimum possible amount of total uncompleted tasks he will still be left with after N days using the Machine in the best way?
Be careful! Time is sensitive! Chef cannot make a day when he completed more tasks then planned, as this may result in a more-work-than-planned paradox, killing all lazy people on the planet!
------ Input ------
The first line of input contains a single integer T, denoting the number of test cases. Description of T test cases follows.
The first line of each test case contains three integers β N, K, M β denoting the number of days, white and black buttons appropriately.
The second line contains N space-separated integers A_{1}, A_{2}, β¦ , A_{N}, denoting the number of planned tasks.
The third line contains N space-separated integers B_{1}, B_{2}, β¦ , B_{N}, denoting the number of completed tasks.
The fourth line contains K space-separated integers C_{1}, C_{2}, β¦ , C_{K}, denoting the integers on white buttons.
The fifth and last line contains M space-separated integers D_{1}, D_{2}, β¦ , D_{M}, denoting the integers on black buttons.
------ Output ------
In a single line, output an integer β the minimum possible amount of uncompleted tasks.
------ Constraints ------
$1 β€ T β€ 4$
$1 β€ N, K, M β€ 10^{5}$
$1 β€ B_{i} β€ A_{i} β€ 10^{5}$
$1 β€ C_{i}, D_{i} β€ 10^{5}$
------ Subtasks ------
$Subtask N β€ 10, K, M β€ 5. Points: 30 $
$Subtask Original constraints. Points: 70 $
----- Sample Input 1 ------
1
4 2 2
5 7 6 1
3 3 1 1
6 3
1 4
----- Sample Output 1 ------
3
----- explanation 1 ------
Example case 1.
In this example Chef goes through the following steps:
Use black button 1 on the first day.
Use black button 4 on the second day.
Use white button 3 on the third day.
The arrays A and B are now effectively changed to:
5 7 3 1
4 7 1 1
So he will have 3 uncompleted tasks. | for z in range(int(input())):
n, k, m = map(int, input().split())
pt = list(map(int, input().split()))
ct = list(map(int, input().split()))
wb = list(map(int, input().split()))
bb = list(map(int, input().split()))
wb.extend(bb)
c = 0
wb.sort(reverse=True)
a = []
for i in range(n):
a.append(pt[i] - ct[i])
a.sort(reverse=True)
x = 0
for i in range(n):
if a[i] == 0:
continue
else:
for j in range(x, len(wb)):
if wb[j] <= a[i]:
a[i] = a[i] - wb[j]
wb[j] = 0
x = j + 1
break
print(sum(a)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines.
1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof.
2. Every house with only one incoming and no outgoing pipe gets a tap.
The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi.
Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible.
Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them.
Example 1:
Input:
n = 9, p = 6
a[] = {7,5,4,2,9,3}
b[] = {4,9,6,8,7,1}
d[] = {98,72,10,22,17,66}
Output:
3
2 8 22
3 1 66
5 6 10
Explanation:
Connected components are
3->1, 5->9->7->4->6 and 2->8.
Therefore, our answer is 3
followed by 2 8 22, 3 1 66, 5 6 10.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number).
Expected Time Complexity: O(n+p)
Expected Auxiliary Space: O(n+p)
Constraints:
1<=n<=20
1<=p<=50
1<=a[i],b[i]<=20
1<=d[i]<=100 | class Solution:
def solve(self, n, p, a, b, d):
adj = [() for i in range(n + 1)]
indeg = [0] * (n + 1)
for u, v, w in zip(a, b, d):
adj[u] = v, w
indeg[v] += 1
res = []
for u in range(1, n + 1):
if indeg[u] == 0 and adj[u]:
res.append([u, u, 0])
for i in range(len(res)):
u, v, w = res[i]
dest, wei = adj[u]
while adj[dest]:
dest1, wei1 = adj[dest]
dest, wei = dest1, min(wei1, wei)
res[i] = [u, dest, wei]
return res | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR RETURN VAR |
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines.
1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof.
2. Every house with only one incoming and no outgoing pipe gets a tap.
The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi.
Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible.
Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them.
Example 1:
Input:
n = 9, p = 6
a[] = {7,5,4,2,9,3}
b[] = {4,9,6,8,7,1}
d[] = {98,72,10,22,17,66}
Output:
3
2 8 22
3 1 66
5 6 10
Explanation:
Connected components are
3->1, 5->9->7->4->6 and 2->8.
Therefore, our answer is 3
followed by 2 8 22, 3 1 66, 5 6 10.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number).
Expected Time Complexity: O(n+p)
Expected Auxiliary Space: O(n+p)
Constraints:
1<=n<=20
1<=p<=50
1<=a[i],b[i]<=20
1<=d[i]<=100 | starting_vertices = None
ending_vertices = None
diameters = None
class Solution:
def dfs(self, u, ans):
if starting_vertices[u] == 0:
return u
if diameters[u] < ans[0]:
ans[0] = diameters[u]
return self.dfs(starting_vertices[u], ans)
def solve(self, n, p, a, b, d):
global starting_vertices, ending_vertices, diameters
starting_vertices = [0] * (n + 1)
diameters = [0] * (n + 1)
ending_vertices = [0] * (n + 1)
for i in range(p):
house1 = a[i]
diameter = d[i]
house2 = b[i]
starting_vertices[house1] = house2
diameters[house1] = d[i]
ending_vertices[house2] = house1
res = []
for u in range(1, n + 1):
if ending_vertices[u] == 0 and starting_vertices[u] != 0:
ans = [1 << 63]
v = self.dfs(u, ans)
res.append([u, v, ans[0]])
res.sort(key=lambda item: item[0])
return res | ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE CLASS_DEF FUNC_DEF IF VAR VAR NUMBER RETURN VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR LIST BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR |
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines.
1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof.
2. Every house with only one incoming and no outgoing pipe gets a tap.
The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi.
Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible.
Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them.
Example 1:
Input:
n = 9, p = 6
a[] = {7,5,4,2,9,3}
b[] = {4,9,6,8,7,1}
d[] = {98,72,10,22,17,66}
Output:
3
2 8 22
3 1 66
5 6 10
Explanation:
Connected components are
3->1, 5->9->7->4->6 and 2->8.
Therefore, our answer is 3
followed by 2 8 22, 3 1 66, 5 6 10.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number).
Expected Time Complexity: O(n+p)
Expected Auxiliary Space: O(n+p)
Constraints:
1<=n<=20
1<=p<=50
1<=a[i],b[i]<=20
1<=d[i]<=100 | class Solution:
def solve(self, n, p, a, b, d):
ans = [[a[0], b[0], d[0]]]
a.pop(0)
b.pop(0)
d.pop(0)
while a:
if ans[-1][1] in a:
index = a.index(ans[-1][1])
ans[-1][1] = b[index]
ans[-1][2] = min(ans[-1][2], d[index])
a.pop(index)
b.pop(index)
d.pop(index)
elif ans[-1][0] in b:
index = b.index(ans[-1][0])
ans[-1][0] = a[index]
ans[-1][2] = min(ans[-1][2], d[index])
a.pop(index)
b.pop(index)
d.pop(index)
elif a:
ans.append([a[0], b[0], d[0]])
a.pop(0)
b.pop(0)
d.pop(0)
for i in ans:
if i[0] == i[1]:
ans.remove(i)
ans.sort()
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST LIST VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR IF VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN VAR |
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines.
1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof.
2. Every house with only one incoming and no outgoing pipe gets a tap.
The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi.
Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible.
Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them.
Example 1:
Input:
n = 9, p = 6
a[] = {7,5,4,2,9,3}
b[] = {4,9,6,8,7,1}
d[] = {98,72,10,22,17,66}
Output:
3
2 8 22
3 1 66
5 6 10
Explanation:
Connected components are
3->1, 5->9->7->4->6 and 2->8.
Therefore, our answer is 3
followed by 2 8 22, 3 1 66, 5 6 10.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number).
Expected Time Complexity: O(n+p)
Expected Auxiliary Space: O(n+p)
Constraints:
1<=n<=20
1<=p<=50
1<=a[i],b[i]<=20
1<=d[i]<=100 | import sys
class Solution:
def dfs(self, graph, vis, src, end, di):
vis[src] = True
if src in graph.keys():
for x in graph[src]:
if not vis[x[0]]:
end[0] = x[0]
di[0] = min(di[0], x[1])
self.dfs(graph, vis, x[0], end, di)
def solve(self, n, p, a, b, d):
graph = {}
In = [0] * (n + 1)
Out = [0] * (n + 1)
Visited = [False] * (n + 1)
ans = []
for i in range(p):
Out[a[i]] = 1
In[b[i]] = 1
graph[a[i]] = [(b[i], d[i])]
for i in range(1, n + 1):
if In[i] == 0 and Out[i] == 1 and not Visited[i]:
start = i
end = [0]
minidi = [sys.maxsize]
self.dfs(graph, Visited, i, end, minidi)
ans.append([start, end[0], minidi[0]])
return ans | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER IF VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR LIST VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR NUMBER VAR NUMBER RETURN VAR |
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines.
1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof.
2. Every house with only one incoming and no outgoing pipe gets a tap.
The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi.
Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible.
Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them.
Example 1:
Input:
n = 9, p = 6
a[] = {7,5,4,2,9,3}
b[] = {4,9,6,8,7,1}
d[] = {98,72,10,22,17,66}
Output:
3
2 8 22
3 1 66
5 6 10
Explanation:
Connected components are
3->1, 5->9->7->4->6 and 2->8.
Therefore, our answer is 3
followed by 2 8 22, 3 1 66, 5 6 10.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number).
Expected Time Complexity: O(n+p)
Expected Auxiliary Space: O(n+p)
Constraints:
1<=n<=20
1<=p<=50
1<=a[i],b[i]<=20
1<=d[i]<=100 | class Solution:
def solve(self, n, p, a, b, d):
ans, source = [], list(set(a) - set(b))
dic = {a[i]: (b[i], d[i]) for i in range(p)}
source.sort()
for s in source:
e = dic[s]
one, two, three = s, e[0], e[1]
while two in dic:
e = dic[two]
two, three = e[0], min(three, e[1])
ans.append([one, two, three])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR LIST FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR RETURN VAR |
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines.
1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof.
2. Every house with only one incoming and no outgoing pipe gets a tap.
The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi.
Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible.
Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them.
Example 1:
Input:
n = 9, p = 6
a[] = {7,5,4,2,9,3}
b[] = {4,9,6,8,7,1}
d[] = {98,72,10,22,17,66}
Output:
3
2 8 22
3 1 66
5 6 10
Explanation:
Connected components are
3->1, 5->9->7->4->6 and 2->8.
Therefore, our answer is 3
followed by 2 8 22, 3 1 66, 5 6 10.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number).
Expected Time Complexity: O(n+p)
Expected Auxiliary Space: O(n+p)
Constraints:
1<=n<=20
1<=p<=50
1<=a[i],b[i]<=20
1<=d[i]<=100 | class Solution:
def dfs(self, i, start, end, wieght, ans):
if not start[i]:
return i
ans[0] = min(ans[0], wieght[i])
return self.dfs(start[i], start, end, wieght, ans)
def solve(self, n, p, a, b, d):
start = [0] * (n + 1)
end = [0] * (n + 1)
wieght = [0] * (n + 1)
for i in range(p):
start[a[i]] = b[i]
end[b[i]] = a[i]
wieght[a[i]] = d[i]
res = []
for i in range(1, n + 1):
ans = [10**10]
if start[i] and not end[i]:
endcomponent = self.dfs(i, start, end, wieght, ans)
res.append([i, endcomponent, ans[0]])
return res | CLASS_DEF FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST BIN_OP NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER RETURN VAR |
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines.
1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof.
2. Every house with only one incoming and no outgoing pipe gets a tap.
The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi.
Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible.
Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them.
Example 1:
Input:
n = 9, p = 6
a[] = {7,5,4,2,9,3}
b[] = {4,9,6,8,7,1}
d[] = {98,72,10,22,17,66}
Output:
3
2 8 22
3 1 66
5 6 10
Explanation:
Connected components are
3->1, 5->9->7->4->6 and 2->8.
Therefore, our answer is 3
followed by 2 8 22, 3 1 66, 5 6 10.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number).
Expected Time Complexity: O(n+p)
Expected Auxiliary Space: O(n+p)
Constraints:
1<=n<=20
1<=p<=50
1<=a[i],b[i]<=20
1<=d[i]<=100 | import sys
class Solution:
def DFS(self, u, starting_vertex, ending_vertex, diameter, temp_dia):
if starting_vertex[u] == 0:
return u
if diameter[u] < temp_dia[0]:
temp_dia[0] = diameter[u]
return self.DFS(
starting_vertex[u], starting_vertex, ending_vertex, diameter, temp_dia
)
def solve(self, n, p, a, b, d):
starting_vertex = [(0) for i in range(1000)]
ending_vertex = [(0) for i in range(1000)]
diameter = [(0) for i in range(1000)]
ans = []
for i in range(len(a)):
starting_vertex[a[i]] = b[i]
diameter[a[i]] = d[i]
ending_vertex[b[i]] = a[i]
for i in range(1, n + 1):
if ending_vertex[i] == 0 and starting_vertex[i]:
temp_dia = [sys.maxsize]
end = self.DFS(i, starting_vertex, ending_vertex, diameter, temp_dia)
ans.append([i, end, temp_dia[0]])
ans.sort()
return ans | IMPORT CLASS_DEF FUNC_DEF IF VAR VAR NUMBER RETURN VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR |
There are n houses and p water pipes in Geek Colony. Every house has at most one pipe going into it and at most one pipe going out of it. Geek needs to install pairs of tanks and taps in the colony according to the following guidelines.
1. Every house with one outgoing pipe but no incoming pipe gets a tank on its roof.
2. Every house with only one incoming and no outgoing pipe gets a tap.
The Geek council has proposed a network of pipes where connections are denoted by three input values: ai, bi, di denoting the pipe of diameter di from house ai to house bi.
Find a more efficient way for the construction of this network of pipes. Minimize the diameter of pipes wherever possible.
Note: The generated output will have the following format. The first line will contain t, denoting the total number of pairs of tanks and taps installed. The next t lines contain three integers each: house number of tank, house number of tap, and the minimum diameter of pipe between them.
Example 1:
Input:
n = 9, p = 6
a[] = {7,5,4,2,9,3}
b[] = {4,9,6,8,7,1}
d[] = {98,72,10,22,17,66}
Output:
3
2 8 22
3 1 66
5 6 10
Explanation:
Connected components are
3->1, 5->9->7->4->6 and 2->8.
Therefore, our answer is 3
followed by 2 8 22, 3 1 66, 5 6 10.
Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes an integer n(the number of houses), p(the number of pipes),the array a[] , b[] and d[] (where d[i] denoting the diameter of the ith pipe from the house a[i] to house b[i]) as input parameter and returns the array of pairs of tanks and taps installed i.e ith element of the array contains three integers: house number of tank, house number of tap and the minimum diameter of pipe between them. Note that, returned array must be sorted based on the house number containing a tank (i.e. smaller house number should come before a large house number).
Expected Time Complexity: O(n+p)
Expected Auxiliary Space: O(n+p)
Constraints:
1<=n<=20
1<=p<=50
1<=a[i],b[i]<=20
1<=d[i]<=100 | class Solution:
def dfs(self, v, out, inn, wt, ans):
if out[v] == 0:
ans[-1][1] = v
return
ans[-1][2] = min(ans[-1][2], wt[v])
self.dfs(out[v], out, inn, wt, ans)
def solve(self, n, p, a, b, d):
out = [0] * (n + 1)
inn = [0] * (n + 1)
wt = [0] * (n + 1)
for i in range(p):
out[a[i]] = b[i]
inn[b[i]] = a[i]
wt[a[i]] = d[i]
ans = []
for i in range(1, n + 1):
if inn[i] == 0 and out[i]:
ans.append([i, -1, wt[i]])
self.dfs(i, out, inn, wt, ans)
return ans | CLASS_DEF FUNC_DEF IF VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR RETURN ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR LIST VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.