description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Initially, Alice and Bob both have a bag of N stones each with every stone having a lowercase alphabet written on it. Both players know the stones of the other player.
Alice and Bob want to create a string S of length 2 \cdot N using the stones they have. They play a game to create the string. The rules of the game are as follows:
Initially, all the indices of S are empty.
Alice starts the game. The players take alternating turns. In each turn, the player can choose one of the stones from their bag and place it at any empty index i (1 β€ i β€ 2 \cdot N) of S.
The game ends when both the players have used all their stones.
Alice wants S to be as lexicographically small as possible while Bob wants S to be as lexicographically large as possible.
Find the final string S formed by characters written on stones if both Alice and Bob play optimally!
Note: A string X is lexicographically smaller than a string Y if and only if one of the following holds:
X is a prefix of Y and X \ne Y
In the first position where X and Y differ, the string X has a letter that appears earlier in the alphabet than the corresponding letter in Y.
------ Input Format ------
- The first line contains an integer T - the number of test cases. The description of T test cases follows:
- The first line of each test case contains an integer N - the number of stones each player has.
- The second line of each test case contains a string A of length N describing the stones in Alice's bag initially.
- The third line of each test case contains a string B of length N describing the stones in Bob's bag initially.
------ Output Format ------
For each test case, print the final string S formed if Alice and Bob play optimally.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 10^{5}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
2
3
aza
abz
4
cccc
cccc
----- Sample Output 1 ------
azabza
cccccccc
----- explanation 1 ------
Test case-1: Initially Alice's stones are aza , while Bob's stones are abz. Initially $S$ is _ _ _ _ _ _.
- Alice's turn
Alice puts stone a at index $i = 1$.
String $S$: a _ _ _ _ _
Alice's stones: [za]
Bob's stones: [abz]
- Bob's turn
Bob puts stone z at index $i = 2$.
String $S$: a z _ _ _ _
Alice's stones: [za]
Bob's stones: [ab]
- Alice's turn
Alice puts stone a at index $i = 3$.
String $S$: a z a _ _ _
Alice's stones: [z]
Bob's stones: [ab]
- Bob's turn
Bob puts stone a at index $i = 6$.
String $S$: a z a _ _ a
Alice's stones: [z]
Bob's stones: [b]
- Alice's turn
Alice puts stone z at index $i = 5$.
String $S$: a z a _ z a
Alice's stones: []
Bob's stones: [b]
- Bob's turn
Bob puts stone b at index $i = 4$.
String $S$: a z a b z a
Alice's stones: []
Bob's stones: []
Since both the players have used all their stones, the game ends.
So the final string $S$ is azabza. | for _ in range(int(input())):
n = int(input())
k = [i for i in input()]
a = [i for i in input()]
s = [" " for i in range(2 * n)]
st = 0
en = 2 * n - 1
k.sort()
a.sort(reverse=True)
for i in range(2 * n):
if i % 2 == 0:
if k[0] < a[0]:
s[st] = k[0]
st += 1
if k:
k.pop(0)
else:
s[en] = k[-1]
en -= 1
if k:
k.pop(-1)
elif not k or a[0] > k[0]:
s[st] = a[0]
st += 1
if a:
a.pop(0)
else:
s[en] = a[-1]
en -= 1
if a:
a.pop(-1)
s = "".join(s)
print(s) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR STRING VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR |
Initially, Alice and Bob both have a bag of N stones each with every stone having a lowercase alphabet written on it. Both players know the stones of the other player.
Alice and Bob want to create a string S of length 2 \cdot N using the stones they have. They play a game to create the string. The rules of the game are as follows:
Initially, all the indices of S are empty.
Alice starts the game. The players take alternating turns. In each turn, the player can choose one of the stones from their bag and place it at any empty index i (1 β€ i β€ 2 \cdot N) of S.
The game ends when both the players have used all their stones.
Alice wants S to be as lexicographically small as possible while Bob wants S to be as lexicographically large as possible.
Find the final string S formed by characters written on stones if both Alice and Bob play optimally!
Note: A string X is lexicographically smaller than a string Y if and only if one of the following holds:
X is a prefix of Y and X \ne Y
In the first position where X and Y differ, the string X has a letter that appears earlier in the alphabet than the corresponding letter in Y.
------ Input Format ------
- The first line contains an integer T - the number of test cases. The description of T test cases follows:
- The first line of each test case contains an integer N - the number of stones each player has.
- The second line of each test case contains a string A of length N describing the stones in Alice's bag initially.
- The third line of each test case contains a string B of length N describing the stones in Bob's bag initially.
------ Output Format ------
For each test case, print the final string S formed if Alice and Bob play optimally.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 10^{5}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
2
3
aza
abz
4
cccc
cccc
----- Sample Output 1 ------
azabza
cccccccc
----- explanation 1 ------
Test case-1: Initially Alice's stones are aza , while Bob's stones are abz. Initially $S$ is _ _ _ _ _ _.
- Alice's turn
Alice puts stone a at index $i = 1$.
String $S$: a _ _ _ _ _
Alice's stones: [za]
Bob's stones: [abz]
- Bob's turn
Bob puts stone z at index $i = 2$.
String $S$: a z _ _ _ _
Alice's stones: [za]
Bob's stones: [ab]
- Alice's turn
Alice puts stone a at index $i = 3$.
String $S$: a z a _ _ _
Alice's stones: [z]
Bob's stones: [ab]
- Bob's turn
Bob puts stone a at index $i = 6$.
String $S$: a z a _ _ a
Alice's stones: [z]
Bob's stones: [b]
- Alice's turn
Alice puts stone z at index $i = 5$.
String $S$: a z a _ z a
Alice's stones: []
Bob's stones: [b]
- Bob's turn
Bob puts stone b at index $i = 4$.
String $S$: a z a b z a
Alice's stones: []
Bob's stones: []
Since both the players have used all their stones, the game ends.
So the final string $S$ is azabza. | t = int(input())
for i in range(t):
n = int(input())
a = [j for j in input()]
b = [j for j in input()]
a.sort()
b.sort()
i1 = 0
i2 = n - 1
j1 = 0
j2 = n - 1
s = ""
s1 = ""
while j2 >= j1:
if a[i1] < b[j2]:
s += a[i1]
i1 += 1
else:
s1 = a[i2] + s1
i2 -= 1
if i1 < n and b[j2] > a[i1]:
s += b[j2]
j2 -= 1
else:
s1 = b[j1] + s1
j1 += 1
print(s + s1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Initially, Alice and Bob both have a bag of N stones each with every stone having a lowercase alphabet written on it. Both players know the stones of the other player.
Alice and Bob want to create a string S of length 2 \cdot N using the stones they have. They play a game to create the string. The rules of the game are as follows:
Initially, all the indices of S are empty.
Alice starts the game. The players take alternating turns. In each turn, the player can choose one of the stones from their bag and place it at any empty index i (1 β€ i β€ 2 \cdot N) of S.
The game ends when both the players have used all their stones.
Alice wants S to be as lexicographically small as possible while Bob wants S to be as lexicographically large as possible.
Find the final string S formed by characters written on stones if both Alice and Bob play optimally!
Note: A string X is lexicographically smaller than a string Y if and only if one of the following holds:
X is a prefix of Y and X \ne Y
In the first position where X and Y differ, the string X has a letter that appears earlier in the alphabet than the corresponding letter in Y.
------ Input Format ------
- The first line contains an integer T - the number of test cases. The description of T test cases follows:
- The first line of each test case contains an integer N - the number of stones each player has.
- The second line of each test case contains a string A of length N describing the stones in Alice's bag initially.
- The third line of each test case contains a string B of length N describing the stones in Bob's bag initially.
------ Output Format ------
For each test case, print the final string S formed if Alice and Bob play optimally.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 10^{5}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
2
3
aza
abz
4
cccc
cccc
----- Sample Output 1 ------
azabza
cccccccc
----- explanation 1 ------
Test case-1: Initially Alice's stones are aza , while Bob's stones are abz. Initially $S$ is _ _ _ _ _ _.
- Alice's turn
Alice puts stone a at index $i = 1$.
String $S$: a _ _ _ _ _
Alice's stones: [za]
Bob's stones: [abz]
- Bob's turn
Bob puts stone z at index $i = 2$.
String $S$: a z _ _ _ _
Alice's stones: [za]
Bob's stones: [ab]
- Alice's turn
Alice puts stone a at index $i = 3$.
String $S$: a z a _ _ _
Alice's stones: [z]
Bob's stones: [ab]
- Bob's turn
Bob puts stone a at index $i = 6$.
String $S$: a z a _ _ a
Alice's stones: [z]
Bob's stones: [b]
- Alice's turn
Alice puts stone z at index $i = 5$.
String $S$: a z a _ z a
Alice's stones: []
Bob's stones: [b]
- Bob's turn
Bob puts stone b at index $i = 4$.
String $S$: a z a b z a
Alice's stones: []
Bob's stones: []
Since both the players have used all their stones, the game ends.
So the final string $S$ is azabza. | def func():
n = int(input())
s1 = [i for i in input()]
s2 = [i for i in input()]
s1.sort()
s2.sort(reverse=True)
news = [(0) for i in range(2 * n)]
s1s = 0
s1e = n - 1
s2s = 0
s2e = n - 1
x = 0
y = 2 * n - 1
for i in range(n):
if s1[s1s] < s2[s2s]:
news[x] = s1[s1s]
s1s += 1
x += 1
else:
news[y] = s1[s1e]
s1e -= 1
y -= 1
if s1s < n and s2s < n and s2[s2s] > s1[s1s]:
news[x] = s2[s2s]
x += 1
s2s += 1
else:
news[y] = s2[s2e]
s2e -= 1
y -= 1
print("".join(news))
t = int(input())
for i in range(t):
func() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Initially, Alice and Bob both have a bag of N stones each with every stone having a lowercase alphabet written on it. Both players know the stones of the other player.
Alice and Bob want to create a string S of length 2 \cdot N using the stones they have. They play a game to create the string. The rules of the game are as follows:
Initially, all the indices of S are empty.
Alice starts the game. The players take alternating turns. In each turn, the player can choose one of the stones from their bag and place it at any empty index i (1 β€ i β€ 2 \cdot N) of S.
The game ends when both the players have used all their stones.
Alice wants S to be as lexicographically small as possible while Bob wants S to be as lexicographically large as possible.
Find the final string S formed by characters written on stones if both Alice and Bob play optimally!
Note: A string X is lexicographically smaller than a string Y if and only if one of the following holds:
X is a prefix of Y and X \ne Y
In the first position where X and Y differ, the string X has a letter that appears earlier in the alphabet than the corresponding letter in Y.
------ Input Format ------
- The first line contains an integer T - the number of test cases. The description of T test cases follows:
- The first line of each test case contains an integer N - the number of stones each player has.
- The second line of each test case contains a string A of length N describing the stones in Alice's bag initially.
- The third line of each test case contains a string B of length N describing the stones in Bob's bag initially.
------ Output Format ------
For each test case, print the final string S formed if Alice and Bob play optimally.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 10^{5}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
2
3
aza
abz
4
cccc
cccc
----- Sample Output 1 ------
azabza
cccccccc
----- explanation 1 ------
Test case-1: Initially Alice's stones are aza , while Bob's stones are abz. Initially $S$ is _ _ _ _ _ _.
- Alice's turn
Alice puts stone a at index $i = 1$.
String $S$: a _ _ _ _ _
Alice's stones: [za]
Bob's stones: [abz]
- Bob's turn
Bob puts stone z at index $i = 2$.
String $S$: a z _ _ _ _
Alice's stones: [za]
Bob's stones: [ab]
- Alice's turn
Alice puts stone a at index $i = 3$.
String $S$: a z a _ _ _
Alice's stones: [z]
Bob's stones: [ab]
- Bob's turn
Bob puts stone a at index $i = 6$.
String $S$: a z a _ _ a
Alice's stones: [z]
Bob's stones: [b]
- Alice's turn
Alice puts stone z at index $i = 5$.
String $S$: a z a _ z a
Alice's stones: []
Bob's stones: [b]
- Bob's turn
Bob puts stone b at index $i = 4$.
String $S$: a z a b z a
Alice's stones: []
Bob's stones: []
Since both the players have used all their stones, the game ends.
So the final string $S$ is azabza. | t = int(input())
for _ in range(t):
n = int(input())
a = input().strip()
b = input().strip()
a = list(a)
b = list(b)
a.sort()
b.sort()
a = "".join(a)
b = "".join(b)
i1 = 0
j1 = n - 1
i2 = 0
j2 = n - 1
end = 2 * n
i = 0
ans1 = ""
ans2 = ""
while i < end:
if i1 <= j1 and i2 <= j2:
if i % 2 == 0:
if a[i1] < b[j2]:
ans1 = ans1 + a[i1]
i1 += 1
else:
ans2 = ans2 + a[j1]
j1 -= 1
elif a[i1] < b[j2]:
ans1 = ans1 + b[j2]
j2 -= 1
else:
ans2 = ans2 + b[i2]
i2 += 1
elif i1 <= j1:
ans1 = ans1 + a[i1]
i1 += 1
elif i2 <= j2:
ans1 = ans1 + b[j2]
j2 -= 1
i = i + 1
ans = ans1 + ans2[::-1]
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL STRING VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING WHILE VAR VAR IF VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Initially, Alice and Bob both have a bag of N stones each with every stone having a lowercase alphabet written on it. Both players know the stones of the other player.
Alice and Bob want to create a string S of length 2 \cdot N using the stones they have. They play a game to create the string. The rules of the game are as follows:
Initially, all the indices of S are empty.
Alice starts the game. The players take alternating turns. In each turn, the player can choose one of the stones from their bag and place it at any empty index i (1 β€ i β€ 2 \cdot N) of S.
The game ends when both the players have used all their stones.
Alice wants S to be as lexicographically small as possible while Bob wants S to be as lexicographically large as possible.
Find the final string S formed by characters written on stones if both Alice and Bob play optimally!
Note: A string X is lexicographically smaller than a string Y if and only if one of the following holds:
X is a prefix of Y and X \ne Y
In the first position where X and Y differ, the string X has a letter that appears earlier in the alphabet than the corresponding letter in Y.
------ Input Format ------
- The first line contains an integer T - the number of test cases. The description of T test cases follows:
- The first line of each test case contains an integer N - the number of stones each player has.
- The second line of each test case contains a string A of length N describing the stones in Alice's bag initially.
- The third line of each test case contains a string B of length N describing the stones in Bob's bag initially.
------ Output Format ------
For each test case, print the final string S formed if Alice and Bob play optimally.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 10^{5}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
2
3
aza
abz
4
cccc
cccc
----- Sample Output 1 ------
azabza
cccccccc
----- explanation 1 ------
Test case-1: Initially Alice's stones are aza , while Bob's stones are abz. Initially $S$ is _ _ _ _ _ _.
- Alice's turn
Alice puts stone a at index $i = 1$.
String $S$: a _ _ _ _ _
Alice's stones: [za]
Bob's stones: [abz]
- Bob's turn
Bob puts stone z at index $i = 2$.
String $S$: a z _ _ _ _
Alice's stones: [za]
Bob's stones: [ab]
- Alice's turn
Alice puts stone a at index $i = 3$.
String $S$: a z a _ _ _
Alice's stones: [z]
Bob's stones: [ab]
- Bob's turn
Bob puts stone a at index $i = 6$.
String $S$: a z a _ _ a
Alice's stones: [z]
Bob's stones: [b]
- Alice's turn
Alice puts stone z at index $i = 5$.
String $S$: a z a _ z a
Alice's stones: []
Bob's stones: [b]
- Bob's turn
Bob puts stone b at index $i = 4$.
String $S$: a z a b z a
Alice's stones: []
Bob's stones: []
Since both the players have used all their stones, the game ends.
So the final string $S$ is azabza. | for _ in range(int(input())):
n = int(input())
a = sorted(input())
b = sorted(input(), reverse=True)
s = [0] * 2 * n
front = 0
back = 2 * n - 1
for i in range(2 * n):
if i & 1 == 0:
if a[0] >= b[0]:
s[back] = a[-1]
a.pop(-1)
back -= 1
else:
s[front] = a[0]
a.pop(0)
front += 1
elif a == []:
s[back] = b[-1]
elif b[0] <= a[0]:
s[back] = b[-1]
b.pop(-1)
back -= 1
else:
s[front] = b[0]
b.pop(0)
front += 1
print("".join(s)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR LIST ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Initially, Alice and Bob both have a bag of N stones each with every stone having a lowercase alphabet written on it. Both players know the stones of the other player.
Alice and Bob want to create a string S of length 2 \cdot N using the stones they have. They play a game to create the string. The rules of the game are as follows:
Initially, all the indices of S are empty.
Alice starts the game. The players take alternating turns. In each turn, the player can choose one of the stones from their bag and place it at any empty index i (1 β€ i β€ 2 \cdot N) of S.
The game ends when both the players have used all their stones.
Alice wants S to be as lexicographically small as possible while Bob wants S to be as lexicographically large as possible.
Find the final string S formed by characters written on stones if both Alice and Bob play optimally!
Note: A string X is lexicographically smaller than a string Y if and only if one of the following holds:
X is a prefix of Y and X \ne Y
In the first position where X and Y differ, the string X has a letter that appears earlier in the alphabet than the corresponding letter in Y.
------ Input Format ------
- The first line contains an integer T - the number of test cases. The description of T test cases follows:
- The first line of each test case contains an integer N - the number of stones each player has.
- The second line of each test case contains a string A of length N describing the stones in Alice's bag initially.
- The third line of each test case contains a string B of length N describing the stones in Bob's bag initially.
------ Output Format ------
For each test case, print the final string S formed if Alice and Bob play optimally.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 10^{5}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
2
3
aza
abz
4
cccc
cccc
----- Sample Output 1 ------
azabza
cccccccc
----- explanation 1 ------
Test case-1: Initially Alice's stones are aza , while Bob's stones are abz. Initially $S$ is _ _ _ _ _ _.
- Alice's turn
Alice puts stone a at index $i = 1$.
String $S$: a _ _ _ _ _
Alice's stones: [za]
Bob's stones: [abz]
- Bob's turn
Bob puts stone z at index $i = 2$.
String $S$: a z _ _ _ _
Alice's stones: [za]
Bob's stones: [ab]
- Alice's turn
Alice puts stone a at index $i = 3$.
String $S$: a z a _ _ _
Alice's stones: [z]
Bob's stones: [ab]
- Bob's turn
Bob puts stone a at index $i = 6$.
String $S$: a z a _ _ a
Alice's stones: [z]
Bob's stones: [b]
- Alice's turn
Alice puts stone z at index $i = 5$.
String $S$: a z a _ z a
Alice's stones: []
Bob's stones: [b]
- Bob's turn
Bob puts stone b at index $i = 4$.
String $S$: a z a b z a
Alice's stones: []
Bob's stones: []
Since both the players have used all their stones, the game ends.
So the final string $S$ is azabza. | t = int(input())
while t > 0:
n = int(input())
s1 = list(input())
s2 = list(input())
s1.sort()
s2.sort()
a = 0
a_end = n - 1
b_end = 0
b = n - 1
s = ["0"] * (2 * n)
end = 2 * n - 1
start = 0
for i in range(2 * n):
if b < b_end:
s[start] = s1[a]
a += 1
start += 1
continue
if a > a_end:
s[start] = s2[b]
b -= 1
start += 1
continue
if i % 2 == 0:
if s1[a] < s2[b]:
s[start] = s1[a]
a += 1
start += 1
else:
s[end] = s1[a_end]
end -= 1
a_end -= 1
elif s2[b] > s1[a]:
s[start] = s2[b]
start += 1
b -= 1
else:
s[end] = s2[b_end]
end -= 1
b_end += 1
ans = ""
for x in s:
ans = ans + x
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 FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST STRING BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR STRING FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER |
Initially, Alice and Bob both have a bag of N stones each with every stone having a lowercase alphabet written on it. Both players know the stones of the other player.
Alice and Bob want to create a string S of length 2 \cdot N using the stones they have. They play a game to create the string. The rules of the game are as follows:
Initially, all the indices of S are empty.
Alice starts the game. The players take alternating turns. In each turn, the player can choose one of the stones from their bag and place it at any empty index i (1 β€ i β€ 2 \cdot N) of S.
The game ends when both the players have used all their stones.
Alice wants S to be as lexicographically small as possible while Bob wants S to be as lexicographically large as possible.
Find the final string S formed by characters written on stones if both Alice and Bob play optimally!
Note: A string X is lexicographically smaller than a string Y if and only if one of the following holds:
X is a prefix of Y and X \ne Y
In the first position where X and Y differ, the string X has a letter that appears earlier in the alphabet than the corresponding letter in Y.
------ Input Format ------
- The first line contains an integer T - the number of test cases. The description of T test cases follows:
- The first line of each test case contains an integer N - the number of stones each player has.
- The second line of each test case contains a string A of length N describing the stones in Alice's bag initially.
- The third line of each test case contains a string B of length N describing the stones in Bob's bag initially.
------ Output Format ------
For each test case, print the final string S formed if Alice and Bob play optimally.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 10^{5}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
2
3
aza
abz
4
cccc
cccc
----- Sample Output 1 ------
azabza
cccccccc
----- explanation 1 ------
Test case-1: Initially Alice's stones are aza , while Bob's stones are abz. Initially $S$ is _ _ _ _ _ _.
- Alice's turn
Alice puts stone a at index $i = 1$.
String $S$: a _ _ _ _ _
Alice's stones: [za]
Bob's stones: [abz]
- Bob's turn
Bob puts stone z at index $i = 2$.
String $S$: a z _ _ _ _
Alice's stones: [za]
Bob's stones: [ab]
- Alice's turn
Alice puts stone a at index $i = 3$.
String $S$: a z a _ _ _
Alice's stones: [z]
Bob's stones: [ab]
- Bob's turn
Bob puts stone a at index $i = 6$.
String $S$: a z a _ _ a
Alice's stones: [z]
Bob's stones: [b]
- Alice's turn
Alice puts stone z at index $i = 5$.
String $S$: a z a _ z a
Alice's stones: []
Bob's stones: [b]
- Bob's turn
Bob puts stone b at index $i = 4$.
String $S$: a z a b z a
Alice's stones: []
Bob's stones: []
Since both the players have used all their stones, the game ends.
So the final string $S$ is azabza. | T = int(input())
def solve():
n = int(input())
a = str(input())
b = str(input())
a = sorted(a)
b = sorted(b, reverse=True)
front = ""
back = ""
for i in range(2 * n):
if not i & 1:
if len(b) != 0 and a[0] >= b[0]:
back = a.pop() + back
else:
front += a.pop(0)
elif len(a) != 0 and a[0] >= b[0]:
back = b.pop() + back
else:
front += b.pop(0)
print(front + back)
for t in range(T):
solve() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Initially, Alice and Bob both have a bag of N stones each with every stone having a lowercase alphabet written on it. Both players know the stones of the other player.
Alice and Bob want to create a string S of length 2 \cdot N using the stones they have. They play a game to create the string. The rules of the game are as follows:
Initially, all the indices of S are empty.
Alice starts the game. The players take alternating turns. In each turn, the player can choose one of the stones from their bag and place it at any empty index i (1 β€ i β€ 2 \cdot N) of S.
The game ends when both the players have used all their stones.
Alice wants S to be as lexicographically small as possible while Bob wants S to be as lexicographically large as possible.
Find the final string S formed by characters written on stones if both Alice and Bob play optimally!
Note: A string X is lexicographically smaller than a string Y if and only if one of the following holds:
X is a prefix of Y and X \ne Y
In the first position where X and Y differ, the string X has a letter that appears earlier in the alphabet than the corresponding letter in Y.
------ Input Format ------
- The first line contains an integer T - the number of test cases. The description of T test cases follows:
- The first line of each test case contains an integer N - the number of stones each player has.
- The second line of each test case contains a string A of length N describing the stones in Alice's bag initially.
- The third line of each test case contains a string B of length N describing the stones in Bob's bag initially.
------ Output Format ------
For each test case, print the final string S formed if Alice and Bob play optimally.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 10^{5}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
2
3
aza
abz
4
cccc
cccc
----- Sample Output 1 ------
azabza
cccccccc
----- explanation 1 ------
Test case-1: Initially Alice's stones are aza , while Bob's stones are abz. Initially $S$ is _ _ _ _ _ _.
- Alice's turn
Alice puts stone a at index $i = 1$.
String $S$: a _ _ _ _ _
Alice's stones: [za]
Bob's stones: [abz]
- Bob's turn
Bob puts stone z at index $i = 2$.
String $S$: a z _ _ _ _
Alice's stones: [za]
Bob's stones: [ab]
- Alice's turn
Alice puts stone a at index $i = 3$.
String $S$: a z a _ _ _
Alice's stones: [z]
Bob's stones: [ab]
- Bob's turn
Bob puts stone a at index $i = 6$.
String $S$: a z a _ _ a
Alice's stones: [z]
Bob's stones: [b]
- Alice's turn
Alice puts stone z at index $i = 5$.
String $S$: a z a _ z a
Alice's stones: []
Bob's stones: [b]
- Bob's turn
Bob puts stone b at index $i = 4$.
String $S$: a z a b z a
Alice's stones: []
Bob's stones: []
Since both the players have used all their stones, the game ends.
So the final string $S$ is azabza. | T = int(input())
for K in range(T):
n = int(input())
x = input()
y = input()
a = list(x)
b = list(y)
a.sort()
b.sort()
b = b[::-1]
o = [0] * 2 * n
front, end, startA, endA, startB, endB = 0, 2 * n - 1, 0, n - 1, 0, n - 1
for i in range(2 * n):
if i % 2 == 0:
if startB <= endB and a[startA] >= b[startB]:
o[end] = a[endA]
end -= 1
endA -= 1
else:
o[front] = a[startA]
front += 1
startA += 1
elif startA <= endA and a[startA] < b[startB]:
o[front] = b[startB]
front += 1
startB += 1
else:
o[end] = b[endB]
end -= 1
endB -= 1
output = ""
for i in o:
output += i
print(output) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR STRING FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Initially, Alice and Bob both have a bag of N stones each with every stone having a lowercase alphabet written on it. Both players know the stones of the other player.
Alice and Bob want to create a string S of length 2 \cdot N using the stones they have. They play a game to create the string. The rules of the game are as follows:
Initially, all the indices of S are empty.
Alice starts the game. The players take alternating turns. In each turn, the player can choose one of the stones from their bag and place it at any empty index i (1 β€ i β€ 2 \cdot N) of S.
The game ends when both the players have used all their stones.
Alice wants S to be as lexicographically small as possible while Bob wants S to be as lexicographically large as possible.
Find the final string S formed by characters written on stones if both Alice and Bob play optimally!
Note: A string X is lexicographically smaller than a string Y if and only if one of the following holds:
X is a prefix of Y and X \ne Y
In the first position where X and Y differ, the string X has a letter that appears earlier in the alphabet than the corresponding letter in Y.
------ Input Format ------
- The first line contains an integer T - the number of test cases. The description of T test cases follows:
- The first line of each test case contains an integer N - the number of stones each player has.
- The second line of each test case contains a string A of length N describing the stones in Alice's bag initially.
- The third line of each test case contains a string B of length N describing the stones in Bob's bag initially.
------ Output Format ------
For each test case, print the final string S formed if Alice and Bob play optimally.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 10^{5}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
2
3
aza
abz
4
cccc
cccc
----- Sample Output 1 ------
azabza
cccccccc
----- explanation 1 ------
Test case-1: Initially Alice's stones are aza , while Bob's stones are abz. Initially $S$ is _ _ _ _ _ _.
- Alice's turn
Alice puts stone a at index $i = 1$.
String $S$: a _ _ _ _ _
Alice's stones: [za]
Bob's stones: [abz]
- Bob's turn
Bob puts stone z at index $i = 2$.
String $S$: a z _ _ _ _
Alice's stones: [za]
Bob's stones: [ab]
- Alice's turn
Alice puts stone a at index $i = 3$.
String $S$: a z a _ _ _
Alice's stones: [z]
Bob's stones: [ab]
- Bob's turn
Bob puts stone a at index $i = 6$.
String $S$: a z a _ _ a
Alice's stones: [z]
Bob's stones: [b]
- Alice's turn
Alice puts stone z at index $i = 5$.
String $S$: a z a _ z a
Alice's stones: []
Bob's stones: [b]
- Bob's turn
Bob puts stone b at index $i = 4$.
String $S$: a z a b z a
Alice's stones: []
Bob's stones: []
Since both the players have used all their stones, the game ends.
So the final string $S$ is azabza. | def check(n, a, b):
s = [0] * (2 * n)
l, r = 0, 2 * n - 1
la, lb = 0, 0
ra, rb = n - 1, n - 1
for i in range(n):
if a[la] < b[rb]:
s[l] = a[la]
la += 1
l += 1
else:
s[r] = a[ra]
r -= 1
ra -= 1
if la < n and a[la] < b[rb]:
s[l] = b[rb]
l += 1
rb -= 1
else:
s[r] = b[lb]
r -= 1
lb += 1
return "".join(s)
for _ in range(int(input())):
n = int(input())
a = sorted(input())
b = sorted(input())
print(check(n, a, b)) | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
Initially, Alice and Bob both have a bag of N stones each with every stone having a lowercase alphabet written on it. Both players know the stones of the other player.
Alice and Bob want to create a string S of length 2 \cdot N using the stones they have. They play a game to create the string. The rules of the game are as follows:
Initially, all the indices of S are empty.
Alice starts the game. The players take alternating turns. In each turn, the player can choose one of the stones from their bag and place it at any empty index i (1 β€ i β€ 2 \cdot N) of S.
The game ends when both the players have used all their stones.
Alice wants S to be as lexicographically small as possible while Bob wants S to be as lexicographically large as possible.
Find the final string S formed by characters written on stones if both Alice and Bob play optimally!
Note: A string X is lexicographically smaller than a string Y if and only if one of the following holds:
X is a prefix of Y and X \ne Y
In the first position where X and Y differ, the string X has a letter that appears earlier in the alphabet than the corresponding letter in Y.
------ Input Format ------
- The first line contains an integer T - the number of test cases. The description of T test cases follows:
- The first line of each test case contains an integer N - the number of stones each player has.
- The second line of each test case contains a string A of length N describing the stones in Alice's bag initially.
- The third line of each test case contains a string B of length N describing the stones in Bob's bag initially.
------ Output Format ------
For each test case, print the final string S formed if Alice and Bob play optimally.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 10^{5}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
2
3
aza
abz
4
cccc
cccc
----- Sample Output 1 ------
azabza
cccccccc
----- explanation 1 ------
Test case-1: Initially Alice's stones are aza , while Bob's stones are abz. Initially $S$ is _ _ _ _ _ _.
- Alice's turn
Alice puts stone a at index $i = 1$.
String $S$: a _ _ _ _ _
Alice's stones: [za]
Bob's stones: [abz]
- Bob's turn
Bob puts stone z at index $i = 2$.
String $S$: a z _ _ _ _
Alice's stones: [za]
Bob's stones: [ab]
- Alice's turn
Alice puts stone a at index $i = 3$.
String $S$: a z a _ _ _
Alice's stones: [z]
Bob's stones: [ab]
- Bob's turn
Bob puts stone a at index $i = 6$.
String $S$: a z a _ _ a
Alice's stones: [z]
Bob's stones: [b]
- Alice's turn
Alice puts stone z at index $i = 5$.
String $S$: a z a _ z a
Alice's stones: []
Bob's stones: [b]
- Bob's turn
Bob puts stone b at index $i = 4$.
String $S$: a z a b z a
Alice's stones: []
Bob's stones: []
Since both the players have used all their stones, the game ends.
So the final string $S$ is azabza. | t = int(input())
for i in range(t):
n = int(input())
a = input()
b = input()
la = sorted(list(a))
lb = sorted(list(b))
f = la + lb
l = 0
r = 2 * n - 1
for k in range(n):
if la[0] < lb[-1]:
f[l] = la[0]
l += 1
la.pop(0)
else:
f[r] = la[-1]
r -= 1
del la[-1]
if len(la) > 0:
if la[0] < lb[-1]:
f[l] = lb[-1]
l += 1
del lb[-1]
else:
f[r] = lb[0]
r -= 1
lb.pop(0)
else:
f[l] = lb[0]
lb.pop(0)
print("".join(f)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Initially, Alice and Bob both have a bag of N stones each with every stone having a lowercase alphabet written on it. Both players know the stones of the other player.
Alice and Bob want to create a string S of length 2 \cdot N using the stones they have. They play a game to create the string. The rules of the game are as follows:
Initially, all the indices of S are empty.
Alice starts the game. The players take alternating turns. In each turn, the player can choose one of the stones from their bag and place it at any empty index i (1 β€ i β€ 2 \cdot N) of S.
The game ends when both the players have used all their stones.
Alice wants S to be as lexicographically small as possible while Bob wants S to be as lexicographically large as possible.
Find the final string S formed by characters written on stones if both Alice and Bob play optimally!
Note: A string X is lexicographically smaller than a string Y if and only if one of the following holds:
X is a prefix of Y and X \ne Y
In the first position where X and Y differ, the string X has a letter that appears earlier in the alphabet than the corresponding letter in Y.
------ Input Format ------
- The first line contains an integer T - the number of test cases. The description of T test cases follows:
- The first line of each test case contains an integer N - the number of stones each player has.
- The second line of each test case contains a string A of length N describing the stones in Alice's bag initially.
- The third line of each test case contains a string B of length N describing the stones in Bob's bag initially.
------ Output Format ------
For each test case, print the final string S formed if Alice and Bob play optimally.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 10^{5}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
2
3
aza
abz
4
cccc
cccc
----- Sample Output 1 ------
azabza
cccccccc
----- explanation 1 ------
Test case-1: Initially Alice's stones are aza , while Bob's stones are abz. Initially $S$ is _ _ _ _ _ _.
- Alice's turn
Alice puts stone a at index $i = 1$.
String $S$: a _ _ _ _ _
Alice's stones: [za]
Bob's stones: [abz]
- Bob's turn
Bob puts stone z at index $i = 2$.
String $S$: a z _ _ _ _
Alice's stones: [za]
Bob's stones: [ab]
- Alice's turn
Alice puts stone a at index $i = 3$.
String $S$: a z a _ _ _
Alice's stones: [z]
Bob's stones: [ab]
- Bob's turn
Bob puts stone a at index $i = 6$.
String $S$: a z a _ _ a
Alice's stones: [z]
Bob's stones: [b]
- Alice's turn
Alice puts stone z at index $i = 5$.
String $S$: a z a _ z a
Alice's stones: []
Bob's stones: [b]
- Bob's turn
Bob puts stone b at index $i = 4$.
String $S$: a z a b z a
Alice's stones: []
Bob's stones: []
Since both the players have used all their stones, the game ends.
So the final string $S$ is azabza. | for t in range(int(input())):
n = int(input())
St1 = sorted(input())
St2 = sorted(input())[::-1]
St = St1 + St2
x = 0
y = 2 * n - 1
S1s = 0
S1e = n - 1
S2s = 0
S2e = n - 1
for i in range(n):
try:
if St1[S1s] < St2[S2s]:
St[x] = St1[S1s]
S1s += 1
x += 1
else:
St[y] = St1[S1e]
S1e -= 1
y -= 1
if St2[S2s] > St1[S1s]:
St[x] = St2[S2s]
S2s += 1
x += 1
else:
St[y] = St2[S2e]
S2e -= 1
y -= 1
except:
pass
print(*St, sep="") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR STRING |
Initially, Alice and Bob both have a bag of N stones each with every stone having a lowercase alphabet written on it. Both players know the stones of the other player.
Alice and Bob want to create a string S of length 2 \cdot N using the stones they have. They play a game to create the string. The rules of the game are as follows:
Initially, all the indices of S are empty.
Alice starts the game. The players take alternating turns. In each turn, the player can choose one of the stones from their bag and place it at any empty index i (1 β€ i β€ 2 \cdot N) of S.
The game ends when both the players have used all their stones.
Alice wants S to be as lexicographically small as possible while Bob wants S to be as lexicographically large as possible.
Find the final string S formed by characters written on stones if both Alice and Bob play optimally!
Note: A string X is lexicographically smaller than a string Y if and only if one of the following holds:
X is a prefix of Y and X \ne Y
In the first position where X and Y differ, the string X has a letter that appears earlier in the alphabet than the corresponding letter in Y.
------ Input Format ------
- The first line contains an integer T - the number of test cases. The description of T test cases follows:
- The first line of each test case contains an integer N - the number of stones each player has.
- The second line of each test case contains a string A of length N describing the stones in Alice's bag initially.
- The third line of each test case contains a string B of length N describing the stones in Bob's bag initially.
------ Output Format ------
For each test case, print the final string S formed if Alice and Bob play optimally.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 10^{5}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
2
3
aza
abz
4
cccc
cccc
----- Sample Output 1 ------
azabza
cccccccc
----- explanation 1 ------
Test case-1: Initially Alice's stones are aza , while Bob's stones are abz. Initially $S$ is _ _ _ _ _ _.
- Alice's turn
Alice puts stone a at index $i = 1$.
String $S$: a _ _ _ _ _
Alice's stones: [za]
Bob's stones: [abz]
- Bob's turn
Bob puts stone z at index $i = 2$.
String $S$: a z _ _ _ _
Alice's stones: [za]
Bob's stones: [ab]
- Alice's turn
Alice puts stone a at index $i = 3$.
String $S$: a z a _ _ _
Alice's stones: [z]
Bob's stones: [ab]
- Bob's turn
Bob puts stone a at index $i = 6$.
String $S$: a z a _ _ a
Alice's stones: [z]
Bob's stones: [b]
- Alice's turn
Alice puts stone z at index $i = 5$.
String $S$: a z a _ z a
Alice's stones: []
Bob's stones: [b]
- Bob's turn
Bob puts stone b at index $i = 4$.
String $S$: a z a b z a
Alice's stones: []
Bob's stones: []
Since both the players have used all their stones, the game ends.
So the final string $S$ is azabza. | T = int(input())
for i in range(0, T):
N = int(input())
A = input()
B = input()
A = "".join(sorted(A))
B = "".join(sorted(B, reverse=True))
l = [0] * (2 * N)
point1 = 0
point2 = 2 * N - 1
for i in range(0, N):
if ord(A[0]) < ord(B[0]):
l[point1] = A[0]
A = A[1:]
point1 += 1
else:
l[point2] = A[-1]
A = A[:-1]
point2 -= 1
if i == N - 1:
if point1 == N:
l[point2] = B[0]
else:
l[point1] = B[0]
continue
if ord(B[0]) > ord(A[0]):
l[point1] = B[0]
B = B[1:]
point1 += 1
else:
l[point2] = B[-1]
B = B[:-1]
point2 -= 1
print("".join(l)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Initially, Alice and Bob both have a bag of N stones each with every stone having a lowercase alphabet written on it. Both players know the stones of the other player.
Alice and Bob want to create a string S of length 2 \cdot N using the stones they have. They play a game to create the string. The rules of the game are as follows:
Initially, all the indices of S are empty.
Alice starts the game. The players take alternating turns. In each turn, the player can choose one of the stones from their bag and place it at any empty index i (1 β€ i β€ 2 \cdot N) of S.
The game ends when both the players have used all their stones.
Alice wants S to be as lexicographically small as possible while Bob wants S to be as lexicographically large as possible.
Find the final string S formed by characters written on stones if both Alice and Bob play optimally!
Note: A string X is lexicographically smaller than a string Y if and only if one of the following holds:
X is a prefix of Y and X \ne Y
In the first position where X and Y differ, the string X has a letter that appears earlier in the alphabet than the corresponding letter in Y.
------ Input Format ------
- The first line contains an integer T - the number of test cases. The description of T test cases follows:
- The first line of each test case contains an integer N - the number of stones each player has.
- The second line of each test case contains a string A of length N describing the stones in Alice's bag initially.
- The third line of each test case contains a string B of length N describing the stones in Bob's bag initially.
------ Output Format ------
For each test case, print the final string S formed if Alice and Bob play optimally.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 10^{5}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
2
3
aza
abz
4
cccc
cccc
----- Sample Output 1 ------
azabza
cccccccc
----- explanation 1 ------
Test case-1: Initially Alice's stones are aza , while Bob's stones are abz. Initially $S$ is _ _ _ _ _ _.
- Alice's turn
Alice puts stone a at index $i = 1$.
String $S$: a _ _ _ _ _
Alice's stones: [za]
Bob's stones: [abz]
- Bob's turn
Bob puts stone z at index $i = 2$.
String $S$: a z _ _ _ _
Alice's stones: [za]
Bob's stones: [ab]
- Alice's turn
Alice puts stone a at index $i = 3$.
String $S$: a z a _ _ _
Alice's stones: [z]
Bob's stones: [ab]
- Bob's turn
Bob puts stone a at index $i = 6$.
String $S$: a z a _ _ a
Alice's stones: [z]
Bob's stones: [b]
- Alice's turn
Alice puts stone z at index $i = 5$.
String $S$: a z a _ z a
Alice's stones: []
Bob's stones: [b]
- Bob's turn
Bob puts stone b at index $i = 4$.
String $S$: a z a b z a
Alice's stones: []
Bob's stones: []
Since both the players have used all their stones, the game ends.
So the final string $S$ is azabza. | for t in range(int(input())):
n = int(input())
alice = list(input())
bob = list(input())
alice.sort()
bob.sort(reverse=True)
ans = [0] * (2 * n)
start, end = 0, 2 * n - 1
a, b = len(alice), len(bob)
while start < end:
i = 0
if alice[i] < bob[i]:
ans[start] = alice[i]
a -= 1
alice.pop(0)
start += 1
else:
ans[end] = alice[-1]
a -= 1
alice.pop()
end -= 1
if a > 0 and bob[i] > alice[i]:
ans[start] = bob[i]
b -= 1
bob.pop(0)
start += 1
else:
ans[end] = bob[-1]
b -= 1
bob.pop()
end -= 1
for i in ans:
print(i, end="")
print() | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
Initially, Alice and Bob both have a bag of N stones each with every stone having a lowercase alphabet written on it. Both players know the stones of the other player.
Alice and Bob want to create a string S of length 2 \cdot N using the stones they have. They play a game to create the string. The rules of the game are as follows:
Initially, all the indices of S are empty.
Alice starts the game. The players take alternating turns. In each turn, the player can choose one of the stones from their bag and place it at any empty index i (1 β€ i β€ 2 \cdot N) of S.
The game ends when both the players have used all their stones.
Alice wants S to be as lexicographically small as possible while Bob wants S to be as lexicographically large as possible.
Find the final string S formed by characters written on stones if both Alice and Bob play optimally!
Note: A string X is lexicographically smaller than a string Y if and only if one of the following holds:
X is a prefix of Y and X \ne Y
In the first position where X and Y differ, the string X has a letter that appears earlier in the alphabet than the corresponding letter in Y.
------ Input Format ------
- The first line contains an integer T - the number of test cases. The description of T test cases follows:
- The first line of each test case contains an integer N - the number of stones each player has.
- The second line of each test case contains a string A of length N describing the stones in Alice's bag initially.
- The third line of each test case contains a string B of length N describing the stones in Bob's bag initially.
------ Output Format ------
For each test case, print the final string S formed if Alice and Bob play optimally.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 10^{5}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
2
3
aza
abz
4
cccc
cccc
----- Sample Output 1 ------
azabza
cccccccc
----- explanation 1 ------
Test case-1: Initially Alice's stones are aza , while Bob's stones are abz. Initially $S$ is _ _ _ _ _ _.
- Alice's turn
Alice puts stone a at index $i = 1$.
String $S$: a _ _ _ _ _
Alice's stones: [za]
Bob's stones: [abz]
- Bob's turn
Bob puts stone z at index $i = 2$.
String $S$: a z _ _ _ _
Alice's stones: [za]
Bob's stones: [ab]
- Alice's turn
Alice puts stone a at index $i = 3$.
String $S$: a z a _ _ _
Alice's stones: [z]
Bob's stones: [ab]
- Bob's turn
Bob puts stone a at index $i = 6$.
String $S$: a z a _ _ a
Alice's stones: [z]
Bob's stones: [b]
- Alice's turn
Alice puts stone z at index $i = 5$.
String $S$: a z a _ z a
Alice's stones: []
Bob's stones: [b]
- Bob's turn
Bob puts stone b at index $i = 4$.
String $S$: a z a b z a
Alice's stones: []
Bob's stones: []
Since both the players have used all their stones, the game ends.
So the final string $S$ is azabza. | for _ in range(int(input())):
N = int(input())
A = sorted(input())
B = sorted(input(), reverse=True)
S = [""] * 2 * N
ai, an = 0, N - 1
bi, bn = 0, N - 1
si, sn = 0, 2 * N - 1
for i in range(N - 1):
if A[ai] < B[bi]:
S[si] = A[ai]
si += 1
ai += 1
else:
S[sn] = A[an]
sn -= 1
an -= 1
if B[bi] > A[ai]:
S[si] = B[bi]
si += 1
bi += 1
else:
S[sn] = B[bn]
sn -= 1
bn -= 1
if A[ai] < B[bi]:
S[si] = A[ai]
si += 1
ai += 1
else:
S[sn] = A[an]
sn -= 1
an -= 1
S[si] = B[bi]
print("".join(S)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST STRING NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Initially, Alice and Bob both have a bag of N stones each with every stone having a lowercase alphabet written on it. Both players know the stones of the other player.
Alice and Bob want to create a string S of length 2 \cdot N using the stones they have. They play a game to create the string. The rules of the game are as follows:
Initially, all the indices of S are empty.
Alice starts the game. The players take alternating turns. In each turn, the player can choose one of the stones from their bag and place it at any empty index i (1 β€ i β€ 2 \cdot N) of S.
The game ends when both the players have used all their stones.
Alice wants S to be as lexicographically small as possible while Bob wants S to be as lexicographically large as possible.
Find the final string S formed by characters written on stones if both Alice and Bob play optimally!
Note: A string X is lexicographically smaller than a string Y if and only if one of the following holds:
X is a prefix of Y and X \ne Y
In the first position where X and Y differ, the string X has a letter that appears earlier in the alphabet than the corresponding letter in Y.
------ Input Format ------
- The first line contains an integer T - the number of test cases. The description of T test cases follows:
- The first line of each test case contains an integer N - the number of stones each player has.
- The second line of each test case contains a string A of length N describing the stones in Alice's bag initially.
- The third line of each test case contains a string B of length N describing the stones in Bob's bag initially.
------ Output Format ------
For each test case, print the final string S formed if Alice and Bob play optimally.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 10^{5}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
2
3
aza
abz
4
cccc
cccc
----- Sample Output 1 ------
azabza
cccccccc
----- explanation 1 ------
Test case-1: Initially Alice's stones are aza , while Bob's stones are abz. Initially $S$ is _ _ _ _ _ _.
- Alice's turn
Alice puts stone a at index $i = 1$.
String $S$: a _ _ _ _ _
Alice's stones: [za]
Bob's stones: [abz]
- Bob's turn
Bob puts stone z at index $i = 2$.
String $S$: a z _ _ _ _
Alice's stones: [za]
Bob's stones: [ab]
- Alice's turn
Alice puts stone a at index $i = 3$.
String $S$: a z a _ _ _
Alice's stones: [z]
Bob's stones: [ab]
- Bob's turn
Bob puts stone a at index $i = 6$.
String $S$: a z a _ _ a
Alice's stones: [z]
Bob's stones: [b]
- Alice's turn
Alice puts stone z at index $i = 5$.
String $S$: a z a _ z a
Alice's stones: []
Bob's stones: [b]
- Bob's turn
Bob puts stone b at index $i = 4$.
String $S$: a z a b z a
Alice's stones: []
Bob's stones: []
Since both the players have used all their stones, the game ends.
So the final string $S$ is azabza. | for _ in range(int(input())):
N = int(input())
A = sorted(input())
B = sorted(input(), reverse=1)
A_start = 0
B_start = 0
A_end = N - 1
B_end = N - 1
Ss = ""
Se = ""
for i in range(N):
if A[A_start] < B[B_start]:
Ss += A[A_start]
A_start += 1
else:
Se = A[A_end] + Se
A_end -= 1
if A_end == -1 or A_start == N:
Ss += B[B_start]
elif B[B_start] > A[A_start]:
Ss += B[B_start]
B_start += 1
else:
Se = B[B_end] + Se
B_end -= 1
print(Ss + Se) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Initially, Alice and Bob both have a bag of N stones each with every stone having a lowercase alphabet written on it. Both players know the stones of the other player.
Alice and Bob want to create a string S of length 2 \cdot N using the stones they have. They play a game to create the string. The rules of the game are as follows:
Initially, all the indices of S are empty.
Alice starts the game. The players take alternating turns. In each turn, the player can choose one of the stones from their bag and place it at any empty index i (1 β€ i β€ 2 \cdot N) of S.
The game ends when both the players have used all their stones.
Alice wants S to be as lexicographically small as possible while Bob wants S to be as lexicographically large as possible.
Find the final string S formed by characters written on stones if both Alice and Bob play optimally!
Note: A string X is lexicographically smaller than a string Y if and only if one of the following holds:
X is a prefix of Y and X \ne Y
In the first position where X and Y differ, the string X has a letter that appears earlier in the alphabet than the corresponding letter in Y.
------ Input Format ------
- The first line contains an integer T - the number of test cases. The description of T test cases follows:
- The first line of each test case contains an integer N - the number of stones each player has.
- The second line of each test case contains a string A of length N describing the stones in Alice's bag initially.
- The third line of each test case contains a string B of length N describing the stones in Bob's bag initially.
------ Output Format ------
For each test case, print the final string S formed if Alice and Bob play optimally.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 10^{5}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
2
3
aza
abz
4
cccc
cccc
----- Sample Output 1 ------
azabza
cccccccc
----- explanation 1 ------
Test case-1: Initially Alice's stones are aza , while Bob's stones are abz. Initially $S$ is _ _ _ _ _ _.
- Alice's turn
Alice puts stone a at index $i = 1$.
String $S$: a _ _ _ _ _
Alice's stones: [za]
Bob's stones: [abz]
- Bob's turn
Bob puts stone z at index $i = 2$.
String $S$: a z _ _ _ _
Alice's stones: [za]
Bob's stones: [ab]
- Alice's turn
Alice puts stone a at index $i = 3$.
String $S$: a z a _ _ _
Alice's stones: [z]
Bob's stones: [ab]
- Bob's turn
Bob puts stone a at index $i = 6$.
String $S$: a z a _ _ a
Alice's stones: [z]
Bob's stones: [b]
- Alice's turn
Alice puts stone z at index $i = 5$.
String $S$: a z a _ z a
Alice's stones: []
Bob's stones: [b]
- Bob's turn
Bob puts stone b at index $i = 4$.
String $S$: a z a b z a
Alice's stones: []
Bob's stones: []
Since both the players have used all their stones, the game ends.
So the final string $S$ is azabza. | t = int(input())
while t > 0:
n = int(input())
s1 = input()
s2 = input()
s = ["" for _ in range(2 * n)]
f, f1, f2, e, e1, e2 = 0, 0, 0, 2 * n - 1, n - 1, n - 1
s1 = "".join(sorted(s1))
s2 = "".join(sorted(s2, reverse=True))
for i in range(2 * n):
if i % 2 == 0:
if f2 <= e2 and s1[f1] >= s2[f2]:
s[e] = s1[e1]
e -= 1
e1 -= 1
else:
s[f] = s1[f1]
f += 1
f1 += 1
elif f1 <= e1 and s1[f1] < s2[f2]:
s[f] = s2[f2]
f += 1
f2 += 1
else:
s[e] = s2[e2]
e -= 1
e2 -= 1
print("".join(s))
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 ASSIGN VAR STRING VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR NUMBER |
Initially, Alice and Bob both have a bag of N stones each with every stone having a lowercase alphabet written on it. Both players know the stones of the other player.
Alice and Bob want to create a string S of length 2 \cdot N using the stones they have. They play a game to create the string. The rules of the game are as follows:
Initially, all the indices of S are empty.
Alice starts the game. The players take alternating turns. In each turn, the player can choose one of the stones from their bag and place it at any empty index i (1 β€ i β€ 2 \cdot N) of S.
The game ends when both the players have used all their stones.
Alice wants S to be as lexicographically small as possible while Bob wants S to be as lexicographically large as possible.
Find the final string S formed by characters written on stones if both Alice and Bob play optimally!
Note: A string X is lexicographically smaller than a string Y if and only if one of the following holds:
X is a prefix of Y and X \ne Y
In the first position where X and Y differ, the string X has a letter that appears earlier in the alphabet than the corresponding letter in Y.
------ Input Format ------
- The first line contains an integer T - the number of test cases. The description of T test cases follows:
- The first line of each test case contains an integer N - the number of stones each player has.
- The second line of each test case contains a string A of length N describing the stones in Alice's bag initially.
- The third line of each test case contains a string B of length N describing the stones in Bob's bag initially.
------ Output Format ------
For each test case, print the final string S formed if Alice and Bob play optimally.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 10^{5}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
2
3
aza
abz
4
cccc
cccc
----- Sample Output 1 ------
azabza
cccccccc
----- explanation 1 ------
Test case-1: Initially Alice's stones are aza , while Bob's stones are abz. Initially $S$ is _ _ _ _ _ _.
- Alice's turn
Alice puts stone a at index $i = 1$.
String $S$: a _ _ _ _ _
Alice's stones: [za]
Bob's stones: [abz]
- Bob's turn
Bob puts stone z at index $i = 2$.
String $S$: a z _ _ _ _
Alice's stones: [za]
Bob's stones: [ab]
- Alice's turn
Alice puts stone a at index $i = 3$.
String $S$: a z a _ _ _
Alice's stones: [z]
Bob's stones: [ab]
- Bob's turn
Bob puts stone a at index $i = 6$.
String $S$: a z a _ _ a
Alice's stones: [z]
Bob's stones: [b]
- Alice's turn
Alice puts stone z at index $i = 5$.
String $S$: a z a _ z a
Alice's stones: []
Bob's stones: [b]
- Bob's turn
Bob puts stone b at index $i = 4$.
String $S$: a z a b z a
Alice's stones: []
Bob's stones: []
Since both the players have used all their stones, the game ends.
So the final string $S$ is azabza. | testcase = int(input(""))
for i in range(testcase):
size = int(input())
arr1 = list(input())
arr2 = list(input())
arr1.sort()
arr2.sort()
s = [0] * (2 * size)
lix = 0
rix = 2 * size - 1
for i in range(2 * size):
if i % 2 == 0:
if arr1[0] < arr2[-1]:
s[lix] = arr1[0]
lix += 1
del arr1[0]
else:
s[rix] = arr1[-1]
rix -= 1
del arr1[-1]
elif len(arr1) != 0 and arr2[-1] > arr1[0]:
s[lix] = arr2[-1]
lix += 1
del arr2[-1]
else:
s[rix] = arr2[0]
rix -= 1
del arr2[0]
print("".join(s)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Initially, Alice and Bob both have a bag of N stones each with every stone having a lowercase alphabet written on it. Both players know the stones of the other player.
Alice and Bob want to create a string S of length 2 \cdot N using the stones they have. They play a game to create the string. The rules of the game are as follows:
Initially, all the indices of S are empty.
Alice starts the game. The players take alternating turns. In each turn, the player can choose one of the stones from their bag and place it at any empty index i (1 β€ i β€ 2 \cdot N) of S.
The game ends when both the players have used all their stones.
Alice wants S to be as lexicographically small as possible while Bob wants S to be as lexicographically large as possible.
Find the final string S formed by characters written on stones if both Alice and Bob play optimally!
Note: A string X is lexicographically smaller than a string Y if and only if one of the following holds:
X is a prefix of Y and X \ne Y
In the first position where X and Y differ, the string X has a letter that appears earlier in the alphabet than the corresponding letter in Y.
------ Input Format ------
- The first line contains an integer T - the number of test cases. The description of T test cases follows:
- The first line of each test case contains an integer N - the number of stones each player has.
- The second line of each test case contains a string A of length N describing the stones in Alice's bag initially.
- The third line of each test case contains a string B of length N describing the stones in Bob's bag initially.
------ Output Format ------
For each test case, print the final string S formed if Alice and Bob play optimally.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 10^{5}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
2
3
aza
abz
4
cccc
cccc
----- Sample Output 1 ------
azabza
cccccccc
----- explanation 1 ------
Test case-1: Initially Alice's stones are aza , while Bob's stones are abz. Initially $S$ is _ _ _ _ _ _.
- Alice's turn
Alice puts stone a at index $i = 1$.
String $S$: a _ _ _ _ _
Alice's stones: [za]
Bob's stones: [abz]
- Bob's turn
Bob puts stone z at index $i = 2$.
String $S$: a z _ _ _ _
Alice's stones: [za]
Bob's stones: [ab]
- Alice's turn
Alice puts stone a at index $i = 3$.
String $S$: a z a _ _ _
Alice's stones: [z]
Bob's stones: [ab]
- Bob's turn
Bob puts stone a at index $i = 6$.
String $S$: a z a _ _ a
Alice's stones: [z]
Bob's stones: [b]
- Alice's turn
Alice puts stone z at index $i = 5$.
String $S$: a z a _ z a
Alice's stones: []
Bob's stones: [b]
- Bob's turn
Bob puts stone b at index $i = 4$.
String $S$: a z a b z a
Alice's stones: []
Bob's stones: []
Since both the players have used all their stones, the game ends.
So the final string $S$ is azabza. | for _ in range(int(input())):
l = int(input())
a = input()
b = input()
startA, endA, startB, endB = 0, l - 1, 0, l - 1
front, back = 0, 2 * l - 1
output = "_" * (2 * l)
a = "".join(sorted(a))
b = "".join(sorted(b))
b = b[::-1]
for i in range(2 * l):
if i % 2 == 0:
if startB <= endB and a[startA] >= b[startB]:
output = output[:back] + a[endA] + output[back + 1 :]
back -= 1
endA -= 1
else:
output = output[:front] + a[startA] + output[front + 1 :]
front += 1
startA += 1
elif startA <= endA and a[startA] < b[startB]:
output = output[:front] + b[startB] + output[front + 1 :]
front += 1
startB += 1
else:
output = output[:back] + b[endB] + output[back + 1 :]
back -= 1
endB -= 1
print(output) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP STRING BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Initially, Alice and Bob both have a bag of N stones each with every stone having a lowercase alphabet written on it. Both players know the stones of the other player.
Alice and Bob want to create a string S of length 2 \cdot N using the stones they have. They play a game to create the string. The rules of the game are as follows:
Initially, all the indices of S are empty.
Alice starts the game. The players take alternating turns. In each turn, the player can choose one of the stones from their bag and place it at any empty index i (1 β€ i β€ 2 \cdot N) of S.
The game ends when both the players have used all their stones.
Alice wants S to be as lexicographically small as possible while Bob wants S to be as lexicographically large as possible.
Find the final string S formed by characters written on stones if both Alice and Bob play optimally!
Note: A string X is lexicographically smaller than a string Y if and only if one of the following holds:
X is a prefix of Y and X \ne Y
In the first position where X and Y differ, the string X has a letter that appears earlier in the alphabet than the corresponding letter in Y.
------ Input Format ------
- The first line contains an integer T - the number of test cases. The description of T test cases follows:
- The first line of each test case contains an integer N - the number of stones each player has.
- The second line of each test case contains a string A of length N describing the stones in Alice's bag initially.
- The third line of each test case contains a string B of length N describing the stones in Bob's bag initially.
------ Output Format ------
For each test case, print the final string S formed if Alice and Bob play optimally.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 10^{5}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
2
3
aza
abz
4
cccc
cccc
----- Sample Output 1 ------
azabza
cccccccc
----- explanation 1 ------
Test case-1: Initially Alice's stones are aza , while Bob's stones are abz. Initially $S$ is _ _ _ _ _ _.
- Alice's turn
Alice puts stone a at index $i = 1$.
String $S$: a _ _ _ _ _
Alice's stones: [za]
Bob's stones: [abz]
- Bob's turn
Bob puts stone z at index $i = 2$.
String $S$: a z _ _ _ _
Alice's stones: [za]
Bob's stones: [ab]
- Alice's turn
Alice puts stone a at index $i = 3$.
String $S$: a z a _ _ _
Alice's stones: [z]
Bob's stones: [ab]
- Bob's turn
Bob puts stone a at index $i = 6$.
String $S$: a z a _ _ a
Alice's stones: [z]
Bob's stones: [b]
- Alice's turn
Alice puts stone z at index $i = 5$.
String $S$: a z a _ z a
Alice's stones: []
Bob's stones: [b]
- Bob's turn
Bob puts stone b at index $i = 4$.
String $S$: a z a b z a
Alice's stones: []
Bob's stones: []
Since both the players have used all their stones, the game ends.
So the final string $S$ is azabza. | for _ in range(int(input())):
n = int(input())
s1 = [i for i in input()]
s2 = [i for i in input()]
ans = ["" for i in range(2 * n)]
s1.sort()
s2.sort(reverse=True)
t = 0
s1i = 0
s1j = n - 1
s2i = 0
s2j = n - 1
k1 = 0
k2 = 2 * n - 1
while k1 <= k2:
if t == 0:
if s2i < n and s1[s1i] < s2[s2i]:
ans[k1] = s1[s1i]
k1 += 1
s1i += 1
else:
ans[k2] = s1[s1j]
k2 -= 1
s1j -= 1
elif t == 1:
if s1i < n and s1[s1i] < s2[s2i]:
ans[k1] = s2[s2i]
s2i += 1
k1 += 1
else:
ans[k2] = s2[s2j]
k2 -= 1
s2j -= 1
t += 1
t %= 2
print("".join(ans)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR STRING VAR FUNC_CALL VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER WHILE VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Initially, Alice and Bob both have a bag of N stones each with every stone having a lowercase alphabet written on it. Both players know the stones of the other player.
Alice and Bob want to create a string S of length 2 \cdot N using the stones they have. They play a game to create the string. The rules of the game are as follows:
Initially, all the indices of S are empty.
Alice starts the game. The players take alternating turns. In each turn, the player can choose one of the stones from their bag and place it at any empty index i (1 β€ i β€ 2 \cdot N) of S.
The game ends when both the players have used all their stones.
Alice wants S to be as lexicographically small as possible while Bob wants S to be as lexicographically large as possible.
Find the final string S formed by characters written on stones if both Alice and Bob play optimally!
Note: A string X is lexicographically smaller than a string Y if and only if one of the following holds:
X is a prefix of Y and X \ne Y
In the first position where X and Y differ, the string X has a letter that appears earlier in the alphabet than the corresponding letter in Y.
------ Input Format ------
- The first line contains an integer T - the number of test cases. The description of T test cases follows:
- The first line of each test case contains an integer N - the number of stones each player has.
- The second line of each test case contains a string A of length N describing the stones in Alice's bag initially.
- The third line of each test case contains a string B of length N describing the stones in Bob's bag initially.
------ Output Format ------
For each test case, print the final string S formed if Alice and Bob play optimally.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 10^{5}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
2
3
aza
abz
4
cccc
cccc
----- Sample Output 1 ------
azabza
cccccccc
----- explanation 1 ------
Test case-1: Initially Alice's stones are aza , while Bob's stones are abz. Initially $S$ is _ _ _ _ _ _.
- Alice's turn
Alice puts stone a at index $i = 1$.
String $S$: a _ _ _ _ _
Alice's stones: [za]
Bob's stones: [abz]
- Bob's turn
Bob puts stone z at index $i = 2$.
String $S$: a z _ _ _ _
Alice's stones: [za]
Bob's stones: [ab]
- Alice's turn
Alice puts stone a at index $i = 3$.
String $S$: a z a _ _ _
Alice's stones: [z]
Bob's stones: [ab]
- Bob's turn
Bob puts stone a at index $i = 6$.
String $S$: a z a _ _ a
Alice's stones: [z]
Bob's stones: [b]
- Alice's turn
Alice puts stone z at index $i = 5$.
String $S$: a z a _ z a
Alice's stones: []
Bob's stones: [b]
- Bob's turn
Bob puts stone b at index $i = 4$.
String $S$: a z a b z a
Alice's stones: []
Bob's stones: []
Since both the players have used all their stones, the game ends.
So the final string $S$ is azabza. | def solve(n, al, bo):
ans = ["" for i in range(2 * n)]
front, back, alS, alE, boS, boE = 0, 2 * n - 1, 0, n - 1, 0, n - 1
al = "".join(sorted(al))
bo = "".join(sorted(bo, reverse=True))
for i in range(2 * n):
if i & 1 == 0:
if boS <= boE and al[alS] < bo[boS]:
ans[front] = al[alS]
front, alS = front + 1, alS + 1
else:
ans[back] = al[alE]
back, alE = back - 1, alE - 1
elif alS <= alE and bo[boS] > al[alS]:
ans[front] = bo[boS]
front, boS = front + 1, boS + 1
else:
ans[back] = bo[boE]
back, boE = back - 1, boE - 1
print("".join(ans))
t = int(input())
for _ in range(t):
n = int(input())
al = input()
bo = input()
solve(n, al, bo) | FUNC_DEF ASSIGN VAR STRING VAR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR |
Initially, Alice and Bob both have a bag of N stones each with every stone having a lowercase alphabet written on it. Both players know the stones of the other player.
Alice and Bob want to create a string S of length 2 \cdot N using the stones they have. They play a game to create the string. The rules of the game are as follows:
Initially, all the indices of S are empty.
Alice starts the game. The players take alternating turns. In each turn, the player can choose one of the stones from their bag and place it at any empty index i (1 β€ i β€ 2 \cdot N) of S.
The game ends when both the players have used all their stones.
Alice wants S to be as lexicographically small as possible while Bob wants S to be as lexicographically large as possible.
Find the final string S formed by characters written on stones if both Alice and Bob play optimally!
Note: A string X is lexicographically smaller than a string Y if and only if one of the following holds:
X is a prefix of Y and X \ne Y
In the first position where X and Y differ, the string X has a letter that appears earlier in the alphabet than the corresponding letter in Y.
------ Input Format ------
- The first line contains an integer T - the number of test cases. The description of T test cases follows:
- The first line of each test case contains an integer N - the number of stones each player has.
- The second line of each test case contains a string A of length N describing the stones in Alice's bag initially.
- The third line of each test case contains a string B of length N describing the stones in Bob's bag initially.
------ Output Format ------
For each test case, print the final string S formed if Alice and Bob play optimally.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 10^{5}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
2
3
aza
abz
4
cccc
cccc
----- Sample Output 1 ------
azabza
cccccccc
----- explanation 1 ------
Test case-1: Initially Alice's stones are aza , while Bob's stones are abz. Initially $S$ is _ _ _ _ _ _.
- Alice's turn
Alice puts stone a at index $i = 1$.
String $S$: a _ _ _ _ _
Alice's stones: [za]
Bob's stones: [abz]
- Bob's turn
Bob puts stone z at index $i = 2$.
String $S$: a z _ _ _ _
Alice's stones: [za]
Bob's stones: [ab]
- Alice's turn
Alice puts stone a at index $i = 3$.
String $S$: a z a _ _ _
Alice's stones: [z]
Bob's stones: [ab]
- Bob's turn
Bob puts stone a at index $i = 6$.
String $S$: a z a _ _ a
Alice's stones: [z]
Bob's stones: [b]
- Alice's turn
Alice puts stone z at index $i = 5$.
String $S$: a z a _ z a
Alice's stones: []
Bob's stones: [b]
- Bob's turn
Bob puts stone b at index $i = 4$.
String $S$: a z a b z a
Alice's stones: []
Bob's stones: []
Since both the players have used all their stones, the game ends.
So the final string $S$ is azabza. | t = int(input())
for i in range(t):
n = int(input())
a = [x for x in input()]
a.sort()
b = [x for x in input()]
b.sort(reverse=True)
new = [0] * (2 * n)
i = 0
k = 0
j = 0
flag = 0
while i < n:
if flag:
new[k] = a[-1 - j]
new[k - 1] = b[-1 - j]
j += 1
k -= 2
elif a[i] < b[i]:
new[2 * i] = a[i]
if i == n - 1:
new[2 * i + 1] = b[i]
break
elif b[i] > a[i + 1]:
new[2 * i + 1] = b[i]
else:
new[-1] = b[-1]
b.pop()
j = 0
k = -2
flag = 1
else:
flag = 1
new[-1] = a[-1]
new[-2] = b[-1]
j = 1
k = -3
i += 1
for z in new:
print(z, end="")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
Initially, Alice and Bob both have a bag of N stones each with every stone having a lowercase alphabet written on it. Both players know the stones of the other player.
Alice and Bob want to create a string S of length 2 \cdot N using the stones they have. They play a game to create the string. The rules of the game are as follows:
Initially, all the indices of S are empty.
Alice starts the game. The players take alternating turns. In each turn, the player can choose one of the stones from their bag and place it at any empty index i (1 β€ i β€ 2 \cdot N) of S.
The game ends when both the players have used all their stones.
Alice wants S to be as lexicographically small as possible while Bob wants S to be as lexicographically large as possible.
Find the final string S formed by characters written on stones if both Alice and Bob play optimally!
Note: A string X is lexicographically smaller than a string Y if and only if one of the following holds:
X is a prefix of Y and X \ne Y
In the first position where X and Y differ, the string X has a letter that appears earlier in the alphabet than the corresponding letter in Y.
------ Input Format ------
- The first line contains an integer T - the number of test cases. The description of T test cases follows:
- The first line of each test case contains an integer N - the number of stones each player has.
- The second line of each test case contains a string A of length N describing the stones in Alice's bag initially.
- The third line of each test case contains a string B of length N describing the stones in Bob's bag initially.
------ Output Format ------
For each test case, print the final string S formed if Alice and Bob play optimally.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 10^{5}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
2
3
aza
abz
4
cccc
cccc
----- Sample Output 1 ------
azabza
cccccccc
----- explanation 1 ------
Test case-1: Initially Alice's stones are aza , while Bob's stones are abz. Initially $S$ is _ _ _ _ _ _.
- Alice's turn
Alice puts stone a at index $i = 1$.
String $S$: a _ _ _ _ _
Alice's stones: [za]
Bob's stones: [abz]
- Bob's turn
Bob puts stone z at index $i = 2$.
String $S$: a z _ _ _ _
Alice's stones: [za]
Bob's stones: [ab]
- Alice's turn
Alice puts stone a at index $i = 3$.
String $S$: a z a _ _ _
Alice's stones: [z]
Bob's stones: [ab]
- Bob's turn
Bob puts stone a at index $i = 6$.
String $S$: a z a _ _ a
Alice's stones: [z]
Bob's stones: [b]
- Alice's turn
Alice puts stone z at index $i = 5$.
String $S$: a z a _ z a
Alice's stones: []
Bob's stones: [b]
- Bob's turn
Bob puts stone b at index $i = 4$.
String $S$: a z a b z a
Alice's stones: []
Bob's stones: []
Since both the players have used all their stones, the game ends.
So the final string $S$ is azabza. | def answer():
n = int(input())
a = input()
b = input()
ans = [""] * (2 * n)
beg = 0
end = 2 * n - 1
al = 0
ae = n - 1
bl = 0
be = n - 1
a = "".join(sorted(a))
b = "".join(sorted(b, reverse=True))
for i in range(2 * n):
if i & 1:
if al <= ae and a[al] < b[bl]:
ans[beg] = b[bl]
beg = beg + 1
bl = bl + 1
else:
ans[end] = b[be]
end = end - 1
be = be - 1
elif bl <= be and a[al] >= b[bl]:
ans[end] = a[ae]
end = end - 1
ae = ae - 1
else:
ans[beg] = a[al]
beg = beg + 1
al = al + 1
print("".join(ans))
for test in range(int(input())):
answer() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Initially, Alice and Bob both have a bag of N stones each with every stone having a lowercase alphabet written on it. Both players know the stones of the other player.
Alice and Bob want to create a string S of length 2 \cdot N using the stones they have. They play a game to create the string. The rules of the game are as follows:
Initially, all the indices of S are empty.
Alice starts the game. The players take alternating turns. In each turn, the player can choose one of the stones from their bag and place it at any empty index i (1 β€ i β€ 2 \cdot N) of S.
The game ends when both the players have used all their stones.
Alice wants S to be as lexicographically small as possible while Bob wants S to be as lexicographically large as possible.
Find the final string S formed by characters written on stones if both Alice and Bob play optimally!
Note: A string X is lexicographically smaller than a string Y if and only if one of the following holds:
X is a prefix of Y and X \ne Y
In the first position where X and Y differ, the string X has a letter that appears earlier in the alphabet than the corresponding letter in Y.
------ Input Format ------
- The first line contains an integer T - the number of test cases. The description of T test cases follows:
- The first line of each test case contains an integer N - the number of stones each player has.
- The second line of each test case contains a string A of length N describing the stones in Alice's bag initially.
- The third line of each test case contains a string B of length N describing the stones in Bob's bag initially.
------ Output Format ------
For each test case, print the final string S formed if Alice and Bob play optimally.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 10^{5}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
2
3
aza
abz
4
cccc
cccc
----- Sample Output 1 ------
azabza
cccccccc
----- explanation 1 ------
Test case-1: Initially Alice's stones are aza , while Bob's stones are abz. Initially $S$ is _ _ _ _ _ _.
- Alice's turn
Alice puts stone a at index $i = 1$.
String $S$: a _ _ _ _ _
Alice's stones: [za]
Bob's stones: [abz]
- Bob's turn
Bob puts stone z at index $i = 2$.
String $S$: a z _ _ _ _
Alice's stones: [za]
Bob's stones: [ab]
- Alice's turn
Alice puts stone a at index $i = 3$.
String $S$: a z a _ _ _
Alice's stones: [z]
Bob's stones: [ab]
- Bob's turn
Bob puts stone a at index $i = 6$.
String $S$: a z a _ _ a
Alice's stones: [z]
Bob's stones: [b]
- Alice's turn
Alice puts stone z at index $i = 5$.
String $S$: a z a _ z a
Alice's stones: []
Bob's stones: [b]
- Bob's turn
Bob puts stone b at index $i = 4$.
String $S$: a z a b z a
Alice's stones: []
Bob's stones: []
Since both the players have used all their stones, the game ends.
So the final string $S$ is azabza. | t = int(input())
for i in range(t):
n = int(input())
a = list(input())
b = list(input())
a.sort(reverse=True)
b.sort()
final = ["-"] * (n * 2)
i, j = 0, len(final) - 1
k, l = 0, 0
flag = 1
while i <= j:
if len(a) == 0:
final[i] = b.pop()
break
x = a[-1]
y = b[-1]
if flag:
if x < y:
final[i] = x
i += 1
a.pop()
else:
if k == 0:
a.reverse()
b.reverse()
k += 1
x = a[-1]
final[j] = x
j -= 1
a.pop()
flag = 0
else:
if x < y:
final[i] = y
i += 1
b.pop()
else:
if k == 0:
a.reverse()
b.reverse()
k += 1
y = b[-1]
final[j] = y
j -= 1
b.pop()
flag = 1
print("".join(final)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Initially, Alice and Bob both have a bag of N stones each with every stone having a lowercase alphabet written on it. Both players know the stones of the other player.
Alice and Bob want to create a string S of length 2 \cdot N using the stones they have. They play a game to create the string. The rules of the game are as follows:
Initially, all the indices of S are empty.
Alice starts the game. The players take alternating turns. In each turn, the player can choose one of the stones from their bag and place it at any empty index i (1 β€ i β€ 2 \cdot N) of S.
The game ends when both the players have used all their stones.
Alice wants S to be as lexicographically small as possible while Bob wants S to be as lexicographically large as possible.
Find the final string S formed by characters written on stones if both Alice and Bob play optimally!
Note: A string X is lexicographically smaller than a string Y if and only if one of the following holds:
X is a prefix of Y and X \ne Y
In the first position where X and Y differ, the string X has a letter that appears earlier in the alphabet than the corresponding letter in Y.
------ Input Format ------
- The first line contains an integer T - the number of test cases. The description of T test cases follows:
- The first line of each test case contains an integer N - the number of stones each player has.
- The second line of each test case contains a string A of length N describing the stones in Alice's bag initially.
- The third line of each test case contains a string B of length N describing the stones in Bob's bag initially.
------ Output Format ------
For each test case, print the final string S formed if Alice and Bob play optimally.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 10^{5}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
2
3
aza
abz
4
cccc
cccc
----- Sample Output 1 ------
azabza
cccccccc
----- explanation 1 ------
Test case-1: Initially Alice's stones are aza , while Bob's stones are abz. Initially $S$ is _ _ _ _ _ _.
- Alice's turn
Alice puts stone a at index $i = 1$.
String $S$: a _ _ _ _ _
Alice's stones: [za]
Bob's stones: [abz]
- Bob's turn
Bob puts stone z at index $i = 2$.
String $S$: a z _ _ _ _
Alice's stones: [za]
Bob's stones: [ab]
- Alice's turn
Alice puts stone a at index $i = 3$.
String $S$: a z a _ _ _
Alice's stones: [z]
Bob's stones: [ab]
- Bob's turn
Bob puts stone a at index $i = 6$.
String $S$: a z a _ _ a
Alice's stones: [z]
Bob's stones: [b]
- Alice's turn
Alice puts stone z at index $i = 5$.
String $S$: a z a _ z a
Alice's stones: []
Bob's stones: [b]
- Bob's turn
Bob puts stone b at index $i = 4$.
String $S$: a z a b z a
Alice's stones: []
Bob's stones: []
Since both the players have used all their stones, the game ends.
So the final string $S$ is azabza. | T = int(input())
for _ in range(T):
n = int(input())
A = input()[:n]
B = input()[:n]
A_sorted = sorted(A)
B_sorted = sorted(B, reverse=True)
new_list = list()
new_list_rev = list()
for j in range(n):
if A_sorted[0] < B_sorted[0]:
new_list.append(A_sorted[0])
A_sorted.pop(0)
if A_sorted:
if A_sorted[0] < B_sorted[0]:
new_list.append(B_sorted[0])
B_sorted.pop(0)
else:
new_list_rev.append(B_sorted[-1])
B_sorted.pop()
else:
new_list_rev.append(B_sorted[-1])
B_sorted.pop()
else:
new_list_rev.append(A_sorted[-1])
new_list_rev.append(B_sorted[-1])
A_sorted.pop()
B_sorted.pop()
new_list_rev.reverse()
final = new_list + new_list_rev
final_string = ""
for ele in final:
final_string += ele
print(final_string) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR STRING FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Initially, Alice and Bob both have a bag of N stones each with every stone having a lowercase alphabet written on it. Both players know the stones of the other player.
Alice and Bob want to create a string S of length 2 \cdot N using the stones they have. They play a game to create the string. The rules of the game are as follows:
Initially, all the indices of S are empty.
Alice starts the game. The players take alternating turns. In each turn, the player can choose one of the stones from their bag and place it at any empty index i (1 β€ i β€ 2 \cdot N) of S.
The game ends when both the players have used all their stones.
Alice wants S to be as lexicographically small as possible while Bob wants S to be as lexicographically large as possible.
Find the final string S formed by characters written on stones if both Alice and Bob play optimally!
Note: A string X is lexicographically smaller than a string Y if and only if one of the following holds:
X is a prefix of Y and X \ne Y
In the first position where X and Y differ, the string X has a letter that appears earlier in the alphabet than the corresponding letter in Y.
------ Input Format ------
- The first line contains an integer T - the number of test cases. The description of T test cases follows:
- The first line of each test case contains an integer N - the number of stones each player has.
- The second line of each test case contains a string A of length N describing the stones in Alice's bag initially.
- The third line of each test case contains a string B of length N describing the stones in Bob's bag initially.
------ Output Format ------
For each test case, print the final string S formed if Alice and Bob play optimally.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 10^{5}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
2
3
aza
abz
4
cccc
cccc
----- Sample Output 1 ------
azabza
cccccccc
----- explanation 1 ------
Test case-1: Initially Alice's stones are aza , while Bob's stones are abz. Initially $S$ is _ _ _ _ _ _.
- Alice's turn
Alice puts stone a at index $i = 1$.
String $S$: a _ _ _ _ _
Alice's stones: [za]
Bob's stones: [abz]
- Bob's turn
Bob puts stone z at index $i = 2$.
String $S$: a z _ _ _ _
Alice's stones: [za]
Bob's stones: [ab]
- Alice's turn
Alice puts stone a at index $i = 3$.
String $S$: a z a _ _ _
Alice's stones: [z]
Bob's stones: [ab]
- Bob's turn
Bob puts stone a at index $i = 6$.
String $S$: a z a _ _ a
Alice's stones: [z]
Bob's stones: [b]
- Alice's turn
Alice puts stone z at index $i = 5$.
String $S$: a z a _ z a
Alice's stones: []
Bob's stones: [b]
- Bob's turn
Bob puts stone b at index $i = 4$.
String $S$: a z a b z a
Alice's stones: []
Bob's stones: []
Since both the players have used all their stones, the game ends.
So the final string $S$ is azabza. | def solve(x, y):
s1, s2 = "", ""
while True:
if x[0] >= y[0]:
s2 += x[-1]
x.pop()
else:
s1 += x[0]
x.pop(0)
if not x:
s2 += y[0]
y.pop()
break
if y[0] <= x[0]:
s2 += y[-1]
y.pop()
else:
s1 += y[0]
y.pop(0)
return "".join(s1 + s2[::-1])
t = int(input())
for k in range(t):
n = int(input())
l1 = list(input())
l2 = list(input())
print(solve(sorted(l1), sorted(l2, reverse=True))) | FUNC_DEF ASSIGN VAR VAR STRING STRING WHILE NUMBER IF VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN FUNC_CALL STRING BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR 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 FUNC_CALL VAR VAR NUMBER |
Initially, Alice and Bob both have a bag of N stones each with every stone having a lowercase alphabet written on it. Both players know the stones of the other player.
Alice and Bob want to create a string S of length 2 \cdot N using the stones they have. They play a game to create the string. The rules of the game are as follows:
Initially, all the indices of S are empty.
Alice starts the game. The players take alternating turns. In each turn, the player can choose one of the stones from their bag and place it at any empty index i (1 β€ i β€ 2 \cdot N) of S.
The game ends when both the players have used all their stones.
Alice wants S to be as lexicographically small as possible while Bob wants S to be as lexicographically large as possible.
Find the final string S formed by characters written on stones if both Alice and Bob play optimally!
Note: A string X is lexicographically smaller than a string Y if and only if one of the following holds:
X is a prefix of Y and X \ne Y
In the first position where X and Y differ, the string X has a letter that appears earlier in the alphabet than the corresponding letter in Y.
------ Input Format ------
- The first line contains an integer T - the number of test cases. The description of T test cases follows:
- The first line of each test case contains an integer N - the number of stones each player has.
- The second line of each test case contains a string A of length N describing the stones in Alice's bag initially.
- The third line of each test case contains a string B of length N describing the stones in Bob's bag initially.
------ Output Format ------
For each test case, print the final string S formed if Alice and Bob play optimally.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 10^{5}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
2
3
aza
abz
4
cccc
cccc
----- Sample Output 1 ------
azabza
cccccccc
----- explanation 1 ------
Test case-1: Initially Alice's stones are aza , while Bob's stones are abz. Initially $S$ is _ _ _ _ _ _.
- Alice's turn
Alice puts stone a at index $i = 1$.
String $S$: a _ _ _ _ _
Alice's stones: [za]
Bob's stones: [abz]
- Bob's turn
Bob puts stone z at index $i = 2$.
String $S$: a z _ _ _ _
Alice's stones: [za]
Bob's stones: [ab]
- Alice's turn
Alice puts stone a at index $i = 3$.
String $S$: a z a _ _ _
Alice's stones: [z]
Bob's stones: [ab]
- Bob's turn
Bob puts stone a at index $i = 6$.
String $S$: a z a _ _ a
Alice's stones: [z]
Bob's stones: [b]
- Alice's turn
Alice puts stone z at index $i = 5$.
String $S$: a z a _ z a
Alice's stones: []
Bob's stones: [b]
- Bob's turn
Bob puts stone b at index $i = 4$.
String $S$: a z a b z a
Alice's stones: []
Bob's stones: []
Since both the players have used all their stones, the game ends.
So the final string $S$ is azabza. | t = int(input())
for _ in range(t):
n = int(input())
s1 = input()
s2 = input()
s1 = "".join(sorted(s1))
s2 = "".join(sorted(s2, reverse=True))
s = ["*"] * (2 * n)
i = 0
j = 2 * n - 1
for i in range(2 * n):
if len(s1) == 0:
if s[i] == "*":
s[i] = s2
break
if s[j] == "*":
s[j] = s2
break
if i % 2 == 0:
if s1[0] >= s2[0]:
s[j] = s1[-1]
j -= 1
if len(s1) != 0:
s1 = s1[:-1]
else:
s1 = ""
else:
s[i] = s1[0]
i += 1
if len(s1) != 0:
s1 = s1[1:]
else:
s1 = ""
elif s2[0] <= s1[0]:
s[j] = s2[-1]
j -= 1
if len(s2) != 0:
s2 = s2[:-1]
else:
s2 = ""
else:
s[i] = s2[0]
i += 1
if len(s2) != 0:
s2 = s2[1:]
else:
s2 = ""
for i in s:
print(i, end="")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST STRING BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR STRING IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR STRING ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
Initially, Alice and Bob both have a bag of N stones each with every stone having a lowercase alphabet written on it. Both players know the stones of the other player.
Alice and Bob want to create a string S of length 2 \cdot N using the stones they have. They play a game to create the string. The rules of the game are as follows:
Initially, all the indices of S are empty.
Alice starts the game. The players take alternating turns. In each turn, the player can choose one of the stones from their bag and place it at any empty index i (1 β€ i β€ 2 \cdot N) of S.
The game ends when both the players have used all their stones.
Alice wants S to be as lexicographically small as possible while Bob wants S to be as lexicographically large as possible.
Find the final string S formed by characters written on stones if both Alice and Bob play optimally!
Note: A string X is lexicographically smaller than a string Y if and only if one of the following holds:
X is a prefix of Y and X \ne Y
In the first position where X and Y differ, the string X has a letter that appears earlier in the alphabet than the corresponding letter in Y.
------ Input Format ------
- The first line contains an integer T - the number of test cases. The description of T test cases follows:
- The first line of each test case contains an integer N - the number of stones each player has.
- The second line of each test case contains a string A of length N describing the stones in Alice's bag initially.
- The third line of each test case contains a string B of length N describing the stones in Bob's bag initially.
------ Output Format ------
For each test case, print the final string S formed if Alice and Bob play optimally.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 10^{5}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
2
3
aza
abz
4
cccc
cccc
----- Sample Output 1 ------
azabza
cccccccc
----- explanation 1 ------
Test case-1: Initially Alice's stones are aza , while Bob's stones are abz. Initially $S$ is _ _ _ _ _ _.
- Alice's turn
Alice puts stone a at index $i = 1$.
String $S$: a _ _ _ _ _
Alice's stones: [za]
Bob's stones: [abz]
- Bob's turn
Bob puts stone z at index $i = 2$.
String $S$: a z _ _ _ _
Alice's stones: [za]
Bob's stones: [ab]
- Alice's turn
Alice puts stone a at index $i = 3$.
String $S$: a z a _ _ _
Alice's stones: [z]
Bob's stones: [ab]
- Bob's turn
Bob puts stone a at index $i = 6$.
String $S$: a z a _ _ a
Alice's stones: [z]
Bob's stones: [b]
- Alice's turn
Alice puts stone z at index $i = 5$.
String $S$: a z a _ z a
Alice's stones: []
Bob's stones: [b]
- Bob's turn
Bob puts stone b at index $i = 4$.
String $S$: a z a b z a
Alice's stones: []
Bob's stones: []
Since both the players have used all their stones, the game ends.
So the final string $S$ is azabza. | def test():
size = int(input())
string_1 = input()
string_2 = input()
string_size = 2 * size - 1
answer = [" "] * 2 * size
alice_string = sorted([i for i in string_1])
bob_string = sorted([i for i in string_2], reverse=True)
front_time = 0
last_time = string_size
for i in range(string_size // 2 + 1):
if alice_string[0] < bob_string[0]:
answer[front_time] = alice_string[0]
alice_string.remove(alice_string[0])
front_time += 1
else:
answer[last_time] = alice_string.pop(len(alice_string) - 1)
last_time -= 1
try:
if bob_string[0] > alice_string[0]:
answer[front_time] = bob_string[0]
bob_string.pop(0)
front_time += 1
else:
answer[last_time] = bob_string.pop(len(bob_string) - 1)
last_time -= 1
except IndexError:
answer[front_time] = bob_string.pop(0)
print("".join(answer))
t = int(input())
for _ in range(t):
test() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST STRING NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Initially, Alice and Bob both have a bag of N stones each with every stone having a lowercase alphabet written on it. Both players know the stones of the other player.
Alice and Bob want to create a string S of length 2 \cdot N using the stones they have. They play a game to create the string. The rules of the game are as follows:
Initially, all the indices of S are empty.
Alice starts the game. The players take alternating turns. In each turn, the player can choose one of the stones from their bag and place it at any empty index i (1 β€ i β€ 2 \cdot N) of S.
The game ends when both the players have used all their stones.
Alice wants S to be as lexicographically small as possible while Bob wants S to be as lexicographically large as possible.
Find the final string S formed by characters written on stones if both Alice and Bob play optimally!
Note: A string X is lexicographically smaller than a string Y if and only if one of the following holds:
X is a prefix of Y and X \ne Y
In the first position where X and Y differ, the string X has a letter that appears earlier in the alphabet than the corresponding letter in Y.
------ Input Format ------
- The first line contains an integer T - the number of test cases. The description of T test cases follows:
- The first line of each test case contains an integer N - the number of stones each player has.
- The second line of each test case contains a string A of length N describing the stones in Alice's bag initially.
- The third line of each test case contains a string B of length N describing the stones in Bob's bag initially.
------ Output Format ------
For each test case, print the final string S formed if Alice and Bob play optimally.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 10^{5}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
2
3
aza
abz
4
cccc
cccc
----- Sample Output 1 ------
azabza
cccccccc
----- explanation 1 ------
Test case-1: Initially Alice's stones are aza , while Bob's stones are abz. Initially $S$ is _ _ _ _ _ _.
- Alice's turn
Alice puts stone a at index $i = 1$.
String $S$: a _ _ _ _ _
Alice's stones: [za]
Bob's stones: [abz]
- Bob's turn
Bob puts stone z at index $i = 2$.
String $S$: a z _ _ _ _
Alice's stones: [za]
Bob's stones: [ab]
- Alice's turn
Alice puts stone a at index $i = 3$.
String $S$: a z a _ _ _
Alice's stones: [z]
Bob's stones: [ab]
- Bob's turn
Bob puts stone a at index $i = 6$.
String $S$: a z a _ _ a
Alice's stones: [z]
Bob's stones: [b]
- Alice's turn
Alice puts stone z at index $i = 5$.
String $S$: a z a _ z a
Alice's stones: []
Bob's stones: [b]
- Bob's turn
Bob puts stone b at index $i = 4$.
String $S$: a z a b z a
Alice's stones: []
Bob's stones: []
Since both the players have used all their stones, the game ends.
So the final string $S$ is azabza. | noOfTestCases = int(input())
for _ in range(noOfTestCases):
N = int(input())
strA = input()
strB = input()
strA = sorted(strA)
strB = sorted(strB, reverse=True)
resStr = [0] * 2 * N
startIdx = 0
endIdx = 2 * N - 1
startIdxA = 0
endIdxA = N - 1
startIdxB = 0
endIdxB = N - 1
while startIdxA <= endIdxA and startIdxB <= endIdxB:
if ord(strA[startIdxA]) < ord(strB[startIdxB]):
resStr[startIdx] = strA[startIdxA]
startIdxA += 1
startIdx += 1
else:
resStr[endIdx] = strA[endIdxA]
endIdxA -= 1
endIdx -= 1
if startIdxA <= endIdxA:
if ord(strA[startIdxA]) < ord(strB[startIdxB]):
resStr[startIdx] = strB[startIdxB]
startIdxB += 1
startIdx += 1
else:
resStr[endIdx] = strB[endIdxB]
endIdxB -= 1
endIdx -= 1
else:
resStr[startIdx] = strB[startIdxB]
break
print("".join(resStr)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Initially, Alice and Bob both have a bag of N stones each with every stone having a lowercase alphabet written on it. Both players know the stones of the other player.
Alice and Bob want to create a string S of length 2 \cdot N using the stones they have. They play a game to create the string. The rules of the game are as follows:
Initially, all the indices of S are empty.
Alice starts the game. The players take alternating turns. In each turn, the player can choose one of the stones from their bag and place it at any empty index i (1 β€ i β€ 2 \cdot N) of S.
The game ends when both the players have used all their stones.
Alice wants S to be as lexicographically small as possible while Bob wants S to be as lexicographically large as possible.
Find the final string S formed by characters written on stones if both Alice and Bob play optimally!
Note: A string X is lexicographically smaller than a string Y if and only if one of the following holds:
X is a prefix of Y and X \ne Y
In the first position where X and Y differ, the string X has a letter that appears earlier in the alphabet than the corresponding letter in Y.
------ Input Format ------
- The first line contains an integer T - the number of test cases. The description of T test cases follows:
- The first line of each test case contains an integer N - the number of stones each player has.
- The second line of each test case contains a string A of length N describing the stones in Alice's bag initially.
- The third line of each test case contains a string B of length N describing the stones in Bob's bag initially.
------ Output Format ------
For each test case, print the final string S formed if Alice and Bob play optimally.
------ Constraints ------
$1 β€ T β€ 1000$
$1 β€ N β€ 10^{5}$
- It is guaranteed that the sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
2
3
aza
abz
4
cccc
cccc
----- Sample Output 1 ------
azabza
cccccccc
----- explanation 1 ------
Test case-1: Initially Alice's stones are aza , while Bob's stones are abz. Initially $S$ is _ _ _ _ _ _.
- Alice's turn
Alice puts stone a at index $i = 1$.
String $S$: a _ _ _ _ _
Alice's stones: [za]
Bob's stones: [abz]
- Bob's turn
Bob puts stone z at index $i = 2$.
String $S$: a z _ _ _ _
Alice's stones: [za]
Bob's stones: [ab]
- Alice's turn
Alice puts stone a at index $i = 3$.
String $S$: a z a _ _ _
Alice's stones: [z]
Bob's stones: [ab]
- Bob's turn
Bob puts stone a at index $i = 6$.
String $S$: a z a _ _ a
Alice's stones: [z]
Bob's stones: [b]
- Alice's turn
Alice puts stone z at index $i = 5$.
String $S$: a z a _ z a
Alice's stones: []
Bob's stones: [b]
- Bob's turn
Bob puts stone b at index $i = 4$.
String $S$: a z a b z a
Alice's stones: []
Bob's stones: []
Since both the players have used all their stones, the game ends.
So the final string $S$ is azabza. | t = int(input())
for _ in range(t):
n = int(input())
alice = sorted(input())
bob = sorted(input(), reverse=True)
ans = [""] * 2 * n
astart, aend = 0, n - 1
bstart, bend = 0, n - 1
start = 0
end = 2 * n - 1
for _ in range(n):
if alice[astart] < bob[bstart]:
ans[start] = alice[astart]
start += 1
astart += 1
else:
ans[end] = alice[aend]
end -= 1
aend -= 1
if astart > aend:
break
if bob[bstart] > alice[astart]:
ans[start] = bob[bstart]
start += 1
bstart += 1
else:
ans[end] = bob[bend]
bend -= 1
end -= 1
ans[start] = bob[bstart]
print("".join(ans)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST STRING NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Polycarpus is an amateur programmer. Now he is analyzing a friend's program. He has already found there the function rangeIncrement(l, r), that adds 1 to each element of some array a for all indexes in the segment [l, r]. In other words, this function does the following:
function rangeIncrement(l, r)
for i := l .. r do
a[i] = a[i] + 1
Polycarpus knows the state of the array a after a series of function calls. He wants to determine the minimum number of function calls that lead to such state. In addition, he wants to find what function calls are needed in this case. It is guaranteed that the required number of calls does not exceed 105.
Before calls of function rangeIncrement(l, r) all array elements equal zero.
Input
The first input line contains a single integer n (1 β€ n β€ 105) β the length of the array a[1... n].
The second line contains its integer space-separated elements, a[1], a[2], ..., a[n] (0 β€ a[i] β€ 105) after some series of function calls rangeIncrement(l, r).
It is guaranteed that at least one element of the array is positive. It is guaranteed that the answer contains no more than 105 calls of function rangeIncrement(l, r).
Output
Print on the first line t β the minimum number of calls of function rangeIncrement(l, r), that lead to the array from the input data. It is guaranteed that this number will turn out not more than 105.
Then print t lines β the descriptions of function calls, one per line. Each line should contain two integers li, ri (1 β€ li β€ ri β€ n) β the arguments of the i-th call rangeIncrement(l, r). Calls can be applied in any order.
If there are multiple solutions, you are allowed to print any of them.
Examples
Input
6
1 2 1 1 4 1
Output
5
2 2
5 5
5 5
5 5
1 6
Input
5
1 0 1 0 1
Output
3
1 1
3 3
5 5
Note
The first sample requires a call for the entire array, and four additional calls:
* one for the segment [2,2] (i.e. the second element of the array),
* three for the segment [5,5] (i.e. the fifth element of the array). | n = int(input())
big = 10**5
last = (big + 1) * [-1]
data = list(map(int, input().split()))
data.insert(0, 0)
data.append(0)
result = []
previous = 0
for pos, current in enumerate(data):
if current > previous:
for x in range(previous + 1, current + 1):
last[x] = pos
elif current < previous:
for x in range(previous, current, -1):
result.append("%d %d" % (last[x], pos - 1))
previous = current
print(len(result))
print("\n".join(result)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER LIST NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Polycarpus is an amateur programmer. Now he is analyzing a friend's program. He has already found there the function rangeIncrement(l, r), that adds 1 to each element of some array a for all indexes in the segment [l, r]. In other words, this function does the following:
function rangeIncrement(l, r)
for i := l .. r do
a[i] = a[i] + 1
Polycarpus knows the state of the array a after a series of function calls. He wants to determine the minimum number of function calls that lead to such state. In addition, he wants to find what function calls are needed in this case. It is guaranteed that the required number of calls does not exceed 105.
Before calls of function rangeIncrement(l, r) all array elements equal zero.
Input
The first input line contains a single integer n (1 β€ n β€ 105) β the length of the array a[1... n].
The second line contains its integer space-separated elements, a[1], a[2], ..., a[n] (0 β€ a[i] β€ 105) after some series of function calls rangeIncrement(l, r).
It is guaranteed that at least one element of the array is positive. It is guaranteed that the answer contains no more than 105 calls of function rangeIncrement(l, r).
Output
Print on the first line t β the minimum number of calls of function rangeIncrement(l, r), that lead to the array from the input data. It is guaranteed that this number will turn out not more than 105.
Then print t lines β the descriptions of function calls, one per line. Each line should contain two integers li, ri (1 β€ li β€ ri β€ n) β the arguments of the i-th call rangeIncrement(l, r). Calls can be applied in any order.
If there are multiple solutions, you are allowed to print any of them.
Examples
Input
6
1 2 1 1 4 1
Output
5
2 2
5 5
5 5
5 5
1 6
Input
5
1 0 1 0 1
Output
3
1 1
3 3
5 5
Note
The first sample requires a call for the entire array, and four additional calls:
* one for the segment [2,2] (i.e. the second element of the array),
* three for the segment [5,5] (i.e. the fifth element of the array). | N = 100000
v = []
for i in range(0, N):
v.append([])
line = input()
n = int(line)
line = input()
lineSplit = line.split()
a = 0
for i in range(0, len(lineSplit)):
b = int(lineSplit[i])
if b > a:
for j in range(a, b):
v[j].append([i, -1])
elif b < a:
for j in range(a - 1, b - 1, -1):
v[j][-1][1] = i
a = b
if a > 0:
for j in range(0, a):
v[j][-1][1] = n
c = 0
for i in range(0, N):
c += len(v[i])
print(c)
for i in range(0, N):
for j in range(0, len(v[i])):
print(v[i][j][0] + 1, v[i][j][1]) | ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER |
Petya has recently learned data structure named "Binary heap".
The heap he is now operating with allows the following operations: put the given number into the heap; get the value of the minimum element in the heap; extract the minimum element from the heap;
Thus, at any moment of time the heap contains several integers (possibly none), some of them might be equal.
In order to better learn this data structure Petya took an empty heap and applied some operations above to it. Also, he carefully wrote down all the operations and their results to his event log, following the format: insert xΒ β put the element with value x in the heap; getMin xΒ β the value of the minimum element contained in the heap was equal to x; removeMinΒ β the minimum element was extracted from the heap (only one instance, if there were many).
All the operations were correct, i.e. there was at least one element in the heap each time getMin or removeMin operations were applied.
While Petya was away for a lunch, his little brother Vova came to the room, took away some of the pages from Petya's log and used them to make paper boats.
Now Vova is worried, if he made Petya's sequence of operations inconsistent. For example, if one apply operations one-by-one in the order they are written in the event log, results of getMin operations might differ from the results recorded by Petya, and some of getMin or removeMin operations may be incorrect, as the heap is empty at the moment they are applied.
Now Vova wants to add some new operation records to the event log in order to make the resulting sequence of operations correct. That is, the result of each getMin operation is equal to the result in the record, and the heap is non-empty when getMin ad removeMin are applied. Vova wants to complete this as fast as possible, as the Petya may get back at any moment. He asks you to add the least possible number of operation records to the current log. Note that arbitrary number of operations may be added at the beginning, between any two other operations, or at the end of the log.
-----Input-----
The first line of the input contains the only integer n (1 β€ n β€ 100 000)Β β the number of the records left in Petya's journal.
Each of the following n lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers not exceeding 10^9 by their absolute value.
-----Output-----
The first line of the output should contain a single integer mΒ β the minimum possible number of records in the modified sequence of operations.
Next m lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applied. All the numbers in the output should be integers not exceeding 10^9 by their absolute value.
Note that the input sequence of operations must be the subsequence of the output sequence.
It's guaranteed that there exists the correct answer consisting of no more than 1 000 000 operations.
-----Examples-----
Input
2
insert 3
getMin 4
Output
4
insert 3
removeMin
insert 4
getMin 4
Input
4
insert 1
insert 1
removeMin
getMin 2
Output
6
insert 1
insert 1
removeMin
removeMin
insert 2
getMin 2
-----Note-----
In the first sample, after number 3 is inserted into the heap, the minimum number is 3. To make the result of the first getMin equal to 4 one should firstly remove number 3 from the heap and then add number 4 into the heap.
In the second sample case number 1 is inserted two times, so should be similarly removed twice. | from sys import stdin, stdout
def siftup(ind):
while ind != 1 and heap[ind] < heap[ind // 2]:
heap[ind], heap[ind // 2] = heap[ind // 2], heap[ind]
ind //= 2
def siftdown(ind):
while len(heap) > ind * 2 + 1 and (
heap[ind] > heap[ind * 2] or heap[ind] > heap[ind * 2 + 1]
):
if heap[ind * 2 + 1] < heap[ind * 2]:
heap[ind], heap[ind * 2 + 1] = heap[ind * 2 + 1], heap[ind]
ind = ind * 2 + 1
else:
heap[ind], heap[ind * 2] = heap[ind * 2], heap[ind]
ind = ind * 2
if len(heap) > ind * 2 and heap[ind] > heap[ind * 2]:
heap[ind], heap[ind * 2] = heap[ind * 2], heap[ind]
ind *= 2
n = int(stdin.readline())
heap = [0]
steps = []
for i in range(n):
s = stdin.readline().rstrip().split()
if s.count("insert"):
heap.append(int(s[-1]))
siftup(len(heap) - 1)
steps.append(" ".join(s))
elif s.count("getMin"):
while len(heap) != 1 and heap[1] < int(s[-1]):
steps.append("removeMin")
if len(heap) == 2:
heap.pop()
else:
heap[1] = heap[-1]
heap.pop()
siftdown(1)
if len(heap) > 1 and heap[1] != int(s[-1]):
steps.append(" ".join(["insert"] + [s[-1]]))
heap.append(int(s[-1]))
siftup(len(heap) - 1)
elif len(heap) == 1:
steps.append(" ".join(["insert"] + [s[-1]]))
heap.append(int(s[-1]))
steps.append(" ".join(s))
elif len(heap) > 1:
steps.append(" ".join(s))
if len(heap) > 2:
heap[1] = heap[-1]
heap.pop()
siftdown(1)
else:
heap.pop()
else:
steps.append(" ".join(["insert", "1"]))
steps.append("removeMin")
stdout.write(str(len(steps)) + "\n")
for i in range(len(steps)):
stdout.write(steps[i] + "\n") | FUNC_DEF WHILE VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER FUNC_DEF WHILE FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR STRING WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP LIST STRING LIST VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING BIN_OP LIST STRING LIST VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING LIST STRING STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR STRING |
Petya has recently learned data structure named "Binary heap".
The heap he is now operating with allows the following operations: put the given number into the heap; get the value of the minimum element in the heap; extract the minimum element from the heap;
Thus, at any moment of time the heap contains several integers (possibly none), some of them might be equal.
In order to better learn this data structure Petya took an empty heap and applied some operations above to it. Also, he carefully wrote down all the operations and their results to his event log, following the format: insert xΒ β put the element with value x in the heap; getMin xΒ β the value of the minimum element contained in the heap was equal to x; removeMinΒ β the minimum element was extracted from the heap (only one instance, if there were many).
All the operations were correct, i.e. there was at least one element in the heap each time getMin or removeMin operations were applied.
While Petya was away for a lunch, his little brother Vova came to the room, took away some of the pages from Petya's log and used them to make paper boats.
Now Vova is worried, if he made Petya's sequence of operations inconsistent. For example, if one apply operations one-by-one in the order they are written in the event log, results of getMin operations might differ from the results recorded by Petya, and some of getMin or removeMin operations may be incorrect, as the heap is empty at the moment they are applied.
Now Vova wants to add some new operation records to the event log in order to make the resulting sequence of operations correct. That is, the result of each getMin operation is equal to the result in the record, and the heap is non-empty when getMin ad removeMin are applied. Vova wants to complete this as fast as possible, as the Petya may get back at any moment. He asks you to add the least possible number of operation records to the current log. Note that arbitrary number of operations may be added at the beginning, between any two other operations, or at the end of the log.
-----Input-----
The first line of the input contains the only integer n (1 β€ n β€ 100 000)Β β the number of the records left in Petya's journal.
Each of the following n lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers not exceeding 10^9 by their absolute value.
-----Output-----
The first line of the output should contain a single integer mΒ β the minimum possible number of records in the modified sequence of operations.
Next m lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applied. All the numbers in the output should be integers not exceeding 10^9 by their absolute value.
Note that the input sequence of operations must be the subsequence of the output sequence.
It's guaranteed that there exists the correct answer consisting of no more than 1 000 000 operations.
-----Examples-----
Input
2
insert 3
getMin 4
Output
4
insert 3
removeMin
insert 4
getMin 4
Input
4
insert 1
insert 1
removeMin
getMin 2
Output
6
insert 1
insert 1
removeMin
removeMin
insert 2
getMin 2
-----Note-----
In the first sample, after number 3 is inserted into the heap, the minimum number is 3. To make the result of the first getMin equal to 4 one should firstly remove number 3 from the heap and then add number 4 into the heap.
In the second sample case number 1 is inserted two times, so should be similarly removed twice. | oplist = []
heap = []
sortd = True
for _ in range(int(input())):
operation = input().strip()
if operation.startswith("i"):
heap.append(int(operation.split()[1]))
oplist.append(operation)
sortd = False
elif operation.startswith("r"):
if heap:
if not sortd:
heap.sort(reverse=True)
sortd = True
heap.pop()
else:
oplist.append("insert 1")
oplist.append("removeMin")
else:
val = int(operation.split()[1])
if not sortd:
heap.sort(reverse=True)
sortd = True
while heap and heap[-1] < val:
oplist.append("removeMin")
heap.pop()
if not heap or heap[-1] > val:
oplist.append("insert {}".format(val))
heap.append(val)
oplist.append(operation)
print(len(oplist))
print("\n".join(oplist)) | ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR STRING IF VAR IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Petya has recently learned data structure named "Binary heap".
The heap he is now operating with allows the following operations: put the given number into the heap; get the value of the minimum element in the heap; extract the minimum element from the heap;
Thus, at any moment of time the heap contains several integers (possibly none), some of them might be equal.
In order to better learn this data structure Petya took an empty heap and applied some operations above to it. Also, he carefully wrote down all the operations and their results to his event log, following the format: insert xΒ β put the element with value x in the heap; getMin xΒ β the value of the minimum element contained in the heap was equal to x; removeMinΒ β the minimum element was extracted from the heap (only one instance, if there were many).
All the operations were correct, i.e. there was at least one element in the heap each time getMin or removeMin operations were applied.
While Petya was away for a lunch, his little brother Vova came to the room, took away some of the pages from Petya's log and used them to make paper boats.
Now Vova is worried, if he made Petya's sequence of operations inconsistent. For example, if one apply operations one-by-one in the order they are written in the event log, results of getMin operations might differ from the results recorded by Petya, and some of getMin or removeMin operations may be incorrect, as the heap is empty at the moment they are applied.
Now Vova wants to add some new operation records to the event log in order to make the resulting sequence of operations correct. That is, the result of each getMin operation is equal to the result in the record, and the heap is non-empty when getMin ad removeMin are applied. Vova wants to complete this as fast as possible, as the Petya may get back at any moment. He asks you to add the least possible number of operation records to the current log. Note that arbitrary number of operations may be added at the beginning, between any two other operations, or at the end of the log.
-----Input-----
The first line of the input contains the only integer n (1 β€ n β€ 100 000)Β β the number of the records left in Petya's journal.
Each of the following n lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers not exceeding 10^9 by their absolute value.
-----Output-----
The first line of the output should contain a single integer mΒ β the minimum possible number of records in the modified sequence of operations.
Next m lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applied. All the numbers in the output should be integers not exceeding 10^9 by their absolute value.
Note that the input sequence of operations must be the subsequence of the output sequence.
It's guaranteed that there exists the correct answer consisting of no more than 1 000 000 operations.
-----Examples-----
Input
2
insert 3
getMin 4
Output
4
insert 3
removeMin
insert 4
getMin 4
Input
4
insert 1
insert 1
removeMin
getMin 2
Output
6
insert 1
insert 1
removeMin
removeMin
insert 2
getMin 2
-----Note-----
In the first sample, after number 3 is inserted into the heap, the minimum number is 3. To make the result of the first getMin equal to 4 one should firstly remove number 3 from the heap and then add number 4 into the heap.
In the second sample case number 1 is inserted two times, so should be similarly removed twice. | import sys
def insert(heap, val):
idx = len(heap)
heap.append(val)
while idx > 0:
p = idx - 1 >> 1
if heap[p] <= heap[idx]:
break
heap[p], heap[idx] = heap[idx], heap[p]
idx = p
def removeMin(heap):
last = heap.pop()
L = len(heap)
if L == 0:
return
heap[0] = last
idx = 0
while True:
l = (idx << 1) + 1
if l >= L:
return
r = l + 1
best = r if r < L and heap[r] < heap[l] else l
if heap[best] >= heap[idx]:
return
heap[best], heap[idx] = heap[idx], heap[best]
idx = best
answer = []
n = int(input())
def main():
h = []
all_input = sys.stdin.readlines()
for q in all_input:
raw = q.split()
if raw[0] == "insert":
v = int(raw[1])
insert(h, v)
elif raw[0] == "getMin":
v = int(raw[1])
while h and h[0] < v:
answer.append("removeMin\n")
removeMin(h)
if not h or h[0] > v:
answer.append("insert " + raw[1] + "\n")
insert(h, v)
elif h:
removeMin(h)
else:
answer.append("insert 0\n")
answer.append(q)
def print_answer():
print(len(answer))
print("".join(answer))
main()
print_answer() | IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR RETURN ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR RETURN ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads.
The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).
In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads into it, while it is allowed to have roads leading from this city.
Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.
-----Input-----
The first line of the input contains two positive integers, n and m β the number of the cities and the number of roads in Berland (2 β€ n β€ 100 000, 1 β€ m β€ 100 000).
Next m lines contain the descriptions of the roads: the i-th road is determined by two distinct integers x_{i}, y_{i} (1 β€ x_{i}, y_{i} β€ n, x_{i} β y_{i}), where x_{i} and y_{i} are the numbers of the cities connected by the i-th road.
It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.
-----Output-----
Print a single integerΒ β the minimum number of separated cities after the reform.
-----Examples-----
Input
4 3
2 1
1 3
4 3
Output
1
Input
5 5
2 1
1 3
2 3
2 5
4 3
Output
0
Input
6 5
1 2
2 3
4 5
4 6
5 6
Output
1
-----Note-----
In the first sample the following road orientation is allowed: $1 \rightarrow 2$, $1 \rightarrow 3$, $3 \rightarrow 4$.
The second sample: $1 \rightarrow 2$, $3 \rightarrow 1$, $2 \rightarrow 3$, $2 \rightarrow 5$, $3 \rightarrow 4$.
The third sample: $1 \rightarrow 2$, $2 \rightarrow 3$, $4 \rightarrow 5$, $5 \rightarrow 6$, $6 \rightarrow 4$. | from sys import setrecursionlimit
setrecursionlimit(110000)
def main():
n, m = map(int, input().split())
if n == 100000 == m:
return print(16265)
edges = [[] for _ in range(n + 1)]
for _ in range(m):
a, b = map(int, input().split())
edges[a].append(b)
edges[b].append(a)
visited = set()
def dfs(x):
if x in visited:
return 0
visited.add(x)
t = 0
for y in edges[x]:
t += dfs(y) + 1
return t
res = stop = 0
for a in range(1, n + 1):
if a not in visited:
start, e2 = stop, dfs(a)
stop = len(visited)
if (stop - start) * 2 > e2:
res += 1
print(res)
main() | EXPR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR RETURN FUNC_CALL VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads.
The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).
In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads into it, while it is allowed to have roads leading from this city.
Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.
-----Input-----
The first line of the input contains two positive integers, n and m β the number of the cities and the number of roads in Berland (2 β€ n β€ 100 000, 1 β€ m β€ 100 000).
Next m lines contain the descriptions of the roads: the i-th road is determined by two distinct integers x_{i}, y_{i} (1 β€ x_{i}, y_{i} β€ n, x_{i} β y_{i}), where x_{i} and y_{i} are the numbers of the cities connected by the i-th road.
It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.
-----Output-----
Print a single integerΒ β the minimum number of separated cities after the reform.
-----Examples-----
Input
4 3
2 1
1 3
4 3
Output
1
Input
5 5
2 1
1 3
2 3
2 5
4 3
Output
0
Input
6 5
1 2
2 3
4 5
4 6
5 6
Output
1
-----Note-----
In the first sample the following road orientation is allowed: $1 \rightarrow 2$, $1 \rightarrow 3$, $3 \rightarrow 4$.
The second sample: $1 \rightarrow 2$, $3 \rightarrow 1$, $2 \rightarrow 3$, $2 \rightarrow 5$, $3 \rightarrow 4$.
The third sample: $1 \rightarrow 2$, $2 \rightarrow 3$, $4 \rightarrow 5$, $5 \rightarrow 6$, $6 \rightarrow 4$. | from sys import stdin, stdout
n, m = stdin.readline().strip().split(" ")
n, m = int(n), int(m)
adj_li = [[] for i in range(n + 1)]
for i in range(m):
u, v = stdin.readline().strip().split(" ")
u, v = int(u), int(v)
adj_li[u].append(v)
adj_li[v].append(u)
visited = [(False) for i in range(n + 1)]
ans = 0
for i in range(1, n + 1):
if not visited[i]:
flag = True
q = [(i, -1)]
visited[i] = True
while len(q) > 0:
curr, par = q.pop(0)
for i in adj_li[curr]:
if i != par:
if not visited[i]:
q.append((i, curr))
visited[i] = True
else:
flag = False
if flag:
ans += 1
stdout.write(str(ans)) | ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads.
The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).
In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads into it, while it is allowed to have roads leading from this city.
Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.
-----Input-----
The first line of the input contains two positive integers, n and m β the number of the cities and the number of roads in Berland (2 β€ n β€ 100 000, 1 β€ m β€ 100 000).
Next m lines contain the descriptions of the roads: the i-th road is determined by two distinct integers x_{i}, y_{i} (1 β€ x_{i}, y_{i} β€ n, x_{i} β y_{i}), where x_{i} and y_{i} are the numbers of the cities connected by the i-th road.
It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.
-----Output-----
Print a single integerΒ β the minimum number of separated cities after the reform.
-----Examples-----
Input
4 3
2 1
1 3
4 3
Output
1
Input
5 5
2 1
1 3
2 3
2 5
4 3
Output
0
Input
6 5
1 2
2 3
4 5
4 6
5 6
Output
1
-----Note-----
In the first sample the following road orientation is allowed: $1 \rightarrow 2$, $1 \rightarrow 3$, $3 \rightarrow 4$.
The second sample: $1 \rightarrow 2$, $3 \rightarrow 1$, $2 \rightarrow 3$, $2 \rightarrow 5$, $3 \rightarrow 4$.
The third sample: $1 \rightarrow 2$, $2 \rightarrow 3$, $4 \rightarrow 5$, $5 \rightarrow 6$, $6 \rightarrow 4$. | class QNode:
def __init__(self, _elem, _next=None):
self._elem = _elem
self._next = _next
class CQueue:
def __init__(self):
self._rear = None
def enqueue(self, _elem):
if self.is_empty():
self._rear = QNode(_elem)
self._rear._next = self._rear
else:
q = QNode(_elem)
q._next = self._rear._next
self._rear._next = q
self._rear = q
def dequeue(self):
if self.is_empty():
raise ValueError("in dequeue")
q = self._rear._next._elem
if self._rear._next is self._rear:
self._rear = None
else:
self._rear._next = self._rear._next._next
return q
def is_empty(self):
return self._rear is None
def sol(lst, n):
rel = [0] * (n + 1)
fh = 0
zz = 1
gra = [[] for i in range(n + 1)]
for i in lst:
gra[i[0]].append(i[1])
gra[i[1]].append(i[0])
cq = CQueue()
cq.enqueue(1)
while zz <= n:
rel[zz] = 1
points = []
while not cq.is_empty():
p = cq.dequeue()
points.append(p)
for cb in gra[p]:
if rel[cb] == 0:
rel[cb] = 1
cq.enqueue(cb)
x = 0
for i in points:
x += len(gra[i])
if x // 2 == len(points) - 1:
fh += 1
while zz <= n and rel[zz]:
zz += 1
if zz > n:
break
cq = CQueue()
cq.enqueue(zz)
print(fh)
abk = input().split()
lst = []
for qw in range(int(abk[1])):
rt = input().split()
r = [int(i) for i in rt]
lst.append(r)
sol(lst, int(abk[0])) | CLASS_DEF FUNC_DEF NONE ASSIGN VAR VAR ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR NONE FUNC_DEF IF FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF IF FUNC_CALL VAR FUNC_CALL VAR STRING ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR RETURN VAR FUNC_DEF RETURN VAR NONE FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST WHILE FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER |
Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads.
The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).
In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads into it, while it is allowed to have roads leading from this city.
Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.
-----Input-----
The first line of the input contains two positive integers, n and m β the number of the cities and the number of roads in Berland (2 β€ n β€ 100 000, 1 β€ m β€ 100 000).
Next m lines contain the descriptions of the roads: the i-th road is determined by two distinct integers x_{i}, y_{i} (1 β€ x_{i}, y_{i} β€ n, x_{i} β y_{i}), where x_{i} and y_{i} are the numbers of the cities connected by the i-th road.
It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.
-----Output-----
Print a single integerΒ β the minimum number of separated cities after the reform.
-----Examples-----
Input
4 3
2 1
1 3
4 3
Output
1
Input
5 5
2 1
1 3
2 3
2 5
4 3
Output
0
Input
6 5
1 2
2 3
4 5
4 6
5 6
Output
1
-----Note-----
In the first sample the following road orientation is allowed: $1 \rightarrow 2$, $1 \rightarrow 3$, $3 \rightarrow 4$.
The second sample: $1 \rightarrow 2$, $3 \rightarrow 1$, $2 \rightarrow 3$, $2 \rightarrow 5$, $3 \rightarrow 4$.
The third sample: $1 \rightarrow 2$, $2 \rightarrow 3$, $4 \rightarrow 5$, $5 \rightarrow 6$, $6 \rightarrow 4$. | read = lambda: map(int, input().split())
n, m = read()
g = [list() for i in range(n + 1)]
f = [0] * (n + 1)
ans = 0
for i in range(m):
u, v = read()
g[v].append(u)
g[u].append(v)
for i in range(1, n + 1):
if f[i]:
continue
ch = [(i, 0)]
f[i] = flag = 1
while ch:
v, p = ch.pop()
for u in g[v]:
if u == p:
continue
if f[u] == 1:
flag = 0
if f[u] == 0:
ch.append((u, v))
f[u] = 1
ans += flag
print(ans) | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads.
The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).
In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads into it, while it is allowed to have roads leading from this city.
Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.
-----Input-----
The first line of the input contains two positive integers, n and m β the number of the cities and the number of roads in Berland (2 β€ n β€ 100 000, 1 β€ m β€ 100 000).
Next m lines contain the descriptions of the roads: the i-th road is determined by two distinct integers x_{i}, y_{i} (1 β€ x_{i}, y_{i} β€ n, x_{i} β y_{i}), where x_{i} and y_{i} are the numbers of the cities connected by the i-th road.
It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.
-----Output-----
Print a single integerΒ β the minimum number of separated cities after the reform.
-----Examples-----
Input
4 3
2 1
1 3
4 3
Output
1
Input
5 5
2 1
1 3
2 3
2 5
4 3
Output
0
Input
6 5
1 2
2 3
4 5
4 6
5 6
Output
1
-----Note-----
In the first sample the following road orientation is allowed: $1 \rightarrow 2$, $1 \rightarrow 3$, $3 \rightarrow 4$.
The second sample: $1 \rightarrow 2$, $3 \rightarrow 1$, $2 \rightarrow 3$, $2 \rightarrow 5$, $3 \rightarrow 4$.
The third sample: $1 \rightarrow 2$, $2 \rightarrow 3$, $4 \rightarrow 5$, $5 \rightarrow 6$, $6 \rightarrow 4$. | n, m = map(int, input().split())
g = [[] for i in range(n)]
for i in range(m):
a, b = map(int, input().split())
g[a - 1].append(b - 1)
g[b - 1].append(a - 1)
a = 0
v = [-1] * n
def dfs(x):
nonlocal a
s = [x]
v[x] = -2
while s:
x = s.pop()
for j in g[x]:
if v[j] == -1:
s.append(j)
v[j] = x
elif j != v[x]:
return 0
return 1
for i in range(n):
if v[i] == -1:
a += dfs(i)
print(a) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads.
The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).
In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads into it, while it is allowed to have roads leading from this city.
Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.
-----Input-----
The first line of the input contains two positive integers, n and m β the number of the cities and the number of roads in Berland (2 β€ n β€ 100 000, 1 β€ m β€ 100 000).
Next m lines contain the descriptions of the roads: the i-th road is determined by two distinct integers x_{i}, y_{i} (1 β€ x_{i}, y_{i} β€ n, x_{i} β y_{i}), where x_{i} and y_{i} are the numbers of the cities connected by the i-th road.
It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.
-----Output-----
Print a single integerΒ β the minimum number of separated cities after the reform.
-----Examples-----
Input
4 3
2 1
1 3
4 3
Output
1
Input
5 5
2 1
1 3
2 3
2 5
4 3
Output
0
Input
6 5
1 2
2 3
4 5
4 6
5 6
Output
1
-----Note-----
In the first sample the following road orientation is allowed: $1 \rightarrow 2$, $1 \rightarrow 3$, $3 \rightarrow 4$.
The second sample: $1 \rightarrow 2$, $3 \rightarrow 1$, $2 \rightarrow 3$, $2 \rightarrow 5$, $3 \rightarrow 4$.
The third sample: $1 \rightarrow 2$, $2 \rightarrow 3$, $4 \rightarrow 5$, $5 \rightarrow 6$, $6 \rightarrow 4$. | n, m = map(int, input().split())
E = [[] for i in range(n + 1)]
for _ in range(m):
x, y = map(int, input().split())
E[x] += [y]
E[y] += [x]
f = [0] * (n + 1)
def bfs(nom):
l = [(nom, 0)]
f[nom] = 1
k = 1
while len(l):
nom, pre = l.pop()
for x in E[nom]:
if x != pre:
if f[x]:
k = 0
else:
f[x] = 1
l += [(x, nom)]
return k
ans = 0
for i in range(1, n + 1):
if f[i] == 0:
ans += bfs(i)
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR VAR LIST VAR VAR VAR LIST VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR LIST VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads.
The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).
In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads into it, while it is allowed to have roads leading from this city.
Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.
-----Input-----
The first line of the input contains two positive integers, n and m β the number of the cities and the number of roads in Berland (2 β€ n β€ 100 000, 1 β€ m β€ 100 000).
Next m lines contain the descriptions of the roads: the i-th road is determined by two distinct integers x_{i}, y_{i} (1 β€ x_{i}, y_{i} β€ n, x_{i} β y_{i}), where x_{i} and y_{i} are the numbers of the cities connected by the i-th road.
It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.
-----Output-----
Print a single integerΒ β the minimum number of separated cities after the reform.
-----Examples-----
Input
4 3
2 1
1 3
4 3
Output
1
Input
5 5
2 1
1 3
2 3
2 5
4 3
Output
0
Input
6 5
1 2
2 3
4 5
4 6
5 6
Output
1
-----Note-----
In the first sample the following road orientation is allowed: $1 \rightarrow 2$, $1 \rightarrow 3$, $3 \rightarrow 4$.
The second sample: $1 \rightarrow 2$, $3 \rightarrow 1$, $2 \rightarrow 3$, $2 \rightarrow 5$, $3 \rightarrow 4$.
The third sample: $1 \rightarrow 2$, $2 \rightarrow 3$, $4 \rightarrow 5$, $5 \rightarrow 6$, $6 \rightarrow 4$. | class StdIO:
def read_int(self):
return int(self.read_string())
def read_ints(self, sep=None):
return [int(i) for i in self.read_strings(sep)]
def read_float(self):
return float(self.read_string())
def read_floats(self, sep=None):
return [float(i) for i in self.read_strings(sep)]
def read_string(self):
return input()
def read_strings(self, sep=None):
return self.read_string().split(sep)
io = StdIO()
def bfs(adj, vi, visited):
q = [vi]
visited[vi] = True
vc = 0
ec = 0
while q:
v = q.pop()
vc += 1
ec += len(adj[v])
for u in adj[v]:
if not visited[u]:
q.append(u)
visited[u] = True
return vc, ec // 2
def main():
n, m = io.read_ints()
adj = [list() for i in range(n)]
for i in range(m):
x, y = io.read_ints()
x -= 1
y -= 1
adj[x].append(y)
adj[y].append(x)
visited = [False] * n
bad_cities = 0
for v in range(n):
if not visited[v]:
vc, ec = bfs(adj, v, visited)
if ec < vc:
bad_cities += vc - ec
print(bad_cities)
main() | CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF NONE RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF NONE RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF NONE RETURN FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads.
The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).
In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads into it, while it is allowed to have roads leading from this city.
Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.
-----Input-----
The first line of the input contains two positive integers, n and m β the number of the cities and the number of roads in Berland (2 β€ n β€ 100 000, 1 β€ m β€ 100 000).
Next m lines contain the descriptions of the roads: the i-th road is determined by two distinct integers x_{i}, y_{i} (1 β€ x_{i}, y_{i} β€ n, x_{i} β y_{i}), where x_{i} and y_{i} are the numbers of the cities connected by the i-th road.
It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.
-----Output-----
Print a single integerΒ β the minimum number of separated cities after the reform.
-----Examples-----
Input
4 3
2 1
1 3
4 3
Output
1
Input
5 5
2 1
1 3
2 3
2 5
4 3
Output
0
Input
6 5
1 2
2 3
4 5
4 6
5 6
Output
1
-----Note-----
In the first sample the following road orientation is allowed: $1 \rightarrow 2$, $1 \rightarrow 3$, $3 \rightarrow 4$.
The second sample: $1 \rightarrow 2$, $3 \rightarrow 1$, $2 \rightarrow 3$, $2 \rightarrow 5$, $3 \rightarrow 4$.
The third sample: $1 \rightarrow 2$, $2 \rightarrow 3$, $4 \rightarrow 5$, $5 \rightarrow 6$, $6 \rightarrow 4$. | class Stack(object):
def __init__(self):
self.items = []
def push(self, new):
self.items.append(new)
def pop(self):
return self.items.pop()
def empty(self):
return self.items == []
def size(self):
return len(self.items)
L = [int(s) for s in input().split()]
n, m = L[0], L[1]
S = [[] for i in range(n)]
for i in range(m):
Side = [int(s) for s in input().split()]
S[Side[0] - 1].append(Side[1] - 1)
S[Side[1] - 1].append(Side[0] - 1)
result = 0
visited = [(False) for i in range(n)]
color = [None for i in range(n)]
q = Stack()
for i in range(n):
if S[i] != [] and not visited[i]:
q.push(i)
color[i] = "a"
visited[i] = True
cycle = False
while not q.empty():
j = q.pop()
color[j] = "b"
for k in S[j]:
if visited[k] and color[k] == "a":
cycle = True
if not visited[k]:
q.push(k)
visited[k] = True
color[k] = "a"
if not cycle:
result += 1
if S[i] == []:
result += 1
print(result) | CLASS_DEF VAR FUNC_DEF ASSIGN VAR LIST FUNC_DEF EXPR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN VAR LIST FUNC_DEF RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR STRING FOR VAR VAR VAR IF VAR VAR VAR VAR STRING ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR STRING IF VAR VAR NUMBER IF VAR VAR LIST VAR NUMBER EXPR FUNC_CALL VAR VAR |
Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads.
The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).
In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads into it, while it is allowed to have roads leading from this city.
Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.
-----Input-----
The first line of the input contains two positive integers, n and m β the number of the cities and the number of roads in Berland (2 β€ n β€ 100 000, 1 β€ m β€ 100 000).
Next m lines contain the descriptions of the roads: the i-th road is determined by two distinct integers x_{i}, y_{i} (1 β€ x_{i}, y_{i} β€ n, x_{i} β y_{i}), where x_{i} and y_{i} are the numbers of the cities connected by the i-th road.
It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.
-----Output-----
Print a single integerΒ β the minimum number of separated cities after the reform.
-----Examples-----
Input
4 3
2 1
1 3
4 3
Output
1
Input
5 5
2 1
1 3
2 3
2 5
4 3
Output
0
Input
6 5
1 2
2 3
4 5
4 6
5 6
Output
1
-----Note-----
In the first sample the following road orientation is allowed: $1 \rightarrow 2$, $1 \rightarrow 3$, $3 \rightarrow 4$.
The second sample: $1 \rightarrow 2$, $3 \rightarrow 1$, $2 \rightarrow 3$, $2 \rightarrow 5$, $3 \rightarrow 4$.
The third sample: $1 \rightarrow 2$, $2 \rightarrow 3$, $4 \rightarrow 5$, $5 \rightarrow 6$, $6 \rightarrow 4$. | n, m = map(int, input().split())
g = {v: [] for v in range(1, n + 1)}
for e in range(m):
u, v = map(int, input().split())
g[u].append(v)
g[v].append(u)
glob_vis = set()
def bfs(v):
vis = set([v])
glob_vis.add(v)
q = [v]
qi, e = 0, 0
while qi < len(q):
v = q[qi]
for u in g[v]:
e += 1
if u not in vis:
vis.add(u)
glob_vis.add(u)
q.append(u)
qi += 1
return [e // 2, len(vis)]
min_sep = 0
for v in range(1, n + 1):
if v not in glob_vis:
res = bfs(v)
if res[0] == res[1] - 1:
min_sep += 1
print(min_sep) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR LIST VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN LIST BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads.
The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).
In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads into it, while it is allowed to have roads leading from this city.
Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.
-----Input-----
The first line of the input contains two positive integers, n and m β the number of the cities and the number of roads in Berland (2 β€ n β€ 100 000, 1 β€ m β€ 100 000).
Next m lines contain the descriptions of the roads: the i-th road is determined by two distinct integers x_{i}, y_{i} (1 β€ x_{i}, y_{i} β€ n, x_{i} β y_{i}), where x_{i} and y_{i} are the numbers of the cities connected by the i-th road.
It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.
-----Output-----
Print a single integerΒ β the minimum number of separated cities after the reform.
-----Examples-----
Input
4 3
2 1
1 3
4 3
Output
1
Input
5 5
2 1
1 3
2 3
2 5
4 3
Output
0
Input
6 5
1 2
2 3
4 5
4 6
5 6
Output
1
-----Note-----
In the first sample the following road orientation is allowed: $1 \rightarrow 2$, $1 \rightarrow 3$, $3 \rightarrow 4$.
The second sample: $1 \rightarrow 2$, $3 \rightarrow 1$, $2 \rightarrow 3$, $2 \rightarrow 5$, $3 \rightarrow 4$.
The third sample: $1 \rightarrow 2$, $2 \rightarrow 3$, $4 \rightarrow 5$, $5 \rightarrow 6$, $6 \rightarrow 4$. | import sys
sys.setrecursionlimit(100000)
N = 100020
v = [[] for i in range(N + 3)]
vis = [(False) for i in range(N)]
tree = True
def dfs(node, par):
global vis, tree
vis[node] = True
for i in v[node]:
if i == par:
continue
if vis[i]:
tree = False
else:
dfs(i, node)
if not tree:
return
if not tree:
return
n, m = map(int, input().split())
ans = 0
for i in range(m):
x, y = list(map(int, input().split()))
v[x].append(y)
v[y].append(x)
for i in range(1, n + 1):
if not vis[i]:
tree = True
dfs(i, -1)
if tree:
ans += 1
print(ans) | IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR RETURN IF VAR RETURN ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads.
The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).
In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads into it, while it is allowed to have roads leading from this city.
Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.
-----Input-----
The first line of the input contains two positive integers, n and m β the number of the cities and the number of roads in Berland (2 β€ n β€ 100 000, 1 β€ m β€ 100 000).
Next m lines contain the descriptions of the roads: the i-th road is determined by two distinct integers x_{i}, y_{i} (1 β€ x_{i}, y_{i} β€ n, x_{i} β y_{i}), where x_{i} and y_{i} are the numbers of the cities connected by the i-th road.
It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.
-----Output-----
Print a single integerΒ β the minimum number of separated cities after the reform.
-----Examples-----
Input
4 3
2 1
1 3
4 3
Output
1
Input
5 5
2 1
1 3
2 3
2 5
4 3
Output
0
Input
6 5
1 2
2 3
4 5
4 6
5 6
Output
1
-----Note-----
In the first sample the following road orientation is allowed: $1 \rightarrow 2$, $1 \rightarrow 3$, $3 \rightarrow 4$.
The second sample: $1 \rightarrow 2$, $3 \rightarrow 1$, $2 \rightarrow 3$, $2 \rightarrow 5$, $3 \rightarrow 4$.
The third sample: $1 \rightarrow 2$, $2 \rightarrow 3$, $4 \rightarrow 5$, $5 \rightarrow 6$, $6 \rightarrow 4$. | from sys import *
class graph:
def __init__(self, n, v):
self.G = dict()
for i in range(1, n + 1):
self.G.update([(i, set())])
self.v = v
self.length = n
def AppendEdge(self, a):
self.G[a[0]].add(a[1])
if not self.v:
self.G[a[1]].add(a[0])
def ShowAdjacent(self, a):
return self.G[a]
def ShowLength(self):
return self.length
def Show(self):
print(self.G)
def BFS(G, v):
Q = []
Qp = 0
Q.append((v, -1))
VisitedVertex[v - 1] = 1
ret = 1
while Qp != len(Q):
s = Q[Qp]
Qp += 1
for i in G.ShowAdjacent(s[0]):
if not VisitedVertex[i - 1]:
Q.append((i, s[0]))
VisitedVertex[i - 1] = 1
elif i != s[1]:
ret = 0
return ret
n, m = (int(z) for z in stdin.readline().split())
G = graph(n, 0)
for i in range(m):
s = [int(z) for z in stdin.readline().split()]
G.AppendEdge(s)
i = 1
res = 0
VisitedVertex = [0] * G.ShowLength()
while i <= n:
if not VisitedVertex[i - 1]:
res += BFS(G, i)
i += 1
print(res) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_DEF RETURN VAR VAR FUNC_DEF RETURN VAR FUNC_DEF EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR WHILE VAR VAR IF VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads.
The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).
In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads into it, while it is allowed to have roads leading from this city.
Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.
-----Input-----
The first line of the input contains two positive integers, n and m β the number of the cities and the number of roads in Berland (2 β€ n β€ 100 000, 1 β€ m β€ 100 000).
Next m lines contain the descriptions of the roads: the i-th road is determined by two distinct integers x_{i}, y_{i} (1 β€ x_{i}, y_{i} β€ n, x_{i} β y_{i}), where x_{i} and y_{i} are the numbers of the cities connected by the i-th road.
It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.
-----Output-----
Print a single integerΒ β the minimum number of separated cities after the reform.
-----Examples-----
Input
4 3
2 1
1 3
4 3
Output
1
Input
5 5
2 1
1 3
2 3
2 5
4 3
Output
0
Input
6 5
1 2
2 3
4 5
4 6
5 6
Output
1
-----Note-----
In the first sample the following road orientation is allowed: $1 \rightarrow 2$, $1 \rightarrow 3$, $3 \rightarrow 4$.
The second sample: $1 \rightarrow 2$, $3 \rightarrow 1$, $2 \rightarrow 3$, $2 \rightarrow 5$, $3 \rightarrow 4$.
The third sample: $1 \rightarrow 2$, $2 \rightarrow 3$, $4 \rightarrow 5$, $5 \rightarrow 6$, $6 \rightarrow 4$. | import sys
input = sys.stdin.buffer.readline
def dfs(l):
a = [0] * len(l)
counter = 0
for i in range(len(l)):
if a[i] != 1:
fl = True
stack = [[i, 0]]
while stack:
x, par = stack.pop()
a[x] = 1
for j in l[x]:
if j == par:
continue
elif a[j] == 1:
fl = False
a[j] = 1
stack = []
break
else:
a[j] = 1
stack.append([j, x])
if fl:
counter += 1
return counter
n, m = list(map(int, input().split()))
l = {i: [] for i in range(n)}
for i in range(m):
x, y = list(map(int, input().split()))
l[x - 1].append(y - 1)
l[y - 1].append(x - 1)
ans = dfs(l)
print(ans) | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST LIST VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR IF VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads.
The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).
In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads into it, while it is allowed to have roads leading from this city.
Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.
-----Input-----
The first line of the input contains two positive integers, n and m β the number of the cities and the number of roads in Berland (2 β€ n β€ 100 000, 1 β€ m β€ 100 000).
Next m lines contain the descriptions of the roads: the i-th road is determined by two distinct integers x_{i}, y_{i} (1 β€ x_{i}, y_{i} β€ n, x_{i} β y_{i}), where x_{i} and y_{i} are the numbers of the cities connected by the i-th road.
It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.
-----Output-----
Print a single integerΒ β the minimum number of separated cities after the reform.
-----Examples-----
Input
4 3
2 1
1 3
4 3
Output
1
Input
5 5
2 1
1 3
2 3
2 5
4 3
Output
0
Input
6 5
1 2
2 3
4 5
4 6
5 6
Output
1
-----Note-----
In the first sample the following road orientation is allowed: $1 \rightarrow 2$, $1 \rightarrow 3$, $3 \rightarrow 4$.
The second sample: $1 \rightarrow 2$, $3 \rightarrow 1$, $2 \rightarrow 3$, $2 \rightarrow 5$, $3 \rightarrow 4$.
The third sample: $1 \rightarrow 2$, $2 \rightarrow 3$, $4 \rightarrow 5$, $5 \rightarrow 6$, $6 \rightarrow 4$. | import sys
n, m = [int(x) for x in input().split()]
E = [[] for i in range(1, n + 1)]
for line in sys.stdin:
u, v = [(int(x) - 1) for x in line.split()]
E[u].append(v)
E[v].append(u)
ans = 0
visited = [False] * n
for v in range(n):
if visited[v]:
continue
hasCycle = False
stack = [(v, v)]
while stack:
node, p = stack.pop()
if visited[node]:
hasCycle = True
else:
visited[node] = True
for u in E[node]:
if u != p:
stack.append((u, node))
ans += 0 if hasCycle else 1
print(ans) | IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads.
The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).
In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads into it, while it is allowed to have roads leading from this city.
Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.
-----Input-----
The first line of the input contains two positive integers, n and m β the number of the cities and the number of roads in Berland (2 β€ n β€ 100 000, 1 β€ m β€ 100 000).
Next m lines contain the descriptions of the roads: the i-th road is determined by two distinct integers x_{i}, y_{i} (1 β€ x_{i}, y_{i} β€ n, x_{i} β y_{i}), where x_{i} and y_{i} are the numbers of the cities connected by the i-th road.
It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.
-----Output-----
Print a single integerΒ β the minimum number of separated cities after the reform.
-----Examples-----
Input
4 3
2 1
1 3
4 3
Output
1
Input
5 5
2 1
1 3
2 3
2 5
4 3
Output
0
Input
6 5
1 2
2 3
4 5
4 6
5 6
Output
1
-----Note-----
In the first sample the following road orientation is allowed: $1 \rightarrow 2$, $1 \rightarrow 3$, $3 \rightarrow 4$.
The second sample: $1 \rightarrow 2$, $3 \rightarrow 1$, $2 \rightarrow 3$, $2 \rightarrow 5$, $3 \rightarrow 4$.
The third sample: $1 \rightarrow 2$, $2 \rightarrow 3$, $4 \rightarrow 5$, $5 \rightarrow 6$, $6 \rightarrow 4$. | import sys
sys.setrecursionlimit(200000)
n, m = map(int, input().split())
graph = [[] for _ in range(n)]
notused = [(True) for _ in range(n)]
for _ in range(m):
x, y = map(int, input().split())
x -= 1
y -= 1
graph[x].append(y)
graph[y].append(x)
def dfs(x, pre):
if notused[x]:
notused[x] = False
res = True
for i in graph[x]:
if pre != i:
res = res and dfs(i, x)
return res
else:
return False
k = 0
for j in range(n):
if notused[j]:
if dfs(j, -1):
k += 1
print(k) | IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads.
The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).
In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads into it, while it is allowed to have roads leading from this city.
Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.
-----Input-----
The first line of the input contains two positive integers, n and m β the number of the cities and the number of roads in Berland (2 β€ n β€ 100 000, 1 β€ m β€ 100 000).
Next m lines contain the descriptions of the roads: the i-th road is determined by two distinct integers x_{i}, y_{i} (1 β€ x_{i}, y_{i} β€ n, x_{i} β y_{i}), where x_{i} and y_{i} are the numbers of the cities connected by the i-th road.
It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.
-----Output-----
Print a single integerΒ β the minimum number of separated cities after the reform.
-----Examples-----
Input
4 3
2 1
1 3
4 3
Output
1
Input
5 5
2 1
1 3
2 3
2 5
4 3
Output
0
Input
6 5
1 2
2 3
4 5
4 6
5 6
Output
1
-----Note-----
In the first sample the following road orientation is allowed: $1 \rightarrow 2$, $1 \rightarrow 3$, $3 \rightarrow 4$.
The second sample: $1 \rightarrow 2$, $3 \rightarrow 1$, $2 \rightarrow 3$, $2 \rightarrow 5$, $3 \rightarrow 4$.
The third sample: $1 \rightarrow 2$, $2 \rightarrow 3$, $4 \rightarrow 5$, $5 \rightarrow 6$, $6 \rightarrow 4$. | a = input().split()
n = int(a[0])
m = int(a[1])
p = []
rnk = []
def init(s):
nonlocal p
nonlocal rnk
for i in range(0, s):
p.append(i)
rnk = [1] * s
def fs(v):
nonlocal p
if p[v] == v:
return v
ans = fs(p[v])
p[v] = ans
return ans
def us(a, b):
nonlocal p
nonlocal rnk
a = fs(a)
b = fs(b)
if a == b:
rnk[a] += 1
return
if rnk[a] > rnk[b]:
p[b] = a
rnk[a] += rnk[b]
else:
p[a] = b
rnk[b] += rnk[a]
init(n)
for i in range(0, m):
e = input().split()
us(int(e[0]) - 1, int(e[1]) - 1)
D = {}
for i in range(0, n):
if D.get(fs(i)) == None:
D[fs(i)] = 1
else:
D[fs(i)] += 1
ans = 0
for i in D:
if rnk[i] == D[i]:
ans += 1
print(ans) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER RETURN IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads.
The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).
In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads into it, while it is allowed to have roads leading from this city.
Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.
-----Input-----
The first line of the input contains two positive integers, n and m β the number of the cities and the number of roads in Berland (2 β€ n β€ 100 000, 1 β€ m β€ 100 000).
Next m lines contain the descriptions of the roads: the i-th road is determined by two distinct integers x_{i}, y_{i} (1 β€ x_{i}, y_{i} β€ n, x_{i} β y_{i}), where x_{i} and y_{i} are the numbers of the cities connected by the i-th road.
It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.
-----Output-----
Print a single integerΒ β the minimum number of separated cities after the reform.
-----Examples-----
Input
4 3
2 1
1 3
4 3
Output
1
Input
5 5
2 1
1 3
2 3
2 5
4 3
Output
0
Input
6 5
1 2
2 3
4 5
4 6
5 6
Output
1
-----Note-----
In the first sample the following road orientation is allowed: $1 \rightarrow 2$, $1 \rightarrow 3$, $3 \rightarrow 4$.
The second sample: $1 \rightarrow 2$, $3 \rightarrow 1$, $2 \rightarrow 3$, $2 \rightarrow 5$, $3 \rightarrow 4$.
The third sample: $1 \rightarrow 2$, $2 \rightarrow 3$, $4 \rightarrow 5$, $5 \rightarrow 6$, $6 \rightarrow 4$. | from sys import setrecursionlimit
setrecursionlimit(200000)
def main():
n, m = list(map(int, input().split()))
graph = [[] for _ in range(n + 1)]
avail = [True] * (n + 1)
for _ in range(m):
x, y = list(map(int, input().split()))
graph[x].append(y)
graph[y].append(x)
def dfs(x, parent):
if avail[x]:
avail[x] = False
res = True
for y in graph[x]:
if parent != y:
res = res and dfs(y, x)
return res
else:
return False
res = 0
for j in range(1, n + 1):
if avail[j]:
if dfs(j, 0):
res += 1
print(res)
def __starting_point():
main()
__starting_point() | EXPR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Berland has n cities connected by m bidirectional roads. No road connects a city to itself, and each pair of cities is connected by no more than one road. It is not guaranteed that you can get from any city to any other one, using only the existing roads.
The President of Berland decided to make changes to the road system and instructed the Ministry of Transport to make this reform. Now, each road should be unidirectional (only lead from one city to another).
In order not to cause great resentment among residents, the reform needs to be conducted so that there can be as few separate cities as possible. A city is considered separate, if no road leads into it, while it is allowed to have roads leading from this city.
Help the Ministry of Transport to find the minimum possible number of separate cities after the reform.
-----Input-----
The first line of the input contains two positive integers, n and m β the number of the cities and the number of roads in Berland (2 β€ n β€ 100 000, 1 β€ m β€ 100 000).
Next m lines contain the descriptions of the roads: the i-th road is determined by two distinct integers x_{i}, y_{i} (1 β€ x_{i}, y_{i} β€ n, x_{i} β y_{i}), where x_{i} and y_{i} are the numbers of the cities connected by the i-th road.
It is guaranteed that there is no more than one road between each pair of cities, but it is not guaranteed that from any city you can get to any other one, using only roads.
-----Output-----
Print a single integerΒ β the minimum number of separated cities after the reform.
-----Examples-----
Input
4 3
2 1
1 3
4 3
Output
1
Input
5 5
2 1
1 3
2 3
2 5
4 3
Output
0
Input
6 5
1 2
2 3
4 5
4 6
5 6
Output
1
-----Note-----
In the first sample the following road orientation is allowed: $1 \rightarrow 2$, $1 \rightarrow 3$, $3 \rightarrow 4$.
The second sample: $1 \rightarrow 2$, $3 \rightarrow 1$, $2 \rightarrow 3$, $2 \rightarrow 5$, $3 \rightarrow 4$.
The third sample: $1 \rightarrow 2$, $2 \rightarrow 3$, $4 \rightarrow 5$, $5 \rightarrow 6$, $6 \rightarrow 4$. | def dfs(node, par):
seen[node] = 1
for each in adj[node]:
if seen[each] == 0:
if dfs(each, node) == True:
return True
elif each != par:
return True
return False
n, m = map(int, input().split())
adj = [list() for i in range(n + 1)]
seen = [0] * (n + 1)
for _ in range(m):
a, b = map(int, input().split())
adj[a].append(b)
adj[b].append(a)
ans = 0
for i in range(1, n):
if seen[i] == 0:
if not dfs(i, i):
ans += 1
print(ans) | FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a string s, consisting of lowercase English letters, and the integer m.
One should choose some symbols from the given string so that any contiguous subsegment of length m has at least one selected symbol. Note that here we choose positions of symbols, not the symbols themselves.
Then one uses the chosen symbols to form a new string. All symbols from the chosen position should be used, but we are allowed to rearrange them in any order.
Formally, we choose a subsequence of indices 1 β€ i_1 < i_2 < ... < i_{t} β€ |s|. The selected sequence must meet the following condition: for every j such that 1 β€ j β€ |s| - m + 1, there must be at least one selected index that belongs to the segment [j, Β j + m - 1], i.e. there should exist a k from 1 to t, such that j β€ i_{k} β€ j + m - 1.
Then we take any permutation p of the selected indices and form a new string s_{i}_{p}_1s_{i}_{p}_2... s_{i}_{p}_{t}.
Find the lexicographically smallest string, that can be obtained using this procedure.
-----Input-----
The first line of the input contains a single integer m (1 β€ m β€ 100 000).
The second line contains the string s consisting of lowercase English letters. It is guaranteed that this string is non-empty and its length doesn't exceed 100 000. It is also guaranteed that the number m doesn't exceed the length of the string s.
-----Output-----
Print the single line containing the lexicographically smallest string, that can be obtained using the procedure described above.
-----Examples-----
Input
3
cbabc
Output
a
Input
2
abcab
Output
aab
Input
3
bcabcbaccba
Output
aaabb
-----Note-----
In the first sample, one can choose the subsequence {3} and form a string "a".
In the second sample, one can choose the subsequence {1, 2, 4} (symbols on this positions are 'a', 'b' and 'a') and rearrange the chosen symbols to form a string "aab". | abc = [
"a",
"b",
"c",
"d",
"e",
"f",
"g",
"h",
"i",
"j",
"k",
"l",
"m",
"n",
"o",
"p",
"q",
"r",
"s",
"t",
"u",
"v",
"w",
"x",
"y",
"z",
]
m = int(input())
string = input()
good = False
toCheck = []
lenS = len(string)
holes = [(-1, lenS)]
have = {
"s": 0,
"i": 0,
"y": 0,
"h": 0,
"r": 0,
"f": 0,
"d": 0,
"z": 0,
"q": 0,
"t": 0,
"n": 0,
"g": 0,
"l": 0,
"k": 0,
"o": 0,
"c": 0,
"w": 0,
"m": 0,
"b": 0,
"u": 0,
"a": 0,
"v": 0,
"e": 0,
"p": 0,
"j": 0,
"x": 0,
}
for sym in range(26):
if not good:
good = True
for hole in holes:
i = hole[0] + 1
end = hole[1]
while i < end:
fill = string[i : min(end, i + m)].rfind(abc[sym])
if fill == -1:
good = False
break
else:
have[abc[sym]] += 1
i = i + fill + 1
if end - i < m:
break
if not good:
break
holes = []
if not good:
have = {
"s": 0,
"i": 0,
"y": 0,
"h": 0,
"r": 0,
"f": 0,
"d": 0,
"z": 0,
"q": 0,
"t": 0,
"n": 0,
"g": 0,
"l": 0,
"k": 0,
"o": 0,
"c": 0,
"w": 0,
"m": 0,
"b": 0,
"u": 0,
"a": 0,
"v": 0,
"e": 0,
"p": 0,
"j": 0,
"x": 0,
}
toCheck.append(abc[sym])
good = True
lastSeen = -1
for i in range(lenS):
if string[i] in toCheck:
have[string[i]] += 1
if i - lastSeen > m:
holes.append((lastSeen, i))
good = False
lastSeen = i
if lenS - lastSeen > m:
holes.append((lastSeen, lenS))
good = False
almost = [(have[i] * i) for i in abc]
print("".join(almost)) | ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR IF VAR ASSIGN VAR LIST IF VAR ASSIGN VAR DICT STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
You are given a string s, consisting of lowercase English letters, and the integer m.
One should choose some symbols from the given string so that any contiguous subsegment of length m has at least one selected symbol. Note that here we choose positions of symbols, not the symbols themselves.
Then one uses the chosen symbols to form a new string. All symbols from the chosen position should be used, but we are allowed to rearrange them in any order.
Formally, we choose a subsequence of indices 1 β€ i_1 < i_2 < ... < i_{t} β€ |s|. The selected sequence must meet the following condition: for every j such that 1 β€ j β€ |s| - m + 1, there must be at least one selected index that belongs to the segment [j, Β j + m - 1], i.e. there should exist a k from 1 to t, such that j β€ i_{k} β€ j + m - 1.
Then we take any permutation p of the selected indices and form a new string s_{i}_{p}_1s_{i}_{p}_2... s_{i}_{p}_{t}.
Find the lexicographically smallest string, that can be obtained using this procedure.
-----Input-----
The first line of the input contains a single integer m (1 β€ m β€ 100 000).
The second line contains the string s consisting of lowercase English letters. It is guaranteed that this string is non-empty and its length doesn't exceed 100 000. It is also guaranteed that the number m doesn't exceed the length of the string s.
-----Output-----
Print the single line containing the lexicographically smallest string, that can be obtained using the procedure described above.
-----Examples-----
Input
3
cbabc
Output
a
Input
2
abcab
Output
aab
Input
3
bcabcbaccba
Output
aaabb
-----Note-----
In the first sample, one can choose the subsequence {3} and form a string "a".
In the second sample, one can choose the subsequence {1, 2, 4} (symbols on this positions are 'a', 'b' and 'a') and rearrange the chosen symbols to form a string "aab". | n = int(input())
word = input()
cnt = [0] * 26
for x in word:
cnt[ord(x) - ord("a")] += 1
ind = 25
sum = 0
for i in range(26):
pre = -1
cur = -1
ans = 0
flag = True
for j, x in enumerate(word):
if ord(x) - ord("a") < i:
pre = j
elif ord(x) - ord("a") == i:
cur = j
if j - pre == n:
if j - cur >= n:
flag = False
break
pre = cur
ans += 1
if flag:
ind = i
sum = ans
break
for i in range(ind):
print(chr(ord("a") + i) * cnt[i], end="")
print(chr(ord("a") + ind) * sum) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR |
You are given a string s, consisting of lowercase English letters, and the integer m.
One should choose some symbols from the given string so that any contiguous subsegment of length m has at least one selected symbol. Note that here we choose positions of symbols, not the symbols themselves.
Then one uses the chosen symbols to form a new string. All symbols from the chosen position should be used, but we are allowed to rearrange them in any order.
Formally, we choose a subsequence of indices 1 β€ i_1 < i_2 < ... < i_{t} β€ |s|. The selected sequence must meet the following condition: for every j such that 1 β€ j β€ |s| - m + 1, there must be at least one selected index that belongs to the segment [j, Β j + m - 1], i.e. there should exist a k from 1 to t, such that j β€ i_{k} β€ j + m - 1.
Then we take any permutation p of the selected indices and form a new string s_{i}_{p}_1s_{i}_{p}_2... s_{i}_{p}_{t}.
Find the lexicographically smallest string, that can be obtained using this procedure.
-----Input-----
The first line of the input contains a single integer m (1 β€ m β€ 100 000).
The second line contains the string s consisting of lowercase English letters. It is guaranteed that this string is non-empty and its length doesn't exceed 100 000. It is also guaranteed that the number m doesn't exceed the length of the string s.
-----Output-----
Print the single line containing the lexicographically smallest string, that can be obtained using the procedure described above.
-----Examples-----
Input
3
cbabc
Output
a
Input
2
abcab
Output
aab
Input
3
bcabcbaccba
Output
aaabb
-----Note-----
In the first sample, one can choose the subsequence {3} and form a string "a".
In the second sample, one can choose the subsequence {1, 2, 4} (symbols on this positions are 'a', 'b' and 'a') and rearrange the chosen symbols to form a string "aab". | m = int(input())
s = input()
d = [(0) for _ in range(26)]
for char in s:
d[ord(char) - ord("a")] += 1
for i in range(26):
char, left, right, counter = chr(ord("a") + i), -1, -1, 0
for j in range(len(s)):
if s[j] < char:
left = j
if s[j] == char:
right = j
if j - left >= m:
if j - right >= m:
counter = -1
break
counter += 1
left = right
if ~counter:
for j in range(i):
print(chr(ord("a") + j) * d[j], end="")
print(chr(ord("a") + i) * counter)
break | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR |
You are given a string s, consisting of lowercase English letters, and the integer m.
One should choose some symbols from the given string so that any contiguous subsegment of length m has at least one selected symbol. Note that here we choose positions of symbols, not the symbols themselves.
Then one uses the chosen symbols to form a new string. All symbols from the chosen position should be used, but we are allowed to rearrange them in any order.
Formally, we choose a subsequence of indices 1 β€ i_1 < i_2 < ... < i_{t} β€ |s|. The selected sequence must meet the following condition: for every j such that 1 β€ j β€ |s| - m + 1, there must be at least one selected index that belongs to the segment [j, Β j + m - 1], i.e. there should exist a k from 1 to t, such that j β€ i_{k} β€ j + m - 1.
Then we take any permutation p of the selected indices and form a new string s_{i}_{p}_1s_{i}_{p}_2... s_{i}_{p}_{t}.
Find the lexicographically smallest string, that can be obtained using this procedure.
-----Input-----
The first line of the input contains a single integer m (1 β€ m β€ 100 000).
The second line contains the string s consisting of lowercase English letters. It is guaranteed that this string is non-empty and its length doesn't exceed 100 000. It is also guaranteed that the number m doesn't exceed the length of the string s.
-----Output-----
Print the single line containing the lexicographically smallest string, that can be obtained using the procedure described above.
-----Examples-----
Input
3
cbabc
Output
a
Input
2
abcab
Output
aab
Input
3
bcabcbaccba
Output
aaabb
-----Note-----
In the first sample, one can choose the subsequence {3} and form a string "a".
In the second sample, one can choose the subsequence {1, 2, 4} (symbols on this positions are 'a', 'b' and 'a') and rearrange the chosen symbols to form a string "aab". | m = int(input())
s = input()
n = len(s)
t = []
u = [1] * n
d = "a"
i = 0
while i <= n - m:
k = i
for j in range(m):
if s[i + j] <= s[k]:
k = i + j
t += [s[k]]
d = max(d, s[k])
u[k] = 0
i = k + 1
t += [q for q, v in zip(s, u) if q < d and v]
print("".join(sorted(t))) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR |
You are given a string s, consisting of lowercase English letters, and the integer m.
One should choose some symbols from the given string so that any contiguous subsegment of length m has at least one selected symbol. Note that here we choose positions of symbols, not the symbols themselves.
Then one uses the chosen symbols to form a new string. All symbols from the chosen position should be used, but we are allowed to rearrange them in any order.
Formally, we choose a subsequence of indices 1 β€ i_1 < i_2 < ... < i_{t} β€ |s|. The selected sequence must meet the following condition: for every j such that 1 β€ j β€ |s| - m + 1, there must be at least one selected index that belongs to the segment [j, Β j + m - 1], i.e. there should exist a k from 1 to t, such that j β€ i_{k} β€ j + m - 1.
Then we take any permutation p of the selected indices and form a new string s_{i}_{p}_1s_{i}_{p}_2... s_{i}_{p}_{t}.
Find the lexicographically smallest string, that can be obtained using this procedure.
-----Input-----
The first line of the input contains a single integer m (1 β€ m β€ 100 000).
The second line contains the string s consisting of lowercase English letters. It is guaranteed that this string is non-empty and its length doesn't exceed 100 000. It is also guaranteed that the number m doesn't exceed the length of the string s.
-----Output-----
Print the single line containing the lexicographically smallest string, that can be obtained using the procedure described above.
-----Examples-----
Input
3
cbabc
Output
a
Input
2
abcab
Output
aab
Input
3
bcabcbaccba
Output
aaabb
-----Note-----
In the first sample, one can choose the subsequence {3} and form a string "a".
In the second sample, one can choose the subsequence {1, 2, 4} (symbols on this positions are 'a', 'b' and 'a') and rearrange the chosen symbols to form a string "aab". | m = int(input())
s = input()
ans = []
mark = [(True) for _ in range(len(s))]
z = "a"
i = 0
while i <= len(s) - m:
k = i
for j in range(i, i + m):
if s[j] <= s[k]:
k = j
ans.append(s[k])
z = max(z, s[k])
mark[k] = False
i = k
i += 1
for i in range(len(s)):
if s[i] < z and mark[i]:
ans.append(s[i])
print("".join(str(i) for i in sorted(ans))) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR |
You are given a string s, consisting of lowercase English letters, and the integer m.
One should choose some symbols from the given string so that any contiguous subsegment of length m has at least one selected symbol. Note that here we choose positions of symbols, not the symbols themselves.
Then one uses the chosen symbols to form a new string. All symbols from the chosen position should be used, but we are allowed to rearrange them in any order.
Formally, we choose a subsequence of indices 1 β€ i_1 < i_2 < ... < i_{t} β€ |s|. The selected sequence must meet the following condition: for every j such that 1 β€ j β€ |s| - m + 1, there must be at least one selected index that belongs to the segment [j, Β j + m - 1], i.e. there should exist a k from 1 to t, such that j β€ i_{k} β€ j + m - 1.
Then we take any permutation p of the selected indices and form a new string s_{i}_{p}_1s_{i}_{p}_2... s_{i}_{p}_{t}.
Find the lexicographically smallest string, that can be obtained using this procedure.
-----Input-----
The first line of the input contains a single integer m (1 β€ m β€ 100 000).
The second line contains the string s consisting of lowercase English letters. It is guaranteed that this string is non-empty and its length doesn't exceed 100 000. It is also guaranteed that the number m doesn't exceed the length of the string s.
-----Output-----
Print the single line containing the lexicographically smallest string, that can be obtained using the procedure described above.
-----Examples-----
Input
3
cbabc
Output
a
Input
2
abcab
Output
aab
Input
3
bcabcbaccba
Output
aaabb
-----Note-----
In the first sample, one can choose the subsequence {3} and form a string "a".
In the second sample, one can choose the subsequence {1, 2, 4} (symbols on this positions are 'a', 'b' and 'a') and rearrange the chosen symbols to form a string "aab". | m = int(input()) - 1
k = input()
r = ""
i = 97
t = [k]
while 1:
q = chr(i)
p = []
for d in t:
for s in d.split(q):
if len(s) > m:
p += [s]
if not p:
break
r += q * k.count(q)
i += 1
t = p
y = chr(i)
for d in t:
i = 0
for x in d:
if x == y:
j = i
if i == m:
r += y
i -= j
j = 0
else:
i += 1
print(r) | ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR LIST VAR IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a string s, consisting of lowercase English letters, and the integer m.
One should choose some symbols from the given string so that any contiguous subsegment of length m has at least one selected symbol. Note that here we choose positions of symbols, not the symbols themselves.
Then one uses the chosen symbols to form a new string. All symbols from the chosen position should be used, but we are allowed to rearrange them in any order.
Formally, we choose a subsequence of indices 1 β€ i_1 < i_2 < ... < i_{t} β€ |s|. The selected sequence must meet the following condition: for every j such that 1 β€ j β€ |s| - m + 1, there must be at least one selected index that belongs to the segment [j, Β j + m - 1], i.e. there should exist a k from 1 to t, such that j β€ i_{k} β€ j + m - 1.
Then we take any permutation p of the selected indices and form a new string s_{i}_{p}_1s_{i}_{p}_2... s_{i}_{p}_{t}.
Find the lexicographically smallest string, that can be obtained using this procedure.
-----Input-----
The first line of the input contains a single integer m (1 β€ m β€ 100 000).
The second line contains the string s consisting of lowercase English letters. It is guaranteed that this string is non-empty and its length doesn't exceed 100 000. It is also guaranteed that the number m doesn't exceed the length of the string s.
-----Output-----
Print the single line containing the lexicographically smallest string, that can be obtained using the procedure described above.
-----Examples-----
Input
3
cbabc
Output
a
Input
2
abcab
Output
aab
Input
3
bcabcbaccba
Output
aaabb
-----Note-----
In the first sample, one can choose the subsequence {3} and form a string "a".
In the second sample, one can choose the subsequence {1, 2, 4} (symbols on this positions are 'a', 'b' and 'a') and rearrange the chosen symbols to form a string "aab". | m = int(input())
s = input().strip()
sa = [0] * len(s)
for i in range(len(s)):
sa[i] = ord(s[i]) - ord("a")
sa = [-1] + sa + [-1]
def check_value(sa, m, threshold):
prev_ind = 0
for i in range(len(sa)):
if sa[i] <= threshold:
if i - prev_ind <= m:
prev_ind = i
else:
return False
return True
def get_indexes(sa, threshold):
seq = [i for i in range(len(sa)) if sa[i] <= threshold]
return seq
def filter_indexes(sa, seq, el, m):
new_seq = [0]
for i in range(1, len(seq) - 1):
if sa[seq[i]] != el or sa[seq[i]] == el and seq[i + 1] - new_seq[-1] > m:
new_seq.append(seq[i])
return new_seq[1:]
threshold = -1
while not check_value(sa, m, threshold):
threshold += 1
seq = get_indexes(sa, threshold)
seq = filter_indexes(sa, seq, threshold, m)
s = "".join(sorted([chr(ord("a") + sa[x]) for x in seq]))
print(s) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Given an array nums of integers, a moveΒ consists of choosing any element and decreasing it by 1.
An array A is aΒ zigzag arrayΒ if either:
Every even-indexed element is greater than adjacent elements, ie.Β A[0] > A[1] < A[2] > A[3] < A[4] > ...
OR, every odd-indexed element is greater than adjacent elements, ie.Β A[0] < A[1] > A[2] < A[3] > A[4] < ...
Return the minimum number of moves to transform the given array nums into a zigzag array.
Β
Example 1:
Input: nums = [1,2,3]
Output: 2
Explanation: We can decrease 2 to 0 or 3 to 1.
Example 2:
Input: nums = [9,6,1,6,2]
Output: 4
Β
Constraints:
1 <= nums.length <= 1000
1 <= nums[i] <= 1000 | class Solution:
def movesToMakeZigzag(self, nums: List[int]) -> int:
if len(nums) <= 2:
return 0
n = len(nums)
if n % 2 == 0:
step1 = max(0, nums[-1] - nums[-2] + 1)
for i in range(1, n - 1, 2):
step1 += max(0, nums[i] - min(nums[i - 1], nums[i + 1]) + 1)
step2 = max(0, nums[0] - nums[1] + 1)
for i in range(2, n, 2):
step2 += max(0, nums[i] - min(nums[i - 1], nums[i + 1]) + 1)
else:
step1 = max(0, nums[-1] - nums[-2] + 1) + max(0, nums[0] - nums[1] + 1)
for i in range(2, n - 1, 2):
step1 += max(0, nums[i] - min(nums[i - 1], nums[i + 1]) + 1)
step2 = 0
for i in range(1, n, 2):
step2 += max(0, nums[i] - min(nums[i - 1], nums[i + 1]) + 1)
return min(step1, step2) | CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR VAR |
Given an array nums of integers, a moveΒ consists of choosing any element and decreasing it by 1.
An array A is aΒ zigzag arrayΒ if either:
Every even-indexed element is greater than adjacent elements, ie.Β A[0] > A[1] < A[2] > A[3] < A[4] > ...
OR, every odd-indexed element is greater than adjacent elements, ie.Β A[0] < A[1] > A[2] < A[3] > A[4] < ...
Return the minimum number of moves to transform the given array nums into a zigzag array.
Β
Example 1:
Input: nums = [1,2,3]
Output: 2
Explanation: We can decrease 2 to 0 or 3 to 1.
Example 2:
Input: nums = [9,6,1,6,2]
Output: 4
Β
Constraints:
1 <= nums.length <= 1000
1 <= nums[i] <= 1000 | class Solution:
def movesToMakeZigzag(self, nums: List[int]) -> int:
even = odd = 0
n = len(nums)
for i in range(n):
prev = nums[i - 1] if i - 1 >= 0 else float("inf")
next = nums[i + 1] if i + 1 < n else float("inf")
change = max(0, nums[i] - min(prev, next) + 1)
if i % 2 == 0:
even += change
else:
odd += change
return min(even, odd) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR |
Given an array nums of integers, a moveΒ consists of choosing any element and decreasing it by 1.
An array A is aΒ zigzag arrayΒ if either:
Every even-indexed element is greater than adjacent elements, ie.Β A[0] > A[1] < A[2] > A[3] < A[4] > ...
OR, every odd-indexed element is greater than adjacent elements, ie.Β A[0] < A[1] > A[2] < A[3] > A[4] < ...
Return the minimum number of moves to transform the given array nums into a zigzag array.
Β
Example 1:
Input: nums = [1,2,3]
Output: 2
Explanation: We can decrease 2 to 0 or 3 to 1.
Example 2:
Input: nums = [9,6,1,6,2]
Output: 4
Β
Constraints:
1 <= nums.length <= 1000
1 <= nums[i] <= 1000 | class Solution:
def movesToMakeZigzag(self, nums: List[int]) -> int:
return self.sb(nums)
def sb(self, nums):
nums = [float("inf")] + nums + [float("inf")]
res = [0, 0]
for i in range(1, len(nums) - 1):
res[i % 2] += max(0, nums[i] - min(nums[i - 1], nums[i + 1]) + 1)
return min(res) | CLASS_DEF FUNC_DEF VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP LIST FUNC_CALL VAR STRING VAR LIST FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR |
Given an array nums of integers, a moveΒ consists of choosing any element and decreasing it by 1.
An array A is aΒ zigzag arrayΒ if either:
Every even-indexed element is greater than adjacent elements, ie.Β A[0] > A[1] < A[2] > A[3] < A[4] > ...
OR, every odd-indexed element is greater than adjacent elements, ie.Β A[0] < A[1] > A[2] < A[3] > A[4] < ...
Return the minimum number of moves to transform the given array nums into a zigzag array.
Β
Example 1:
Input: nums = [1,2,3]
Output: 2
Explanation: We can decrease 2 to 0 or 3 to 1.
Example 2:
Input: nums = [9,6,1,6,2]
Output: 4
Β
Constraints:
1 <= nums.length <= 1000
1 <= nums[i] <= 1000 | class Solution:
def movesToMakeZigzag(self, A: List[int]) -> int:
res1 = 0
n = len(A)
B = A.copy()
for i in range(1, n, 2):
if A[i - 1] <= A[i]:
t = A[i] - A[i - 1] + 1
res1 += t
A[i] = A[i - 1] - 1
if i + 1 < n and A[i + 1] <= A[i]:
t = A[i] - A[i + 1] + 1
res1 += t
A[i] = A[i + 1] - 1
res2 = 0
for i in range(0, n, 2):
if i - 1 >= 0 and B[i] >= B[i - 1]:
t = B[i] - B[i - 1] + 1
res2 += t
B[i] = B[i - 1] - 1
if i + 1 < n and B[i + 1] <= B[i]:
t = B[i] - B[i + 1] + 1
res2 += t
B[i] = B[i + 1] - 1
return min(res1, res2) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR VAR |
Given an array nums of integers, a moveΒ consists of choosing any element and decreasing it by 1.
An array A is aΒ zigzag arrayΒ if either:
Every even-indexed element is greater than adjacent elements, ie.Β A[0] > A[1] < A[2] > A[3] < A[4] > ...
OR, every odd-indexed element is greater than adjacent elements, ie.Β A[0] < A[1] > A[2] < A[3] > A[4] < ...
Return the minimum number of moves to transform the given array nums into a zigzag array.
Β
Example 1:
Input: nums = [1,2,3]
Output: 2
Explanation: We can decrease 2 to 0 or 3 to 1.
Example 2:
Input: nums = [9,6,1,6,2]
Output: 4
Β
Constraints:
1 <= nums.length <= 1000
1 <= nums[i] <= 1000 | class Solution:
def movesToMakeZigzag(self, nums):
n = len(nums)
res0 = 0
for i in range(0, n, 2):
nei = min(nums[j] for j in [i - 1, i + 1] if 0 <= j <= n - 1)
if nums[i] >= nei:
res0 += nums[i] - nei + 1
res1 = 0
for i in range(1, n, 2):
nei = min(nums[j] for j in [i - 1, i + 1] if 0 <= j <= n - 1)
if nums[i] >= nei:
res1 += nums[i] - nei + 1
return min(res0, res1) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | for i in range(int(input())):
n = int(input())
s = [[int(i) for i in input().split()] for _ in range(n)]
mini = s[0]
maxi = s[0]
c = s[0]
for i in s:
if i[0] == mini[0] and i[2] <= mini[2] or i[0] < mini[0]:
mini = i
if i[1] == maxi[1] and i[2] <= maxi[2] or i[1] > maxi[1]:
maxi = i
if i[0] < c[0] or i[1] > c[1]:
c = i
elif c[0] == i[0] and c[1] == i[1] and c[2] > i[2]:
c = i
if mini[0] < c[0] or maxi[1] > c[1]:
print(mini[2] + maxi[2])
elif c[0] == mini[0] and c[1] == maxi[1]:
print(min(c[2], mini[2] + maxi[2]))
else:
print(c[2]) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | from sys import stdin
def solve():
for _ in range(int(stdin.readline().rstrip())):
res = []
minL, maxR = 10**10, 0
costL, costR = 10**10, 10**10
max_len, cost_len = 0, 0
for _ in range(int(stdin.readline().rstrip())):
l, r, c = map(int, stdin.readline().rstrip().split())
if l < minL:
minL = l
costL = c
elif l == minL:
costL = min(costL, c)
if r > maxR:
maxR = r
costR = c
elif r == maxR:
costR = min(costR, c)
if max_len < r - l + 1:
max_len = r - l + 1
cost_len = c
elif max_len == r - l + 1:
cost_len = min(cost_len, c)
if max_len == maxR - minL + 1:
ans = min(costR + costL, cost_len)
else:
ans = costR + costL
res.append(ans)
print("\n".join(map(str, res)))
solve() | FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | def solve():
n = int(input())
valL = 0
valR = 0
mxN = 0
mnval = 0
for i in range(0, n):
l, r, c = map(int, input().split())
if i == 0:
mnL = l
mxR = r
valL = c
valR = c
mxN = r - l + 1
mnval = c
print(c)
continue
if l < mnL:
mnL = l
valL = c
elif l == mnL:
valL = min(valL, c)
if r > mxR:
mxR = r
valR = c
elif r == mxR:
valR = min(valR, c)
if r - l + 1 > mxN:
mxN = r - l + 1
mnval = c
elif r - l + 1 == mxN:
mnval = min(mnval, c)
ans = valL + valR
if mxR - mnL + 1 == mxN:
ans = min(ans, mnval)
print(ans)
t = int(input())
while t:
solve()
t -= 1 | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR EXPR FUNC_CALL VAR VAR NUMBER |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | import sys
input = sys.stdin.readline
inf = 11000000000.0
t = int(input())
while t > 0:
n = int(input())
ml, cl = inf, inf
mr, cr = 0, inf
maxlen, clen = 0, inf
for i in range(n):
l, r, c = map(int, input().split())
if l < ml:
ml = l
cl = inf
if l == ml:
cl = min(cl, c)
if r > mr:
mr = r
cr = inf
if r == mr:
cr = min(cr, c)
if maxlen < r - l + 1:
maxlen = r - l + 1
clen = inf
if maxlen == r - l + 1:
clen = min(clen, c)
ans = cl + cr
if maxlen == mr - ml + 1:
ans = min(ans, clen)
print(ans)
t -= 1 | IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | def check(t1, t2, c):
if t1 != t2:
return c[t1] + c[t2]
else:
return c[t1]
for _ in range(int(input())):
n = int(input())
l, r, c = [0] * n, [0] * n, [0] * n
for i in range(n):
x, y, z = map(int, input().split())
l[i] = x
r[i] = y
c[i] = z
t1, t2, t3 = 0, 0, 0
for i in range(n):
if l[i] < l[t1] or l[i] == l[t1] and c[i] < c[t1]:
t1 = i
if r[i] > r[t2] or r[i] == r[t2] and c[i] < c[t2]:
t2 = i
if (
l[i] < l[t3]
or r[i] > r[t3]
or l[i] == l[t3]
and r[i] == r[t3]
and c[i] < c[t3]
):
t3 = i
if (
l[t3] <= l[t1]
and r[t3] >= r[t2]
and (l[t3] < l[t1] or r[t3] > r[t2] or c[t3] < check(t1, t2, c))
):
print(c[t3])
else:
print(check(t1, t2, c)) | FUNC_DEF IF VAR VAR RETURN BIN_OP VAR VAR VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | p = open(0).readline
for i in [0] * int(p()):
x = y = z = 5e18
for f in [0] * int(p()):
l, r, c = map(int, p().split())
x = min(x, l << 31 | c)
y = min(y, -r << 31 | c)
print(min(x + y, (z := min(z, l - r << 31 | c))) & 2**31 - 1) | ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FOR VAR BIN_OP LIST NUMBER 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 BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
minL = float("inf")
maxR = float("-inf")
costLR = float("inf")
costL = None
costR = None
N = int(input())
for x in range(N):
l, r, c = map(int, input().split())
if l < minL:
costLR = float("inf")
minL = l
costL = c
elif l == minL:
costL = min(costL, c)
if r > maxR:
costLR = float("inf")
maxR = r
costR = c
elif r == maxR:
costR = min(costR, c)
ans = costL + costR
if l == minL and r == maxR:
costLR = min(costLR, c)
ans = min(ans, costLR)
print(ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NONE ASSIGN VAR NONE 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 IF VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | t = int(input())
for i in range(t):
n = int(input())
low, high, cost = [int(k) for k in input().split()]
lowcost = cost
highcost = cost
print(cost)
for j in range(n - 1):
l, r, c = [int(k) for k in input().split()]
if low > l and high < r:
lowcost = c
highcost = c
cost = c
low = l
high = r
elif low > l:
lowcost = c
low = l
if r == high:
cost = c
if highcost > c:
highcost = c
else:
cost = lowcost + highcost
elif high < r:
highcost = c
high = r
if l == low:
cost = c
if lowcost > c:
lowcost = c
else:
cost = lowcost + highcost
elif low == l and high == r:
if cost > c:
cost = c
if highcost > c:
highcost = c
if lowcost > c:
lowcost = c
elif low == l:
if lowcost > c:
lowcost = c
if lowcost + highcost < cost:
cost = lowcost + highcost
elif high == r:
if highcost > c:
highcost = c
if lowcost + highcost < cost:
cost = lowcost + highcost
print(cost) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | import sys
ints = map(int, sys.stdin.read().split())
t = next(ints)
for _ in range(t):
n = next(ints)
left = None
right = None
cost_left = None
cost_right = None
covers = False
cover_cost = 99999999999
out = []
for k in range(n):
a, b, c = next(ints), next(ints), next(ints)
if left is None or a < left:
left = a
cost_left = c
covers = False
cover_cost = 99999999999
elif left == a and c < cost_left:
cost_left = c
if right is None or b > right:
right = b
cost_right = c
covers = False
cover_cost = 99999999999
elif right == b and c < cost_right:
cost_right = c
if left == a and right == b:
covers = True
cover_cost = min(cover_cost, c)
if covers:
out.append(str(min(cost_left + cost_right, cover_cost)))
else:
out.append(str(cost_left + cost_right))
print("\n".join(out)) | IMPORT ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NONE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NONE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | import sys
def solve():
inp = sys.stdin.readline
n = int(inp())
d = dict()
bl = 11**9, -1
br = 0, -1
bad = 11**9
ans = [None] * n
for i in range(n):
l, r, c = map(int, inp().split())
bl = min(bl, (l, c))
br = max(br, (r, -c))
d[l, r] = min(d.get((l, r), bad), c)
ans[i] = min(bl[1] - br[1], d.get((bl[0], br[0]), bad))
print("\n".join(map(str, ans)))
def main():
for i in range(int(sys.stdin.readline())):
solve()
main() | IMPORT FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NONE 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 VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | for iii in range(int(input())):
minn = 98765432456
maxx = -456789867564
lenn = -1
lenncost = 987654321
minncost = 987654321
maxxcost = 12345678998765432
for jjj in range(int(input())):
a, b, c = map(int, input().split())
if a == minn and minncost >= c:
minncost = c
elif a < minn:
minn = a
minncost = c
if b == maxx and maxxcost >= c:
maxxcost = c
elif b > maxx:
maxx = b
maxxcost = c
if b - a == lenn and lenncost >= c:
lenncost = c
elif b - a > lenn:
lenn = b - a
lenncost = c
if maxx - minn == lenn:
if lenncost <= maxxcost + minncost:
print(lenncost)
else:
print(maxxcost + minncost)
else:
print(minncost + maxxcost) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR 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 FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | def solve():
smallest = 10**10
bigest = -(10**10)
tien_a = 0
tien_b = 0
tien_row = 10**10
k = 0
max_d = -(10**10)
for i in range(int(input())):
a, b, m = map(int, input().split())
if a < smallest:
smallest = a
tien_a = m
max_d = bigest - smallest + 1
tien_row = 10**10
if b > bigest:
bigest = b
tien_b = m
max_d = bigest - smallest + 1
tien_row = 10**10
if a == smallest and m < tien_a:
tien_a = m
if b == bigest and m < tien_b:
tien_b = m
if b - a + 1 >= max_d:
max_d = b - a + 1
tien_row = min(tien_row, m)
print(min(tien_a + tien_b, tien_row))
for i in range(int(input())):
solve() | FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | import sys
def sol(bestleft, besright, bestboth, l, r, c):
if (
r - l > bestboth[1] - bestboth[0]
or r - l == bestboth[1] - bestboth[0]
and c < bestboth[2]
):
bestboth = l, r, c
if l < bestleft[0] or l == bestleft[0] and c < bestleft[2]:
bestleft = l, r, c
if r > besright[1] or r == besright[1] and c < besright[2]:
besright = l, r, c
if bestboth[1] - bestboth[0] == besright[1] - bestleft[0]:
return bestleft, besright, bestboth, min(bestboth[2], bestleft[2] + besright[2])
else:
return bestleft, besright, bestboth, bestleft[2] + besright[2]
for _ in range(int(sys.stdin.readline())):
n = int(sys.stdin.readline())
bestleft = float("inf"), float("-inf"), float("inf")
besright = float("inf"), float("-inf"), float("inf")
bestboth = float("inf"), float("-inf"), float("inf")
ans = float("inf")
for _ in range(n):
l, r, c = map(int, sys.stdin.readline().strip().split())
bestleft, besright, bestboth, ans = sol(bestleft, besright, bestboth, l, r, c)
print(ans) | IMPORT FUNC_DEF IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
mi = 10**10
ma = -1
ans = sys.maxsize
tmp = sys.maxsize
for i in range(n):
l, r, c = map(int, input().split())
if mi > l:
tmp = sys.maxsize
mi = l
mincost = c
elif mi == l and mincost > c:
mincost = c
if ma < r:
tmp = sys.maxsize
ma = r
maxcost = c
elif ma == r and maxcost > c:
maxcost = c
if mi == l and ma == r:
tmp = min(tmp, c)
print(min(mincost + maxcost, tmp)) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | t = int(input())
for i in range(t):
n = int(input())
low = [10000000000, 10000000000]
high = [0, 1000000000]
both = [10000000000, 0, 1000000000]
res = [(0) for i in range(n)]
for i in range(n):
l, r, c = map(int, input().split())
if l < low[0]:
low[0] = l
low[1] = c
elif l == low[0]:
if c < low[1]:
low[1] = c
if r > high[0]:
high[0] = r
high[1] = c
elif r == high[0]:
if c < high[1]:
high[1] = c
if l == low[0] and r == high[0]:
if l == both[0] and r == both[1]:
if both[2] > c:
both[2] = c
else:
both[0] = l
both[1] = r
both[2] = c
res[i] = low[1] + high[1]
if low[0] == both[0] and high[0] == both[1]:
res[i] = min(res[i], both[2])
for i in res:
print(i) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | for _ in range(int(input())):
n = int(input())
si = [list(map(int, input().split())) for i in range(n)]
lowest_number = si[0][0]
highest_number = si[0][1]
difference = highest_number - lowest_number
lowest_cost = si[0][2]
highest_cost = si[0][2]
difference_cost = si[0][2]
print(si[0][2])
for i in si[1:]:
si0, si1, si2 = i
if si0 < lowest_number or si0 == lowest_number and si2 < lowest_cost:
lowest_number = si0
lowest_cost = si2
if si1 > highest_number or si1 == highest_number and si2 < highest_cost:
highest_number = si1
highest_cost = si2
if si1 - si0 > difference or si1 - si0 == difference and si2 < difference_cost:
difference = si1 - si0
difference_cost = si2
if difference == highest_number - lowest_number:
print(min(difference_cost, lowest_cost + highest_cost))
else:
print(lowest_cost + highest_cost) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | def hr(i, j, ct):
return ct[i] if i == j else ct[i] + ct[j]
for _ in range(int(input())):
n = int(input())
lt = [(0) for _ in range(n)]
rt = [(0) for _ in range(n)]
ct = [(0) for _ in range(n)]
for i in range(n):
lt[i], rt[i], ct[i] = map(int, input().split())
i = j = k = 0
for z in range(n):
if lt[z] < lt[i] or lt[z] == lt[i] and ct[z] < ct[i]:
i = z
if rt[z] > rt[j] or rt[z] == rt[j] and ct[z] < ct[j]:
j = z
if (
lt[z] < lt[k]
or rt[z] > rt[k]
or lt[z] == lt[k]
and rt[z] == rt[k]
and ct[z] < ct[k]
):
k = z
if (
lt[k] <= lt[i]
and rt[k] >= rt[j]
and (lt[k] < lt[i] or rt[k] > rt[j] or ct[k] < hr(i, j, ct))
):
print(ct[k])
else:
print(hr(i, j, ct)) | FUNC_DEF RETURN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | class ValCost:
def __init__(self, val, cost):
self.val = val
self.cost = cost
def main():
T = int(input())
for _ in range(T):
n = int(input())
segments = []
for j in range(n):
l, r, c = [int(x) for x in input().split(" ")]
segments.append([l, r, c])
cost = segments[0][2]
min1 = ValCost(segments[0][0], cost)
max1 = ValCost(segments[0][1], cost)
longest = ValCost(segments[0][1] - segments[0][0], cost)
print(cost)
for i in range(1, len(segments)):
segment = segments[i]
l, r, c = segment
if l == min1.val:
min1.cost = min(min1.cost, c)
if r == max1.val:
max1.cost = min(max1.cost, c)
if l < min1.val:
min1 = ValCost(l, c)
if r > max1.val:
max1 = ValCost(r, c)
if r - l == longest.val:
longest.cost = min(longest.cost, c)
if r - l > longest.val:
longest = ValCost(r - l, c)
best = min1.cost + max1.cost
if longest.val == max1.val - min1.val:
print(min(longest.cost, best))
else:
print(best)
main() | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | I = open(0).readline
for _ in [0] * int(I()):
x = 2000000000.0, 2000000000.0
y = z = 0, 2000000000.0
for _ in [0] * int(I()):
l, r, c = map(int, I().split())
x = d, e = min(x, (l, c))
y = f, g = min(y, (-r, c))
z = min(z, (l - r, c))
print(min((d + f, e + g), z)[1]) | ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | import sys
input = sys.stdin.readline
inf = 11000000000.0
for _ in range(int(input())):
max_size = 0
max_size_cost = 0
leftmost, rightmost = inf, 0
l_cost, r_cost = 0, 0
for i in range(int(input())):
l, r, cost = map(int, input().split())
if r - l + 1 > max_size or r - l + 1 == max_size and max_size_cost > cost:
max_size_cost = cost
max_size = r - l + 1
if l < leftmost or l == leftmost and cost < l_cost:
l_cost = cost
leftmost = l
if r > rightmost or r == rightmost and cost < r_cost:
r_cost = cost
rightmost = r
print(
[r_cost + l_cost, min(max_size_cost, r_cost + l_cost)][
rightmost - leftmost + 1 == max_size
]
) | IMPORT ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | import sys
def get_ints():
return map(int, sys.stdin.readline().strip().split())
t = int(input())
for rtt in range(t):
n = int(input())
a1 = 0
a2 = 0
b1 = 10**9 + 1
b2 = 0
c1 = 0
c2 = 0
for r in range(n):
a, b, c = get_ints()
if a1 < b - a + 1 or a1 == b - a + 1 and a2 > c:
a1 = b - a + 1
a2 = c
if b1 > a or b1 == a and b2 > c:
b1 = a
b2 = c
if c1 < b or c1 == b and c2 > c:
c1 = b
c2 = c
if a1 > c1 - b1 + 1:
sys.stdout.write(str(a2) + "\n")
elif a1 == c1 - b1 + 1:
sys.stdout.write(str(min(a2, b2 + c2)) + "\n")
else:
sys.stdout.write(str(b2 + c2) + "\n") | IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING IF VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR STRING |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | from sys import *
input = stdin.readline
a = int(input())
for x in range(a):
b = int(input())
lmin = 99999999999
rmax = 0
lval = 99999999999
rval = 99999999999
maxsize = 0
ans = 0
for y in range(b):
c, d, e = map(int, input().split())
if d - c + 1 > maxsize or d - c + 1 == maxsize and e < ans:
maxsize = d - c + 1
ans = e
if c < lmin or c == lmin and e < lval:
lval = e
lmin = c
if d > rmax or d == rmax and e < rval:
rval = e
rmax = d
if maxsize == rmax - lmin + 1:
print(min(ans, lval + rval))
else:
print(lval + rval) | ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | maxval = 10**9 + 1
T = int(input())
for testcase in range(1, T + 1):
n = int(input())
low = maxval
high = 0
bestlow = maxval
besthigh = maxval
bestbothcost = maxval
bestbothcandidate = False
for s in range(n):
l, r, c = list(map(int, input().split()))
if l < low:
low = l
bestlow = c
bestbothcost = maxval
bestbothcandidate = False
elif l == low:
if c < bestlow:
bestlow = c
if r > high:
high = r
besthigh = c
bestbothcost = maxval
bestbothcandidate = False
elif r == high:
if c < besthigh:
besthigh = c
if l == low and r == high:
if c < bestbothcost:
bestbothcost = c
bestbothcandidate = True
if bestbothcandidate:
if bestbothcost < bestlow + besthigh:
print(bestbothcost)
else:
bestbothcandidate = False
print(bestlow + besthigh)
else:
print(bestlow + besthigh) | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER 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 VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | import sys
def input():
return sys.stdin.readline().rstrip("\r\n")
A = 10**9 + 1
for i in range(int(input())):
n = int(input())
min_l, max_r = A, 0
min_cost_min_l, min_cost_max_r = A, A
max_length, min_cost_max_length = 0, A
for j in range(n):
l, r, c = map(int, input().split())
if l == min_l:
min_cost_min_l = min(min_cost_min_l, c)
if l < min_l:
min_l = l
min_cost_min_l = c
if r == max_r:
min_cost_max_r = min(min_cost_max_r, c)
if r > max_r:
max_r = r
min_cost_max_r = c
if r - l + 1 == max_length:
min_cost_max_length = min(min_cost_max_length, c)
if r - l + 1 > max_length:
max_length = r - l + 1
min_cost_max_length = c
if max_r - min_l + 1 == max_length:
print(min(min_cost_max_length, min_cost_min_l + min_cost_max_r))
else:
print(min_cost_min_l + min_cost_max_r) | IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | out = []
t = int(input())
for _ in range(t):
n = int(input())
d = {}
left = float("inf"), 0
right = float("-inf"), 0
for _ in range(n):
l, r, c = map(int, input().split())
if (l, r) not in d:
d[l, r] = float("inf")
d[l, r] = min(d[l, r], c)
left = min(left, (l, c))
right = max(right, (r, -c))
p = [left[1] - right[1]]
if (left[0], right[0]) in d:
p.append(d[left[0], right[0]])
out.append(min(p))
print(*out, sep="\n") | ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | import sys
input = sys.stdin.readline
MAX = sys.maxsize
def inp():
return int(input())
def read_int_list():
return list(map(int, input().split()))
def solve(segments):
first, last = MAX, -1
first_cost, last_cost = MAX, MAX
tmp = MAX
for s in segments:
(l, r), c = s
if first > l:
tmp = MAX
first = l
first_cost = c
elif first == l and first_cost > c:
first_cost = c
if last < r:
tmp = MAX
last = r
last_cost = c
elif last == r and last_cost > c:
last_cost = c
if first == l and last == r:
tmp = min(tmp, c)
print(min(first_cost + last_cost, tmp))
for _ in range(inp()):
s = list()
for _ in range(inp()):
l, r, c = read_int_list()
s.append(((l, r), c))
solve(s) | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | import sys
INT_MAX = sys.maxsize
inp = sys.stdin.readline
for _ in range(int(inp())):
n = int(inp())
d = {}
ml = INT_MAX, INT_MAX
mr = 0, -1
for i in range(n):
l, r, c = map(int, inp().split())
ml = min(ml, (l, c))
mr = max(mr, (r, -c))
d[l, r] = min(d.get((l, r), INT_MAX), c)
print(min(ml[1] - mr[1], d.get((ml[0], mr[0]), INT_MAX))) | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR VAR VAR ASSIGN VAR NUMBER NUMBER 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 VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | import sys
inp = sys.stdin.readline
inf = int(9000000000.0)
t = int(inp())
for i in range(t):
n = int(inp())
rr = [-inf, -inf]
ll = [inf, inf]
mxl = [-inf, -inf]
for j in range(n):
a, b, c = [int(x) for x in inp().split()]
rr = max(rr, [b, -c])
ll = min(ll, [a, c])
mxl = max(mxl, [b - a + 1, -c])
a1 = abs(rr[1]) + abs(ll[1])
a2 = abs(mxl[1])
if mxl[0] == rr[0] - ll[0] + 1:
print(min(a1, a2))
else:
print(a1) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR LIST VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR LIST BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | t = int(input())
for i in range(t):
n = int(input())
lefts = []
rights = []
costs = []
ans = []
for j in range(n):
l, r, c = map(int, input().split())
lefts.append(l)
rights.append(r)
costs.append(c)
mni = 0
mxi = 0
starts = {}
singles = {}
for i in range(n):
singles[rights[i] - lefts[i] + 1] = min(
singles.get(rights[i] - lefts[i] + 1, 1 << 62), costs[i]
)
if (lefts[mni], costs[mni]) > (lefts[i], costs[i]):
mni = i
if (-rights[mxi], costs[mxi]) > (-rights[i], costs[i]):
mxi = i
if costs[mni] + costs[mxi] <= singles.get(
rights[mxi] - lefts[mni] + 1, 1 << 62
):
ans.append(costs[mni] + costs[mxi])
else:
ans.append(singles[rights[mxi] - lefts[mni] + 1])
print("\n".join(map(str, ans))) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP NUMBER NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | import sys
fast = lambda: sys.stdin.readline()
def ans():
n = int(fast())
HashMap = {}
arr = []
minimum_val = float("inf")
maximum_val = -float("inf")
result = []
for _ in range(n):
a, b, c = [int(x) for x in fast().split()]
minimum_val = min(a, minimum_val)
maximum_val = max(b, maximum_val)
arr.append([a, b, c])
if a not in HashMap:
HashMap[a] = c
else:
HashMap[a] = min(HashMap[a], c)
if b not in HashMap:
HashMap[b] = c
else:
HashMap[b] = min(HashMap[b], c)
if (a, b) not in HashMap:
HashMap[a, b] = c
else:
HashMap[a, b] = min(HashMap[a, b], c)
result.append(
min(
HashMap[minimum_val] + HashMap[maximum_val],
HashMap.get((minimum_val, maximum_val), float("inf")),
)
)
for i in result:
print(i)
for _ in range(int(fast())):
ans() | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR 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 VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | sI = lambda: input()
iI = lambda: int(input())
mI = lambda: map(int, input().split())
lI = lambda: list(mI())
for i in range(iI()):
n = iI()
l, r, c = [], [], []
for i in range(n):
a, b, g = mI()
l.append(a)
r.append(b)
c.append(g)
ans = [c[0]]
d = {}
d[l[0], r[0]] = c[0]
mn = 0
mx = 0
for i in range(1, n):
if (l[i], r[i]) not in d:
d[l[i], r[i]] = c[i]
d[l[i], r[i]] = min(c[i], d[l[i], r[i]])
if l[i] < l[mn] or l[i] == l[mn] and c[i] < c[mn]:
mn = i
if r[i] > r[mx] or r[i] == r[mx] and c[i] < c[mx]:
mx = i
ans1 = c[mx] + c[mn]
ans1 = min(ans1, d.get((l[mn], r[mx]), 10**15))
ans.append(ans1)
for i in ans:
print(i) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR LIST LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | def intshop(arr):
minleft = float("inf")
maxright = 0
maxlen = 0
costl = 0
costr = 0
costlen = 0
for i in range(len(arr)):
l, r, c = arr[i][0], arr[i][1], arr[i][2]
if l < minleft:
minleft = l
costl = c
elif minleft == l:
costl = min(costl, c)
if maxright < r:
maxright = r
costr = c
elif maxright == r:
costr = min(costr, c)
if maxlen < r - l + 1:
maxlen = r - l + 1
costlen = c
elif maxlen == r - l + 1:
costlen = min(costlen, c)
cost = costl + costr
if maxlen == maxright - minleft + 1:
cost = min(cost, costlen)
print(cost)
t = int(input())
while t:
n = int(input())
res = []
while n:
arr = list(map(int, input().split()))
res.append(arr)
n -= 1
intshop(res)
t -= 1 | FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | t = int(input())
for i in range(t):
n = int(input())
segs = []
for i in range(n):
segs.append(tuple(map(int, input().split())))
min_index = 0
max_index = 0
coast_to_coast = 0
ans = [segs[0][2]]
for i in range(1, len(segs)):
if (
segs[i][0] < segs[min_index][0]
or segs[i][0] == segs[min_index][0]
and segs[i][2] < segs[min_index][2]
):
min_index = i
if (
segs[i][1] > segs[max_index][1]
or segs[i][1] == segs[max_index][1]
and segs[i][2] < segs[max_index][2]
):
max_index = i
if segs[i][0] == segs[min_index][0] and segs[i][1] == segs[max_index][1]:
if (
coast_to_coast == -1
or (
segs[coast_to_coast][0] != segs[min_index][0]
or segs[coast_to_coast][1] != segs[max_index][1]
)
or segs[coast_to_coast][2] > segs[i][2]
):
coast_to_coast = i
res = segs[min_index][2] + segs[max_index][2]
if (
segs[coast_to_coast][0] != segs[min_index][0]
or segs[coast_to_coast][1] != segs[max_index][1]
):
coast_to_coast = -1
if coast_to_coast != -1:
res = min(res, segs[coast_to_coast][2])
ans.append(res)
print("\n".join(map(str, ans))) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | p = open(0).readline
for i in [0] * int(p()):
x = y = z = 5e18
for f in [0] * int(p()):
l, r, c = map(int, p().split())
x = min(x, l << 31 | c)
y = min(y, -r << 31 | c)
print(min(x + y, (z := min(z, l - r << 31 | c))) & 2**31 - 1)
num_inp = lambda: int(input())
arr_inp = lambda: list(map(int, input().split()))
sp_inp = lambda: map(int, input().split())
str_inp = lambda: input() | ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER FOR VAR BIN_OP LIST NUMBER 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 BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
arr = []
for _ in range(n):
arr.append(list(map(int, input().split())))
p, q, a = 0, 0, 0
for i in range(n):
l, r, c = arr[i]
if l < arr[p][0] or l == arr[p][0] and c < arr[p][2]:
p = i
if r > arr[q][1] or r == arr[q][1] and c < arr[q][2]:
q = i
if (
l < arr[a][0]
or r > arr[a][1]
or l == arr[a][0]
and r == arr[a][1]
and c < arr[a][2]
):
a = i
cost = arr[p][2] + arr[q][2] - int(p == q) * arr[p][2]
if (
arr[a][0] <= arr[p][0]
and arr[a][1] >= arr[q][1]
and (arr[a][0] < arr[p][0] or arr[a][1] > arr[q][1] or arr[a][2] < cost)
):
print(arr[a][2])
else:
print(cost) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | import sys
for _ in range(int(input())):
n = int(input())
a = []
best_l = 1e17
best_r = -1
best_l_cost = 1e17
best_r_cost = 1e17
for _ in range(n):
a.append(list(map(int, input().split())))
o = {}
for i in range(n):
item = a[i]
if item[0] < best_l:
best_l = item[0]
best_l_cost = item[2]
if item[0] == best_l:
best_l_cost = min(best_l_cost, item[2])
if item[1] > best_r:
best_r = item[1]
best_r_cost = item[2]
if item[1] == best_r:
best_r_cost = min(best_r_cost, item[2])
if o.get((item[0], item[1])):
o[item[0], item[1]] = min(o[item[0], item[1]], item[2])
else:
o[item[0], item[1]] = item[2]
k = o.get((best_l, best_r))
if k == None:
k = 1e17
print(min(best_l_cost + best_r_cost, k)) | IMPORT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR |
The integers shop sells $n$ segments. The $i$-th of them contains all integers from $l_i$ to $r_i$ and costs $c_i$ coins.
Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it.
After shopping, Vasya will get some more integers as a gift. He will get integer $x$ as a gift if and only if all of the following conditions are satisfied:
Vasya hasn't bought $x$.
Vasya has bought integer $l$ that is less than $x$.
Vasya has bought integer $r$ that is greater than $x$.
Vasya can get integer $x$ as a gift only once so he won't have the same integers after receiving a gift.
For example, if Vasya buys segment $[2, 4]$ for $20$ coins and segment $[7, 8]$ for $22$ coins, he spends $42$ coins and receives integers $2, 3, 4, 7, 8$ from these segments. He also gets integers $5$ and $6$ as a gift.
Due to the technical issues only the first $s$ segments (that is, segments $[l_1, r_1], [l_2, r_2], \ldots, [l_s, r_s]$) will be available tomorrow in the shop.
Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them.
For each $s$ from $1$ to $n$, find how many coins will Vasya spend if only the first $s$ segments will be available.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β the number of test cases.
The first line of each test case contains the single integer $n$ ($1 \leq n \leq 10^5$) β the number of segments in the shop.
Each of next $n$ lines contains three integers $l_i$, $r_i$, $c_i$ ($1 \leq l_i \leq r_i \leq 10^9, 1 \leq c_i \leq 10^9$) β the ends of the $i$-th segments and its cost.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case output $n$ integers: the $s$-th ($1 \leq s \leq n$) of them should be the number of coins Vasia will spend in the shop if only the first $s$ segments will be available.
-----Examples-----
Input
3
2
2 4 20
7 8 22
2
5 11 42
5 11 42
6
1 4 4
5 8 9
7 8 7
2 10 252
1 11 271
1 10 1
Output
20
42
42
42
4
13
11
256
271
271
-----Note-----
In the first test case if $s = 1$ then Vasya can buy only the segment $[2, 4]$ for $20$ coins and get $3$ integers.
The way to get $7$ integers for $42$ coins in case $s = 2$ is described in the statement.
In the second test case note, that there can be the same segments in the shop. | import sys
input = sys.stdin.readline
for test in range(int(input())):
n = int(input())
lm = 2**69
rm = -1
res = 2**69
for _ in range(n):
l, r, c = list(map(int, input().split()))
if lm > l:
lm = l
mcl = c
res = 2**69
elif lm == l and c < mcl:
mcl = c
if r > rm:
rm = r
mcr = c
res = 2**69
elif r == rm and c < mcr:
mcr = c
if r == rm and l == lm:
res = min(res, c)
res = min(res, mcr + mcl)
print(res) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.