description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
*"You have a reason to leave this place, but I don't."*
In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows.
Both of them are given 2 strings β S and P β of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?.
First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation.
Once the above step is done, you are allowed to perform the following two operations on P any number of times:
Pick an index 1 β€ i β€ N such that P_{i} is a vowel, and change P_{i} to any consonant.
Pick an index 1 β€ i β€ N such that P_{i} is a consonant, and change P_{i} to any vowel.
Note: The vowels are the 5 characters \{a, e, i, o, u\} β the other 21 are consonants.
Find the minimum number of operations required to convert P into S.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, denoting the length of strings S and P.
- The second and third lines contain strings S and P respectively.
------ Output Format ------
For each test case, output in a single line the minimum number of operations required to convert P into S.
------ Constraints ------
$1 β€ T β€ 10^{4}$
$1 β€ N β€ 10^{5}$
- The sum of all $N$ across all test cases does not exceed $5*10^{5}$.
$|S| = |P|$
- Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$.
- Every $?$ present in $S$ and $P$ should be assigned the same character.
----- Sample Input 1 ------
3
5
ab?c?
aeg?k
6
abcde?
ehio??
4
abcd
abcd
----- Sample Output 1 ------
4
6
0
----- explanation 1 ------
Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows:
1. Convert $P_{2} = e$ to $b$ in 1 operation.
2. Convert $P_{3} = g$ to $u$ in 1 operation.
3. Convert $P_{4} = u$ to $c$ in 1 operation.
4. Convert $P_{5} = k$ to $u$ in 1 operation.
Test Case 3: The strings are already equal, so no operations are required. | for _ in range(int(input())):
n = int(input())
s = input()
t = input()
res = 0
vow = "aeiou"
freq = [0] * 26
for i in range(n):
if s[i] == t[i]:
continue
if s[i] != "?" and t[i] != "?":
if s[i] in vow and t[i] in vow:
res += 2
elif s[i] not in vow and t[i] not in vow:
res += 2
else:
res += 1
elif s[i] != "?":
freq[ord(s[i]) - 97] += 1
else:
freq[ord(t[i]) - 97] += 1
m1 = 999999999
vowc = 0
c = 0
for i in range(26):
if chr(i + 97) in vow:
vowc += freq[i]
else:
c += freq[i]
for i in range(26):
if chr(i + 97) in vow:
m1 = min(m1, 2 * (vowc - freq[i]) + c)
else:
m1 = min(m1, 2 * (c - freq[i]) + vowc)
if m1 != 999999999:
res += m1
print(res) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING VAR VAR STRING IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
*"You have a reason to leave this place, but I don't."*
In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows.
Both of them are given 2 strings β S and P β of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?.
First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation.
Once the above step is done, you are allowed to perform the following two operations on P any number of times:
Pick an index 1 β€ i β€ N such that P_{i} is a vowel, and change P_{i} to any consonant.
Pick an index 1 β€ i β€ N such that P_{i} is a consonant, and change P_{i} to any vowel.
Note: The vowels are the 5 characters \{a, e, i, o, u\} β the other 21 are consonants.
Find the minimum number of operations required to convert P into S.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, denoting the length of strings S and P.
- The second and third lines contain strings S and P respectively.
------ Output Format ------
For each test case, output in a single line the minimum number of operations required to convert P into S.
------ Constraints ------
$1 β€ T β€ 10^{4}$
$1 β€ N β€ 10^{5}$
- The sum of all $N$ across all test cases does not exceed $5*10^{5}$.
$|S| = |P|$
- Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$.
- Every $?$ present in $S$ and $P$ should be assigned the same character.
----- Sample Input 1 ------
3
5
ab?c?
aeg?k
6
abcde?
ehio??
4
abcd
abcd
----- Sample Output 1 ------
4
6
0
----- explanation 1 ------
Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows:
1. Convert $P_{2} = e$ to $b$ in 1 operation.
2. Convert $P_{3} = g$ to $u$ in 1 operation.
3. Convert $P_{4} = u$ to $c$ in 1 operation.
4. Convert $P_{5} = k$ to $u$ in 1 operation.
Test Case 3: The strings are already equal, so no operations are required. | vowels = ["a", "e", "i", "o", "u"]
alphabets = [
"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",
]
t = int(input())
while t:
t = t - 1
n = int(input())
S = input()
P = input()
source = ""
target = ""
count_arr = []
for x_x in alphabets:
count = 0
source = S.replace("?", x_x)
target = P.replace("?", x_x)
for i in range(n):
if source[i] != target[i]:
if source[i] not in vowels and target[i] not in vowels:
count = count + 2
elif source[i] in vowels and target[i] in vowels:
count = count + 2
else:
count = count + 1
count_arr.append(count)
print(min(count_arr)) | ASSIGN VAR LIST STRING STRING STRING STRING STRING 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 WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
*"You have a reason to leave this place, but I don't."*
In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows.
Both of them are given 2 strings β S and P β of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?.
First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation.
Once the above step is done, you are allowed to perform the following two operations on P any number of times:
Pick an index 1 β€ i β€ N such that P_{i} is a vowel, and change P_{i} to any consonant.
Pick an index 1 β€ i β€ N such that P_{i} is a consonant, and change P_{i} to any vowel.
Note: The vowels are the 5 characters \{a, e, i, o, u\} β the other 21 are consonants.
Find the minimum number of operations required to convert P into S.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, denoting the length of strings S and P.
- The second and third lines contain strings S and P respectively.
------ Output Format ------
For each test case, output in a single line the minimum number of operations required to convert P into S.
------ Constraints ------
$1 β€ T β€ 10^{4}$
$1 β€ N β€ 10^{5}$
- The sum of all $N$ across all test cases does not exceed $5*10^{5}$.
$|S| = |P|$
- Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$.
- Every $?$ present in $S$ and $P$ should be assigned the same character.
----- Sample Input 1 ------
3
5
ab?c?
aeg?k
6
abcde?
ehio??
4
abcd
abcd
----- Sample Output 1 ------
4
6
0
----- explanation 1 ------
Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows:
1. Convert $P_{2} = e$ to $b$ in 1 operation.
2. Convert $P_{3} = g$ to $u$ in 1 operation.
3. Convert $P_{4} = u$ to $c$ in 1 operation.
4. Convert $P_{5} = k$ to $u$ in 1 operation.
Test Case 3: The strings are already equal, so no operations are required. | t = int(input())
for _ in range(t):
n = int(input())
a = input()
b = input()
ans = 10000000007
o = {"a", "e", "i", "o", "u"}
for i in range(26):
s = a.replace("?", chr(i + 97))
p = b.replace("?", chr(i + 97))
r = 0
for i in range(n):
if s[i] != p[i]:
if s[i] not in o and p[i] in o or s[i] in o and p[i] not in o:
r += 1
else:
r += 2
ans = min(ans, r)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
*"You have a reason to leave this place, but I don't."*
In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows.
Both of them are given 2 strings β S and P β of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?.
First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation.
Once the above step is done, you are allowed to perform the following two operations on P any number of times:
Pick an index 1 β€ i β€ N such that P_{i} is a vowel, and change P_{i} to any consonant.
Pick an index 1 β€ i β€ N such that P_{i} is a consonant, and change P_{i} to any vowel.
Note: The vowels are the 5 characters \{a, e, i, o, u\} β the other 21 are consonants.
Find the minimum number of operations required to convert P into S.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, denoting the length of strings S and P.
- The second and third lines contain strings S and P respectively.
------ Output Format ------
For each test case, output in a single line the minimum number of operations required to convert P into S.
------ Constraints ------
$1 β€ T β€ 10^{4}$
$1 β€ N β€ 10^{5}$
- The sum of all $N$ across all test cases does not exceed $5*10^{5}$.
$|S| = |P|$
- Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$.
- Every $?$ present in $S$ and $P$ should be assigned the same character.
----- Sample Input 1 ------
3
5
ab?c?
aeg?k
6
abcde?
ehio??
4
abcd
abcd
----- Sample Output 1 ------
4
6
0
----- explanation 1 ------
Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows:
1. Convert $P_{2} = e$ to $b$ in 1 operation.
2. Convert $P_{3} = g$ to $u$ in 1 operation.
3. Convert $P_{4} = u$ to $c$ in 1 operation.
4. Convert $P_{5} = k$ to $u$ in 1 operation.
Test Case 3: The strings are already equal, so no operations are required. | for _ in range(int(input())):
n = int(input())
ss = input()
pp = input()
vov = "aeiou"
if ss == pp:
print(0)
else:
alphs = [chr(i) for i in range(97, 123)]
orops = 9999999999
for j in range(len(alphs)):
aa = ss
bb = pp
aa = aa.replace("?", alphs[j])
bb = bb.replace("?", alphs[j])
ops = 0
for i in range(n):
if aa[i] == bb[i]:
continue
if aa[i] in vov and bb[i] not in vov:
ops += 1
elif aa[i] not in vov and bb[i] in vov:
ops += 1
else:
ops += 2
orops = min(orops, ops)
if orops == 0:
break
print(orops) | 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 STRING IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR |
*"You have a reason to leave this place, but I don't."*
In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows.
Both of them are given 2 strings β S and P β of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?.
First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation.
Once the above step is done, you are allowed to perform the following two operations on P any number of times:
Pick an index 1 β€ i β€ N such that P_{i} is a vowel, and change P_{i} to any consonant.
Pick an index 1 β€ i β€ N such that P_{i} is a consonant, and change P_{i} to any vowel.
Note: The vowels are the 5 characters \{a, e, i, o, u\} β the other 21 are consonants.
Find the minimum number of operations required to convert P into S.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, denoting the length of strings S and P.
- The second and third lines contain strings S and P respectively.
------ Output Format ------
For each test case, output in a single line the minimum number of operations required to convert P into S.
------ Constraints ------
$1 β€ T β€ 10^{4}$
$1 β€ N β€ 10^{5}$
- The sum of all $N$ across all test cases does not exceed $5*10^{5}$.
$|S| = |P|$
- Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$.
- Every $?$ present in $S$ and $P$ should be assigned the same character.
----- Sample Input 1 ------
3
5
ab?c?
aeg?k
6
abcde?
ehio??
4
abcd
abcd
----- Sample Output 1 ------
4
6
0
----- explanation 1 ------
Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows:
1. Convert $P_{2} = e$ to $b$ in 1 operation.
2. Convert $P_{3} = g$ to $u$ in 1 operation.
3. Convert $P_{4} = u$ to $c$ in 1 operation.
4. Convert $P_{5} = k$ to $u$ in 1 operation.
Test Case 3: The strings are already equal, so no operations are required. | vowels = "aeiou"
alpha = "abcdefghijklmnopqrstuvwxyz"
def isvowel(a):
if a in vowels:
return True
else:
return False
def main():
t = int(input())
for _ in range(t):
ans = 10**9
n = int(input())
s = str(input())
p = str(input())
for c in alpha:
curr = 0
for i in range(n):
si = c if s[i] == "?" else s[i]
pi = c if p[i] == "?" else p[i]
if si == pi:
continue
curr += 1 if isvowel(si) != isvowel(pi) else 2
ans = min(ans, curr)
print(ans)
main() | ASSIGN VAR STRING ASSIGN VAR STRING FUNC_DEF IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER 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 FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR STRING VAR VAR VAR ASSIGN VAR VAR VAR STRING VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
*"You have a reason to leave this place, but I don't."*
In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows.
Both of them are given 2 strings β S and P β of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?.
First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation.
Once the above step is done, you are allowed to perform the following two operations on P any number of times:
Pick an index 1 β€ i β€ N such that P_{i} is a vowel, and change P_{i} to any consonant.
Pick an index 1 β€ i β€ N such that P_{i} is a consonant, and change P_{i} to any vowel.
Note: The vowels are the 5 characters \{a, e, i, o, u\} β the other 21 are consonants.
Find the minimum number of operations required to convert P into S.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, denoting the length of strings S and P.
- The second and third lines contain strings S and P respectively.
------ Output Format ------
For each test case, output in a single line the minimum number of operations required to convert P into S.
------ Constraints ------
$1 β€ T β€ 10^{4}$
$1 β€ N β€ 10^{5}$
- The sum of all $N$ across all test cases does not exceed $5*10^{5}$.
$|S| = |P|$
- Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$.
- Every $?$ present in $S$ and $P$ should be assigned the same character.
----- Sample Input 1 ------
3
5
ab?c?
aeg?k
6
abcde?
ehio??
4
abcd
abcd
----- Sample Output 1 ------
4
6
0
----- explanation 1 ------
Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows:
1. Convert $P_{2} = e$ to $b$ in 1 operation.
2. Convert $P_{3} = g$ to $u$ in 1 operation.
3. Convert $P_{4} = u$ to $c$ in 1 operation.
4. Convert $P_{5} = k$ to $u$ in 1 operation.
Test Case 3: The strings are already equal, so no operations are required. | vowels = ["a", "e", "i", "o", "u"]
def is_vowel(c):
return c in vowels
for _ in range(int(input())):
n = int(input())
s = input()
p = input()
ans = 1000000
for ci in range(26):
c = chr(ord("a") + ci)
s2 = s.replace("?", c)
p2 = p.replace("?", c)
ops = 0
for si, pi in zip(s2, p2):
if si == pi:
continue
ops += 2 if is_vowel(si) == is_vowel(pi) else 1
ans = min(ans, ops)
print(ans) | ASSIGN VAR LIST STRING STRING STRING STRING STRING FUNC_DEF RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
*"You have a reason to leave this place, but I don't."*
In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows.
Both of them are given 2 strings β S and P β of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?.
First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation.
Once the above step is done, you are allowed to perform the following two operations on P any number of times:
Pick an index 1 β€ i β€ N such that P_{i} is a vowel, and change P_{i} to any consonant.
Pick an index 1 β€ i β€ N such that P_{i} is a consonant, and change P_{i} to any vowel.
Note: The vowels are the 5 characters \{a, e, i, o, u\} β the other 21 are consonants.
Find the minimum number of operations required to convert P into S.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, denoting the length of strings S and P.
- The second and third lines contain strings S and P respectively.
------ Output Format ------
For each test case, output in a single line the minimum number of operations required to convert P into S.
------ Constraints ------
$1 β€ T β€ 10^{4}$
$1 β€ N β€ 10^{5}$
- The sum of all $N$ across all test cases does not exceed $5*10^{5}$.
$|S| = |P|$
- Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$.
- Every $?$ present in $S$ and $P$ should be assigned the same character.
----- Sample Input 1 ------
3
5
ab?c?
aeg?k
6
abcde?
ehio??
4
abcd
abcd
----- Sample Output 1 ------
4
6
0
----- explanation 1 ------
Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows:
1. Convert $P_{2} = e$ to $b$ in 1 operation.
2. Convert $P_{3} = g$ to $u$ in 1 operation.
3. Convert $P_{4} = u$ to $c$ in 1 operation.
4. Convert $P_{5} = k$ to $u$ in 1 operation.
Test Case 3: The strings are already equal, so no operations are required. | def abc(a, b):
l1 = []
x = "aeiou"
y = "bcdfghjklmnpqrstvwxyz"
for i in range(97, 123):
count = 0
s = a.replace("?", chr(i))
p = b.replace("?", chr(i))
for j in range(n):
if s[j] != p[j]:
if s[j] in x and p[j] in x:
count += 2
elif s[j] in x and p[j] in y:
count += 1
elif s[j] in y and p[j] in x:
count += 1
else:
count += 2
l1.append(count)
return min(l1)
for _ in range(int(input())):
n = int(input())
a = input()
b = input()
print(abc(a, b)) | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
*"You have a reason to leave this place, but I don't."*
In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows.
Both of them are given 2 strings β S and P β of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?.
First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation.
Once the above step is done, you are allowed to perform the following two operations on P any number of times:
Pick an index 1 β€ i β€ N such that P_{i} is a vowel, and change P_{i} to any consonant.
Pick an index 1 β€ i β€ N such that P_{i} is a consonant, and change P_{i} to any vowel.
Note: The vowels are the 5 characters \{a, e, i, o, u\} β the other 21 are consonants.
Find the minimum number of operations required to convert P into S.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, denoting the length of strings S and P.
- The second and third lines contain strings S and P respectively.
------ Output Format ------
For each test case, output in a single line the minimum number of operations required to convert P into S.
------ Constraints ------
$1 β€ T β€ 10^{4}$
$1 β€ N β€ 10^{5}$
- The sum of all $N$ across all test cases does not exceed $5*10^{5}$.
$|S| = |P|$
- Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$.
- Every $?$ present in $S$ and $P$ should be assigned the same character.
----- Sample Input 1 ------
3
5
ab?c?
aeg?k
6
abcde?
ehio??
4
abcd
abcd
----- Sample Output 1 ------
4
6
0
----- explanation 1 ------
Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows:
1. Convert $P_{2} = e$ to $b$ in 1 operation.
2. Convert $P_{3} = g$ to $u$ in 1 operation.
3. Convert $P_{4} = u$ to $c$ in 1 operation.
4. Convert $P_{5} = k$ to $u$ in 1 operation.
Test Case 3: The strings are already equal, so no operations are required. | def min_dico(dico):
mini = dico["a"]
for i in alphabet:
if dico[i] < mini:
mini = dico[i]
return mini
T = int(input())
alphabet = [
"a",
"z",
"e",
"r",
"t",
"y",
"u",
"i",
"o",
"p",
"q",
"s",
"d",
"f",
"g",
"h",
"j",
"k",
"l",
"m",
"w",
"x",
"c",
"v",
"b",
"n",
]
consonne = [
"z",
"r",
"t",
"y",
"p",
"q",
"s",
"d",
"f",
"g",
"h",
"j",
"k",
"l",
"m",
"w",
"x",
"c",
"v",
"b",
"n",
]
voyelle = ["a", "e", "i", "o", "u"]
for i in range(T):
N = int(input())
s = [j for j in input()]
p = [j for j in input()]
tot = 0
dic = {
"a": 0,
"z": 0,
"e": 0,
"r": 0,
"t": 0,
"y": 0,
"u": 0,
"i": 0,
"o": 0,
"p": 0,
"q": 0,
"s": 0,
"d": 0,
"f": 0,
"g": 0,
"h": 0,
"j": 0,
"k": 0,
"l": 0,
"m": 0,
"w": 0,
"x": 0,
"c": 0,
"v": 0,
"b": 0,
"n": 0,
}
for j in range(N):
if p[j] != "?" and s[j] != "?":
if p[j] != s[j]:
if s[j] in voyelle:
if p[j] in voyelle:
tot += 2
else:
tot += 1
elif not p[j] in voyelle:
tot += 2
else:
tot += 1
elif p[j] == "?":
if s[j] in voyelle:
for k in consonne:
dic[k] += 1
for k in voyelle:
dic[k] += 2
dic[s[j]] -= 2
if s[j] in consonne:
for k in consonne:
dic[k] += 2
for k in voyelle:
dic[k] += 1
dic[s[j]] -= 2
elif s[j] == "?":
if p[j] in voyelle:
for k in consonne:
dic[k] += 1
for k in voyelle:
dic[k] += 2
dic[p[j]] -= 2
if p[j] in consonne:
for k in consonne:
dic[k] += 2
for k in voyelle:
dic[k] += 1
dic[p[j]] -= 2
print(tot + min_dico(dic)) | FUNC_DEF ASSIGN VAR VAR STRING FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR 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 LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST STRING STRING STRING STRING STRING 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 ASSIGN VAR NUMBER 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 VAR IF VAR VAR STRING VAR VAR STRING IF VAR VAR VAR VAR IF VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR STRING IF VAR VAR VAR FOR VAR VAR VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR FOR VAR VAR VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR STRING IF VAR VAR VAR FOR VAR VAR VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR FOR VAR VAR VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR |
*"You have a reason to leave this place, but I don't."*
In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows.
Both of them are given 2 strings β S and P β of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?.
First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation.
Once the above step is done, you are allowed to perform the following two operations on P any number of times:
Pick an index 1 β€ i β€ N such that P_{i} is a vowel, and change P_{i} to any consonant.
Pick an index 1 β€ i β€ N such that P_{i} is a consonant, and change P_{i} to any vowel.
Note: The vowels are the 5 characters \{a, e, i, o, u\} β the other 21 are consonants.
Find the minimum number of operations required to convert P into S.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, denoting the length of strings S and P.
- The second and third lines contain strings S and P respectively.
------ Output Format ------
For each test case, output in a single line the minimum number of operations required to convert P into S.
------ Constraints ------
$1 β€ T β€ 10^{4}$
$1 β€ N β€ 10^{5}$
- The sum of all $N$ across all test cases does not exceed $5*10^{5}$.
$|S| = |P|$
- Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$.
- Every $?$ present in $S$ and $P$ should be assigned the same character.
----- Sample Input 1 ------
3
5
ab?c?
aeg?k
6
abcde?
ehio??
4
abcd
abcd
----- Sample Output 1 ------
4
6
0
----- explanation 1 ------
Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows:
1. Convert $P_{2} = e$ to $b$ in 1 operation.
2. Convert $P_{3} = g$ to $u$ in 1 operation.
3. Convert $P_{4} = u$ to $c$ in 1 operation.
4. Convert $P_{5} = k$ to $u$ in 1 operation.
Test Case 3: The strings are already equal, so no operations are required. | def check(c1, c2):
s = "bcdfghjklmnpqrstvwxyz"
if c1 == c2:
return 0
elif c1 in "aeiou" and c2 in "aeiou" or c1 in s and c2 in s:
return 2
else:
return 1
def compute(c1, c2):
if len(c1) == 0:
return 0
minval = float("inf")
for x in "abcdefghijklmnopqrstuvwxyz":
str1 = ""
str2 = ""
for i in range(len(c1)):
if c1[i] == "?" and c2[i] != "?":
str1 += x
str2 += c2[i]
elif c1[i] != "?" and c2[i] == "?":
str2 += x
str1 += c1[i]
else:
str1 += c1[i]
str2 += c2[i]
cc = 0
for i in range(len(c1)):
cc += check(str1[i], str2[i])
if cc < minval:
minval = cc
return minval
for _ in range(int(input())):
num = int(input())
stt = input()
ptt = input()
answer = num
string = "abcdefghijklmnopqrstuvwxyz"
str1 = ""
str2 = ""
for i in range(num):
if stt[i] == ptt[i]:
answer -= 1
elif stt[i] in string and ptt[i] in string:
if check(stt[i], ptt[i]) == 2:
answer += 1
else:
answer -= 1
str1 += stt[i]
str2 += ptt[i]
print(answer + compute(str1, str2)) | FUNC_DEF ASSIGN VAR STRING IF VAR VAR RETURN NUMBER IF VAR STRING VAR STRING VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR VAR STRING VAR VAR VAR VAR VAR IF VAR VAR STRING VAR VAR STRING VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR |
*"You have a reason to leave this place, but I don't."*
In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows.
Both of them are given 2 strings β S and P β of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?.
First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation.
Once the above step is done, you are allowed to perform the following two operations on P any number of times:
Pick an index 1 β€ i β€ N such that P_{i} is a vowel, and change P_{i} to any consonant.
Pick an index 1 β€ i β€ N such that P_{i} is a consonant, and change P_{i} to any vowel.
Note: The vowels are the 5 characters \{a, e, i, o, u\} β the other 21 are consonants.
Find the minimum number of operations required to convert P into S.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, denoting the length of strings S and P.
- The second and third lines contain strings S and P respectively.
------ Output Format ------
For each test case, output in a single line the minimum number of operations required to convert P into S.
------ Constraints ------
$1 β€ T β€ 10^{4}$
$1 β€ N β€ 10^{5}$
- The sum of all $N$ across all test cases does not exceed $5*10^{5}$.
$|S| = |P|$
- Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$.
- Every $?$ present in $S$ and $P$ should be assigned the same character.
----- Sample Input 1 ------
3
5
ab?c?
aeg?k
6
abcde?
ehio??
4
abcd
abcd
----- Sample Output 1 ------
4
6
0
----- explanation 1 ------
Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows:
1. Convert $P_{2} = e$ to $b$ in 1 operation.
2. Convert $P_{3} = g$ to $u$ in 1 operation.
3. Convert $P_{4} = u$ to $c$ in 1 operation.
4. Convert $P_{5} = k$ to $u$ in 1 operation.
Test Case 3: The strings are already equal, so no operations are required. | for _ in range(int(input())):
n = int(input())
s = input()
p = input()
res = 0
v = 0
dv = {}
mv = 0
c = 0
dc = {}
mc = 0
for i in range(n):
a = s[i]
b = p[i]
if a == b:
continue
if a == "?" or b == "?":
if a == "?":
l = b
else:
l = a
if l in ["a", "e", "i", "o", "u"]:
v += 1
dv[l] = dv.get(l, 0) + 1
mv = max(dv[l], mv)
else:
c += 1
dc[l] = dc.get(l, 0) + 1
mc = max(dc[l], mc)
continue
av = False
bv = False
if a in ["a", "e", "i", "o", "u"]:
av = True
if b in ["a", "e", "i", "o", "u"]:
bv = True
if av == bv:
res += 2
else:
res += 1
c_cost = (c - mc) * 2 + v
v_cost = (v - mv) * 2 + c
print(res + min(c_cost, v_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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR IF VAR STRING VAR STRING IF VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR IF VAR LIST STRING STRING STRING STRING STRING VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR LIST STRING STRING STRING STRING STRING ASSIGN VAR NUMBER IF VAR LIST STRING STRING STRING STRING STRING ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR |
*"You have a reason to leave this place, but I don't."*
In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows.
Both of them are given 2 strings β S and P β of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?.
First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation.
Once the above step is done, you are allowed to perform the following two operations on P any number of times:
Pick an index 1 β€ i β€ N such that P_{i} is a vowel, and change P_{i} to any consonant.
Pick an index 1 β€ i β€ N such that P_{i} is a consonant, and change P_{i} to any vowel.
Note: The vowels are the 5 characters \{a, e, i, o, u\} β the other 21 are consonants.
Find the minimum number of operations required to convert P into S.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, denoting the length of strings S and P.
- The second and third lines contain strings S and P respectively.
------ Output Format ------
For each test case, output in a single line the minimum number of operations required to convert P into S.
------ Constraints ------
$1 β€ T β€ 10^{4}$
$1 β€ N β€ 10^{5}$
- The sum of all $N$ across all test cases does not exceed $5*10^{5}$.
$|S| = |P|$
- Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$.
- Every $?$ present in $S$ and $P$ should be assigned the same character.
----- Sample Input 1 ------
3
5
ab?c?
aeg?k
6
abcde?
ehio??
4
abcd
abcd
----- Sample Output 1 ------
4
6
0
----- explanation 1 ------
Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows:
1. Convert $P_{2} = e$ to $b$ in 1 operation.
2. Convert $P_{3} = g$ to $u$ in 1 operation.
3. Convert $P_{4} = u$ to $c$ in 1 operation.
4. Convert $P_{5} = k$ to $u$ in 1 operation.
Test Case 3: The strings are already equal, so no operations are required. | import sys
l = ["a", "e", "i", "o", "u"]
co = [
"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",
]
def func(s1, s2, c):
count = 0
for i in range(len(s1)):
if s1[i] == "?":
s1[i] = c
if s2[i] == "?":
s2[i] = c
for i in range(len(s1)):
if s1[i] != s2[i]:
if s1[i] not in l and s2[i] not in l:
count = count + 2
elif s1[i] in l and s2[i] in l:
count = count + 2
else:
count = count + 1
return count
n = int(input())
for i in range(n):
n1 = int(input())
s1 = input()
s2 = input()
temp = sys.maxsize
for k in co:
count1 = func(list(s1), list(s2), k)
temp = min(temp, count1)
print(temp) | IMPORT ASSIGN VAR LIST STRING STRING STRING STRING STRING 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 FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
*"You have a reason to leave this place, but I don't."*
In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows.
Both of them are given 2 strings β S and P β of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?.
First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation.
Once the above step is done, you are allowed to perform the following two operations on P any number of times:
Pick an index 1 β€ i β€ N such that P_{i} is a vowel, and change P_{i} to any consonant.
Pick an index 1 β€ i β€ N such that P_{i} is a consonant, and change P_{i} to any vowel.
Note: The vowels are the 5 characters \{a, e, i, o, u\} β the other 21 are consonants.
Find the minimum number of operations required to convert P into S.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, denoting the length of strings S and P.
- The second and third lines contain strings S and P respectively.
------ Output Format ------
For each test case, output in a single line the minimum number of operations required to convert P into S.
------ Constraints ------
$1 β€ T β€ 10^{4}$
$1 β€ N β€ 10^{5}$
- The sum of all $N$ across all test cases does not exceed $5*10^{5}$.
$|S| = |P|$
- Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$.
- Every $?$ present in $S$ and $P$ should be assigned the same character.
----- Sample Input 1 ------
3
5
ab?c?
aeg?k
6
abcde?
ehio??
4
abcd
abcd
----- Sample Output 1 ------
4
6
0
----- explanation 1 ------
Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows:
1. Convert $P_{2} = e$ to $b$ in 1 operation.
2. Convert $P_{3} = g$ to $u$ in 1 operation.
3. Convert $P_{4} = u$ to $c$ in 1 operation.
4. Convert $P_{5} = k$ to $u$ in 1 operation.
Test Case 3: The strings are already equal, so no operations are required. | vowels = {"a", "e", "i", "o", "u"}
def changeops(a, b):
if a == b:
return 0
elif a in vowels and b in vowels:
return 2
elif a not in vowels and b not in vowels:
return 2
else:
return 1
t = int(input())
for _ in range(t):
n = int(input())
change = [(0) for i in range(n)]
vowcount = {}
conscount = {}
for i in range(97, 123):
if chr(i) in vowels:
vowcount[chr(i)] = 0
else:
conscount[chr(i)] = 0
s = str(input())
p = str(input())
for j in range(n):
si = s[j]
pi = p[j]
if si != "?" and pi != "?":
change[j] = changeops(si, pi)
if si == "?" and pi == "?":
change[j] = 0
elif si == "?":
if pi in vowels:
vowcount[pi] += 1
else:
conscount[pi] += 1
elif pi == "?":
if si in vowels:
vowcount[si] += 1
else:
conscount[si] += 1
totvow = sum(vowcount.values())
totcons = sum(conscount.values())
opcount = []
for v in vowcount:
opcount.append(totcons + (totvow - vowcount[v]) * 2)
for c in conscount:
opcount.append(totvow + (totcons - conscount[c]) * 2)
print(min(opcount) + sum(change)) | ASSIGN VAR STRING STRING STRING STRING STRING FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN 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 NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR STRING VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR STRING VAR STRING ASSIGN VAR VAR NUMBER IF VAR STRING IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR STRING IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
*"You have a reason to leave this place, but I don't."*
In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows.
Both of them are given 2 strings β S and P β of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?.
First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation.
Once the above step is done, you are allowed to perform the following two operations on P any number of times:
Pick an index 1 β€ i β€ N such that P_{i} is a vowel, and change P_{i} to any consonant.
Pick an index 1 β€ i β€ N such that P_{i} is a consonant, and change P_{i} to any vowel.
Note: The vowels are the 5 characters \{a, e, i, o, u\} β the other 21 are consonants.
Find the minimum number of operations required to convert P into S.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, denoting the length of strings S and P.
- The second and third lines contain strings S and P respectively.
------ Output Format ------
For each test case, output in a single line the minimum number of operations required to convert P into S.
------ Constraints ------
$1 β€ T β€ 10^{4}$
$1 β€ N β€ 10^{5}$
- The sum of all $N$ across all test cases does not exceed $5*10^{5}$.
$|S| = |P|$
- Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$.
- Every $?$ present in $S$ and $P$ should be assigned the same character.
----- Sample Input 1 ------
3
5
ab?c?
aeg?k
6
abcde?
ehio??
4
abcd
abcd
----- Sample Output 1 ------
4
6
0
----- explanation 1 ------
Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows:
1. Convert $P_{2} = e$ to $b$ in 1 operation.
2. Convert $P_{3} = g$ to $u$ in 1 operation.
3. Convert $P_{4} = u$ to $c$ in 1 operation.
4. Convert $P_{5} = k$ to $u$ in 1 operation.
Test Case 3: The strings are already equal, so no operations are required. | def swap(P, S):
mins = float("inf")
vow = ["a", "e", "i", "o", "u"]
for i in range(97, 123):
s = S[:]
p = P[:]
c = 0
s = s.replace("?", chr(i))
p = p.replace("?", chr(i))
for x, y in zip(p, s):
if x != y:
if x in vow and y in vow:
c += 2
elif x not in vow and y not in vow:
c += 2
else:
c += 1
mins = min(mins, c)
return mins
T = int(input())
while T:
N = int(input())
S = input()
P = input()
print(swap(P, S))
T -= 1 | FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST STRING STRING STRING STRING STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER |
*"You have a reason to leave this place, but I don't."*
In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows.
Both of them are given 2 strings β S and P β of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?.
First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation.
Once the above step is done, you are allowed to perform the following two operations on P any number of times:
Pick an index 1 β€ i β€ N such that P_{i} is a vowel, and change P_{i} to any consonant.
Pick an index 1 β€ i β€ N such that P_{i} is a consonant, and change P_{i} to any vowel.
Note: The vowels are the 5 characters \{a, e, i, o, u\} β the other 21 are consonants.
Find the minimum number of operations required to convert P into S.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, denoting the length of strings S and P.
- The second and third lines contain strings S and P respectively.
------ Output Format ------
For each test case, output in a single line the minimum number of operations required to convert P into S.
------ Constraints ------
$1 β€ T β€ 10^{4}$
$1 β€ N β€ 10^{5}$
- The sum of all $N$ across all test cases does not exceed $5*10^{5}$.
$|S| = |P|$
- Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$.
- Every $?$ present in $S$ and $P$ should be assigned the same character.
----- Sample Input 1 ------
3
5
ab?c?
aeg?k
6
abcde?
ehio??
4
abcd
abcd
----- Sample Output 1 ------
4
6
0
----- explanation 1 ------
Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows:
1. Convert $P_{2} = e$ to $b$ in 1 operation.
2. Convert $P_{3} = g$ to $u$ in 1 operation.
3. Convert $P_{4} = u$ to $c$ in 1 operation.
4. Convert $P_{5} = k$ to $u$ in 1 operation.
Test Case 3: The strings are already equal, so no operations are required. | t = int(input())
for _ in range(t):
n = int(input())
a = input().strip()
b = input().strip()
ans = 0
vows = {"a", "e", "i", "o", "u"}
s1 = ""
s2 = ""
for i in range(n):
if a[i] != b[i]:
if a[i] != "?" and b[i] != "?":
m = a[i] in vows
n = b[i] in vows
if m ^ n:
ans += 1
else:
ans += 2
else:
s1 += a[i]
s2 += b[i]
min_c = float("inf")
for i in range(26):
c = chr(ord("a") + i)
temp = 0
for i in range(len(s1)):
if s1[i] == "?":
k = s2[i]
else:
k = s1[i]
if k != c:
m = k in vows
n = c in vows
if m ^ n:
temp += 1
else:
temp += 2
min_c = min(min_c, temp)
print(ans + min_c) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR STRING STRING STRING STRING STRING ASSIGN VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
*"You have a reason to leave this place, but I don't."*
In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows.
Both of them are given 2 strings β S and P β of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?.
First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation.
Once the above step is done, you are allowed to perform the following two operations on P any number of times:
Pick an index 1 β€ i β€ N such that P_{i} is a vowel, and change P_{i} to any consonant.
Pick an index 1 β€ i β€ N such that P_{i} is a consonant, and change P_{i} to any vowel.
Note: The vowels are the 5 characters \{a, e, i, o, u\} β the other 21 are consonants.
Find the minimum number of operations required to convert P into S.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, denoting the length of strings S and P.
- The second and third lines contain strings S and P respectively.
------ Output Format ------
For each test case, output in a single line the minimum number of operations required to convert P into S.
------ Constraints ------
$1 β€ T β€ 10^{4}$
$1 β€ N β€ 10^{5}$
- The sum of all $N$ across all test cases does not exceed $5*10^{5}$.
$|S| = |P|$
- Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$.
- Every $?$ present in $S$ and $P$ should be assigned the same character.
----- Sample Input 1 ------
3
5
ab?c?
aeg?k
6
abcde?
ehio??
4
abcd
abcd
----- Sample Output 1 ------
4
6
0
----- explanation 1 ------
Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows:
1. Convert $P_{2} = e$ to $b$ in 1 operation.
2. Convert $P_{3} = g$ to $u$ in 1 operation.
3. Convert $P_{4} = u$ to $c$ in 1 operation.
4. Convert $P_{5} = k$ to $u$ in 1 operation.
Test Case 3: The strings are already equal, so no operations are required. | def check_vowel(x):
if x in "aeiou":
return True
return False
def get_min_move(s, p, N):
min_move = N * 2
for ch in "abcdefghijklmnopqrstuvwxyz":
s_arr = s
p_arr = p
s_arr = s_arr.replace("?", ch)
p_arr = p_arr.replace("?", ch)
move = 0
for i in range(N):
s_arr_vowel = check_vowel(s_arr[i])
p_arr_vowel = check_vowel(p_arr[i])
if s_arr[i] == p_arr[i]:
continue
elif s_arr_vowel and not p_arr_vowel:
move += 1
elif p_arr_vowel and not s_arr_vowel:
move += 1
else:
move += 2
if move < min_move:
min_move = move
return min_move
for i in range(int(input())):
N = int(input())
s_arr = input()
p_arr = input()
min_move = get_min_move(s_arr, p_arr, N)
print(min_move) | FUNC_DEF IF VAR STRING RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER FOR VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
*"You have a reason to leave this place, but I don't."*
In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows.
Both of them are given 2 strings β S and P β of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?.
First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation.
Once the above step is done, you are allowed to perform the following two operations on P any number of times:
Pick an index 1 β€ i β€ N such that P_{i} is a vowel, and change P_{i} to any consonant.
Pick an index 1 β€ i β€ N such that P_{i} is a consonant, and change P_{i} to any vowel.
Note: The vowels are the 5 characters \{a, e, i, o, u\} β the other 21 are consonants.
Find the minimum number of operations required to convert P into S.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, denoting the length of strings S and P.
- The second and third lines contain strings S and P respectively.
------ Output Format ------
For each test case, output in a single line the minimum number of operations required to convert P into S.
------ Constraints ------
$1 β€ T β€ 10^{4}$
$1 β€ N β€ 10^{5}$
- The sum of all $N$ across all test cases does not exceed $5*10^{5}$.
$|S| = |P|$
- Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$.
- Every $?$ present in $S$ and $P$ should be assigned the same character.
----- Sample Input 1 ------
3
5
ab?c?
aeg?k
6
abcde?
ehio??
4
abcd
abcd
----- Sample Output 1 ------
4
6
0
----- explanation 1 ------
Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows:
1. Convert $P_{2} = e$ to $b$ in 1 operation.
2. Convert $P_{3} = g$ to $u$ in 1 operation.
3. Convert $P_{4} = u$ to $c$ in 1 operation.
4. Convert $P_{5} = k$ to $u$ in 1 operation.
Test Case 3: The strings are already equal, so no operations are required. | t = int(input())
for i in range(0, t):
x = int(input())
vo = ["a", "e", "i", "o", "u"]
al = [
"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",
]
li = input()
ri = input()
min = 1000000000000
for j in range(0, len(al)):
mi = li.replace("?", al[j])
pi = ri.replace("?", al[j])
count = 0
for k in range(0, len(li)):
if mi[k] == pi[k]:
count = count + 0
elif (
mi[k] in "aeiou"
and pi[k] in "aeiou"
or mi[k] not in "aeiou"
and pi[k] not in "aeiou"
):
count = count + 2
else:
count = count + 1
if count < min:
min = count
print(min) | 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 LIST STRING STRING STRING STRING STRING 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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
*"You have a reason to leave this place, but I don't."*
In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows.
Both of them are given 2 strings β S and P β of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?.
First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation.
Once the above step is done, you are allowed to perform the following two operations on P any number of times:
Pick an index 1 β€ i β€ N such that P_{i} is a vowel, and change P_{i} to any consonant.
Pick an index 1 β€ i β€ N such that P_{i} is a consonant, and change P_{i} to any vowel.
Note: The vowels are the 5 characters \{a, e, i, o, u\} β the other 21 are consonants.
Find the minimum number of operations required to convert P into S.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, denoting the length of strings S and P.
- The second and third lines contain strings S and P respectively.
------ Output Format ------
For each test case, output in a single line the minimum number of operations required to convert P into S.
------ Constraints ------
$1 β€ T β€ 10^{4}$
$1 β€ N β€ 10^{5}$
- The sum of all $N$ across all test cases does not exceed $5*10^{5}$.
$|S| = |P|$
- Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$.
- Every $?$ present in $S$ and $P$ should be assigned the same character.
----- Sample Input 1 ------
3
5
ab?c?
aeg?k
6
abcde?
ehio??
4
abcd
abcd
----- Sample Output 1 ------
4
6
0
----- explanation 1 ------
Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows:
1. Convert $P_{2} = e$ to $b$ in 1 operation.
2. Convert $P_{3} = g$ to $u$ in 1 operation.
3. Convert $P_{4} = u$ to $c$ in 1 operation.
4. Convert $P_{5} = k$ to $u$ in 1 operation.
Test Case 3: The strings are already equal, so no operations are required. | for i in range(int(input())):
n = int(input())
s = input()
p = input()
d = s
e = p
arr = []
co = ["a", "e", "i", "o", "u"]
alphabet_list = [
"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",
]
for j in alphabet_list:
s = d
p = e
c = 0
s = s.replace("?", j)
p = p.replace("?", j)
for i in range(n):
if s[i] == p[i]:
pass
elif s[i] in "aeiou" and p[i] not in "aeiou":
c += 1
elif p[i] in "aeiou" and s[i] not in "aeiou":
c += 1
else:
c += 2
arr.append(c)
print(min(arr)) | 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 ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST STRING STRING STRING STRING STRING 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 FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
*"You have a reason to leave this place, but I don't."*
In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows.
Both of them are given 2 strings β S and P β of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?.
First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation.
Once the above step is done, you are allowed to perform the following two operations on P any number of times:
Pick an index 1 β€ i β€ N such that P_{i} is a vowel, and change P_{i} to any consonant.
Pick an index 1 β€ i β€ N such that P_{i} is a consonant, and change P_{i} to any vowel.
Note: The vowels are the 5 characters \{a, e, i, o, u\} β the other 21 are consonants.
Find the minimum number of operations required to convert P into S.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, denoting the length of strings S and P.
- The second and third lines contain strings S and P respectively.
------ Output Format ------
For each test case, output in a single line the minimum number of operations required to convert P into S.
------ Constraints ------
$1 β€ T β€ 10^{4}$
$1 β€ N β€ 10^{5}$
- The sum of all $N$ across all test cases does not exceed $5*10^{5}$.
$|S| = |P|$
- Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$.
- Every $?$ present in $S$ and $P$ should be assigned the same character.
----- Sample Input 1 ------
3
5
ab?c?
aeg?k
6
abcde?
ehio??
4
abcd
abcd
----- Sample Output 1 ------
4
6
0
----- explanation 1 ------
Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows:
1. Convert $P_{2} = e$ to $b$ in 1 operation.
2. Convert $P_{3} = g$ to $u$ in 1 operation.
3. Convert $P_{4} = u$ to $c$ in 1 operation.
4. Convert $P_{5} = k$ to $u$ in 1 operation.
Test Case 3: The strings are already equal, so no operations are required. | def swap(s, p, N):
mins = float("inf")
d = {"a": 1, "e": 1, "i": 1, "o": 1, "u": 1}
for i in range(97, 123):
S = s.replace("?", chr(i))
P = p.replace("?", chr(i))
count = 0
for i in range(N):
if S[i] != P[i]:
if S[i] in d:
if P[i] in d:
count += 2
else:
count += 1
elif P[i] in d:
count += 1
else:
count += 2
mins = min(count, mins)
return mins
t = int(input().strip())
while t:
N = int(input().strip())
S = input().strip()
P = input().strip()
print(swap(S, P, N))
t -= 1 | FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR DICT STRING STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER |
*"You have a reason to leave this place, but I don't."*
In the fourth game, Sang-Woo and Ali are competing against each other. Sang-Woo is the more intelligent one between them, and he plans to win the game by cheating. Ali is your childhood friend and you want to help him win this game. The game goes as follows.
Both of them are given 2 strings β S and P β of equal length (say N). The task is to find the minimum number of operations required to convert the string P into S. Every character of both S and P is either a lowercase English alphabet or ?.
First, before performing any operation, you must choose a single lowercase character from the English alphabet, and replace every occurrence of ? with this character. Note that every ? present in either S or P is replaced by the exact same character. This does not count as an operation.
Once the above step is done, you are allowed to perform the following two operations on P any number of times:
Pick an index 1 β€ i β€ N such that P_{i} is a vowel, and change P_{i} to any consonant.
Pick an index 1 β€ i β€ N such that P_{i} is a consonant, and change P_{i} to any vowel.
Note: The vowels are the 5 characters \{a, e, i, o, u\} β the other 21 are consonants.
Find the minimum number of operations required to convert P into S.
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, denoting the length of strings S and P.
- The second and third lines contain strings S and P respectively.
------ Output Format ------
For each test case, output in a single line the minimum number of operations required to convert P into S.
------ Constraints ------
$1 β€ T β€ 10^{4}$
$1 β€ N β€ 10^{5}$
- The sum of all $N$ across all test cases does not exceed $5*10^{5}$.
$|S| = |P|$
- Every character of both $S$ and $P$ is either a lowercase English alphabet or $?$.
- Every $?$ present in $S$ and $P$ should be assigned the same character.
----- Sample Input 1 ------
3
5
ab?c?
aeg?k
6
abcde?
ehio??
4
abcd
abcd
----- Sample Output 1 ------
4
6
0
----- explanation 1 ------
Test Case 1: First, replace every $?$ by $u$. This gives us $S = abucu$ and $P = aeguk$. Now $P$ can be converted to $S$ as follows:
1. Convert $P_{2} = e$ to $b$ in 1 operation.
2. Convert $P_{3} = g$ to $u$ in 1 operation.
3. Convert $P_{4} = u$ to $c$ in 1 operation.
4. Convert $P_{5} = k$ to $u$ in 1 operation.
Test Case 3: The strings are already equal, so no operations are required. | t = int(input())
for _ in range(t):
n = int(input())
a = input()
b = input()
ans = 0
cons = [
"b",
"c",
"d",
"f",
"g",
"h",
"j",
"k",
"l",
"m",
"n",
"p",
"q",
"r",
"s",
"t",
"v",
"w",
"x",
"y",
"z",
]
vowel = ["a", "e", "i", "o", "u"]
dc, dv = {}, {}
i = 0
while i < n:
if a[i] != b[i]:
if a[i] in cons and b[i] in vowel or a[i] in vowel and b[i] in cons:
ans += 1
elif a[i] in cons and b[i] in cons or a[i] in vowel and b[i] in vowel:
ans += 2
elif a[i] == "?":
if b[i] in cons:
temp = dc.get(b[i], 0) + 1
dc[b[i]] = temp
else:
temp = dv.get(b[i], 0) + 1
dv[b[i]] = temp
elif b[i] == "?":
if a[i] in cons:
temp = dc.get(a[i], 0) + 1
dc[a[i]] = temp
else:
temp = dv.get(a[i], 0) + 1
dv[a[i]] = temp
i += 1
x = 2 * (sum(dv.values()) - max(dv.values(), default=0)) + sum(dc.values())
y = 2 * (sum(dc.values()) - max(dc.values(), default=0)) + sum(dv.values())
print(ans + min(x, y)) | 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 NUMBER ASSIGN VAR LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING ASSIGN VAR LIST STRING STRING STRING STRING STRING ASSIGN VAR VAR DICT DICT ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR STRING IF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR STRING IF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | def solve(l, r, p):
res = 1000000000
for i in range(0, p + 1):
res = min(res, max(a[l + i], a[r - (p - i)]))
return res
T = int(input())
while T > 0:
T -= 1
n, m, k = map(int, input().split())
k = min(k, m - 1)
a = list(map(int, input().split()))
ans = 0
for l in range(0, k + 1):
ans = max(ans, solve(l, n - (k - l) - 1, m - k - 1))
print(ans) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | def solve(n, m, k, a):
prior_selections = m - 1
forced_choice = min(k, prior_selections)
free_choice = prior_selections - forced_choice
best_selection = [0] * (prior_selections + 1)
for selected_first in range(prior_selections + 1):
first_element = a[selected_first]
selected_last = prior_selections - selected_first
last_element = a[-(selected_last + 1)]
best_selection[selected_first] = max(first_element, last_element)
return max(
min(
best_selection[forced_first + picked_first]
for picked_first in range(free_choice + 1)
)
for forced_first in range(forced_choice + 1)
)
for T in range(int(input())):
n, m, k = map(int, input().split())
print(solve(n, m, k, list(map(int, input().split())))) | FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | t = int(input())
for _ in range(t):
n, m, k = [int(x) for x in input().split(" ")]
v = [int(x) for x in input().split(" ")]
i, j = 0, n - 1
k = min(k, m - 1)
kk = k
mm = m
ans = max(v)
final = min(v)
for x in range(0, k + 1):
ans = max(v)
for y in range(0, m - k):
ans = min(ans, max(v[x + y], v[n - 1 - (m - 1 - x - y)]))
final = max(final, ans)
print(final) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | import sys
def input():
return sys.stdin.readline().rstrip()
t = int(input())
for fashjufjh in range(t):
n, m, k = map(int, input().split())
k = min(k, m - 1)
a = list(map(int, input().split()))
ans = []
for i in range(m):
ans.append([max(a[i], a[i + n - m]), i])
ans.sort()
ans1 = [0]
for i in ans:
ans1.append(i[1] + 1)
ans1.append(len(ans) + 1)
ans1.sort()
aans = 100000000000
for j in range(len(ans1) - 1):
aans = min(aans, ans1[j] + len(ans) - ans1[j + 1] + 1)
ans1.pop()
if aans > k:
print(i[0])
break | IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR NUMBER |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | import sys
readline = sys.stdin.readline
class Segtree:
def __init__(self, A, intv, initialize=True, segf=max):
self.N = len(A)
self.N0 = 2 ** (self.N - 1).bit_length()
self.intv = intv
self.segf = segf
if initialize:
self.data = [intv] * self.N0 + A + [intv] * (self.N0 - self.N)
for i in range(self.N0 - 1, 0, -1):
self.data[i] = self.segf(self.data[2 * i], self.data[2 * i + 1])
else:
self.data = [intv] * (2 * self.N0)
def update(self, k, x):
k += self.N0
self.data[k] = x
while k > 0:
k = k >> 1
self.data[k] = self.segf(self.data[2 * k], self.data[2 * k + 1])
def query(self, l, r):
L, R = l + self.N0, r + self.N0
s = self.intv
while L < R:
if R & 1:
R -= 1
s = self.segf(s, self.data[R])
if L & 1:
s = self.segf(s, self.data[L])
L += 1
L >>= 1
R >>= 1
return s
def binsearch(self, l, r, check, reverse=False):
L, R = l + self.N0, r + self.N0
SL, SR = [], []
while L < R:
if R & 1:
R -= 1
SR.append(R)
if L & 1:
SL.append(L)
L += 1
L >>= 1
R >>= 1
if reverse:
for idx in SR + SL[::-1]:
if check(self.data[idx]):
break
else:
return -1
while idx < self.N0:
if check(self.data[2 * idx + 1]):
idx = 2 * idx + 1
else:
idx = 2 * idx
return idx - self.N0
else:
for idx in SL + SR[::-1]:
if check(self.data[idx]):
break
else:
return -1
while idx < self.N0:
if check(self.data[2 * idx]):
idx = 2 * idx
else:
idx = 2 * idx + 1
return idx - self.N0
Tc = int(readline())
Ans = [None] * Tc
for qu in range(Tc):
N, M, K = list(map(int, readline().split()))
A = list(map(int, readline().split()))
Ai = A[::-1]
table = [None] * M
for i in range(M):
j = M - 1 - i
table[i] = max(A[i], Ai[j])
inf = 10**9 + 7
T = Segtree(table, inf, initialize=True, segf=min)
ans = min(table)
K = min(K, M - 1)
R = M - 1 - K
for ki in range(K + 1):
ans = max(ans, T.query(ki, ki + R + 1))
Ans[qu] = ans
print("\n".join(map(str, Ans))) | IMPORT ASSIGN VAR VAR CLASS_DEF FUNC_DEF NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP LIST VAR VAR VAR BIN_OP LIST VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP NUMBER VAR FUNC_DEF VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR LIST LIST WHILE VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FOR VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR RETURN BIN_OP VAR VAR FOR VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | from sys import stderr, stdin
readline = stdin.buffer.readline
def rl():
return [int(w) for w in readline().split()]
(t,) = rl()
for _ in range(t):
n, m, k = rl()
a = rl()
k = min(k, m - 1)
print(
max(
min(max(a[x + y], a[n - m + x + y]) for y in range(m - k))
for x in range(k + 1)
)
) | ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | t = int(input())
for _ in range(t):
n, m, k = map(int, input().split())
l = list(map(int, input().split()))
left = n - m
done = m - 1
k = min(k, done)
ans = 0
for i in range(k + 1):
temp = 10**9 + 3
for j in range(done - k + 1):
t = max(l[i + j], l[i + j + left])
temp = min(temp, t)
ans = max(ans, temp)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | t = int(input())
while t != 0:
t -= 1
n, m, k = map(int, input().split())
k = min(k, m - 1)
w = m - k
l = list(map(int, input().split()))
a = [max(l[i], l[i + n - m]) for i in range(m)]
b = [min(a[i : i + w]) for i in range(m - w + 1)]
print(max(b)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | t = int(input())
for _ in range(t):
n, m, control = map(int, input().split())
a = list(map(int, input().split()))
control = min(m - 1, control)
not_control = m - control - 1
num = n - control - not_control
ans = 0
for i in range(control + 1):
temp = 10**9
for j in range(not_control + 1):
temp = min(temp, max(a[i + j], a[i + j + num - 1]))
ans = max(temp, ans)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | from sys import stdin
input = stdin.readline
for _ in range(int(input())):
n, m, k = map(int, input().split())
if k >= m:
k = m - 1
a = list(map(int, input().split()))
l = [max(a[i], a[i + n - m]) for i in range(m)]
l2 = [min(l[i : i + m - k]) for i in range(k + 1)]
print(max(l2)) | ASSIGN VAR VAR 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 BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | import sys
sys.setrecursionlimit(10**6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II():
return int(sys.stdin.readline())
def MI():
return map(int, sys.stdin.readline().split())
def LI():
return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number):
return [LI() for _ in range(rows_number)]
def SI():
return sys.stdin.readline()[:-1]
class SegtreeMin:
def __init__(self, aa):
self.inf = 10**16
n = len(aa)
tree_width = 2
while tree_width < n:
tree_width *= 2
self.tree_width = tree_width
self.tree = [self.inf] * (tree_width * 2 - 1)
self.tree[tree_width - 1 : tree_width - 1 + n] = aa[:]
for i in range(tree_width - 2, -1, -1):
self.tree[i] = min(self.tree[i * 2 + 1], self.tree[i * 2 + 2])
def update(self, i, a):
seg_i = self.tree_width - 1 + i
self.tree[seg_i] = a
while seg_i != 0:
seg_i = (seg_i - 1) // 2
self.tree[seg_i] = min(self.tree[seg_i * 2 + 1], self.tree[seg_i * 2 + 2])
def element(self, i):
return self.tree[self.tree_width - 1 + i]
def min(self, l, r, seg_i=0, segL=0, segR=-1):
if segR == -1:
segR = self.tree_width
if r <= segL or segR <= l:
return self.inf
if l <= segL and segR <= r:
return self.tree[seg_i]
segM = (segL + segR) // 2
ret0 = self.min(l, r, seg_i * 2 + 1, segL, segM)
ret1 = self.min(l, r, seg_i * 2 + 2, segM, segR)
return min(ret0, ret1)
def main():
t = II()
for _ in range(t):
n, m, k = MI()
aa = LI()
pp = [max(aa[i], aa[i + n - m]) for i in range(m)]
st = SegtreeMin(pp)
ans = 0
k = min(k, m - 1)
for i in range(k + 1):
ans = max(st.min(i, i + m - k), ans)
print(ans)
main() | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF RETURN VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_DEF NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | t = int(input())
for y in range(t):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
if k >= m - 1:
ans = max(max(a[:m]), max(a[n - m :]))
else:
i = k
j = 0
ans = 0
while i >= 0:
ind1 = i + (m - k) - 1
ind2 = n - j - 1
res2 = 1000000000.0
for kk in range(m - k):
res = max(a[ind1 - kk], a[ind2 - kk])
res2 = min(res, res2)
ans = max(ans, res2)
i -= 1
j += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | t = int(input())
for _ in range(t):
n, m, k = map(int, input().split())
k = min(m - 1, k)
a = list(map(int, input().split()))
a2 = [(a[i] if a[i] > a[j] else a[j]) for i, j in zip(range(n), range(n - m, n))]
front, back = [(a2[0], a2[0])], []
for i in range(1, m - k):
front.append((a2[i], min(a2[i], front[-1][1])))
ans = front[-1][1]
inf = 10**9
for i in range(m - k, m):
front.append((a2[i], min(front[-1][1], a2[i]) if front else a2[i]))
if not back:
while front:
x = front.pop()[0]
back.append((x, min(x, back[-1][1]) if back else x))
back.pop()
ans = max(
ans, min(front[-1][1] if front else inf, back[-1][1] if back else inf)
)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR LIST VAR NUMBER VAR NUMBER LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR VAR IF VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | t = int(input())
def solve(n, m, k, a):
pairs = {}
for j in range(m + 1):
pairs[j] = max(a[j], a[n - 1 - (m - j)])
ans = 0
for f in range(k + 1):
l = k - f
alpha = f
beta = m - l
delans = 10000000000
for j in range(alpha, beta + 1):
delans = min(delans, pairs[j])
ans = max(ans, delans)
return ans
for _ in range(t):
n, m, k = map(int, input().split())
m -= 1
k = min(m, k)
a = list(map(int, input().split()))
print(solve(n, m, k, a)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | def main():
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
k = min(k, m - 1)
ans = 0
for i in range(k + 1):
c = 10000000000.0 + 10
for j in range(m - k):
c = min(c, max(a[i + j], a[i + j + n - m]))
ans = max(c, ans)
print(ans)
return
def test():
t = int(input())
while t:
main()
t -= 1
test() | FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | import sys
input = lambda: sys.stdin.readline().rstrip()
T = int(input())
for _ in range(T):
N, M, K = list(map(int, input().split()))
K = min(K, M - 1)
A = [int(a) for a in input().split()]
B = [max(A[i], A[-M + i]) for i in range(M)]
print(max([min(B[i : i + M - K]) for i in range(K + 1)])) | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | t = int(input())
for test in range(t):
n, m, k = map(int, input().split())
arr = list(map(int, input().split()))
if k >= m:
k = m - 1
x = m - 1 - k
maxi = -1
for i in range(k + 1):
s = i
e = k - i
mini = 100000000000000000
for j in range(x + 1):
s += j
e += x - j
ind1 = 0 + s
ind2 = len(arr) - 1 - e
ans = max(arr[ind1], arr[ind2])
if ans < mini:
mini = ans
s -= j
e -= x - j
if mini > maxi:
maxi = mini
print(maxi) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | import sys
input = sys.stdin.buffer.readline
def getmax(l, r):
return max(a[l], a[r])
t = int(input())
for _ in range(t):
n, m, k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
control = min(k, m - 1)
remainingM = m - control
ans = -1
for l in range(control + 1):
r = n - 1 + l - control
minn = float("inf")
for mm in range(remainingM):
ll = l + mm
rr = r - (remainingM - mm) + 1
minn = min(minn, getmax(ll, rr))
ans = max(ans, minn)
print(ans) | IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | t = int(input())
ans = []
for l in range(t):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
if k < m:
p = m - k - 1
else:
k = m - 1
p = 0
res2 = -1
for i in range(k + 1):
res = 1000000001
for j in range(p + 1):
if max(a[k + p - i - j], a[-1 - i - j]) < res:
res = max(a[k + p - i - j], a[-1 - i - j])
if res > res2:
res2 = res
print(res2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | t = int(input())
for _ in range(t):
n, m, k = map(int, input().split())
data = list(map(int, input().split()))
if k >= m - 1:
output = data[0]
for i in range(m):
output = max(output, data[i], data[n - m + i])
print(output)
else:
output = float("-inf")
for i in range(k + 1):
best_k = float("inf")
for j in range(m - k):
best_k = min(best_k, max(data[i + j], data[n + i - m + j]))
output = max(output, best_k)
print(int(output)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | t = int(input())
for _ in range(t):
n, m, k = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 0
control_forward = min(k, m - 1)
for i in range(control_forward + 1):
l = i
r = n - 1 - control_forward + i
non_control_forward = max(m - 1 - control_forward, 0)
anstemp = 10**10
for j in range(non_control_forward + 1):
l += j
r += -non_control_forward + j
anstemp = min(anstemp, max(a[l], a[r]))
l -= j
r -= -non_control_forward + j
ans = max(anstemp, ans)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | for _ in range(int(input())):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
pre = a[:m]
suf = a[-1 : -m - 1 : -1]
l = [max(pre[i], suf[m - i - 1]) for i in range(m)]
if k >= m - 1:
ans = max(l)
elif k != 0:
kmin = [min(l[i : m - k + i]) for i in range(k + 1)]
ans = max(kmin)
else:
ans = min(l)
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | def calc(l, start, end, left):
arr = l[start : end + 1]
ans = max(arr)
for i in range(left + 1):
ans = min(ans, max(arr[i], arr[len(arr) - left - 1 + i]))
return ans
for nt in range(int(input())):
n, m, k = map(int, input().split())
l = list(map(int, input().split()))
if m == 1:
print(max(l[0], l[-1]))
continue
if m > k + 1:
ans = -1
for i in range(k + 1):
ans = max(ans, calc(l, i, n - 1 - k + i, m - k - 1))
print(ans)
else:
ans = -1
for i in range(m):
if l[i] > ans:
ans = l[i]
for i in range(n - 1, n - 1 - m, -1):
if l[i] > ans:
ans = l[i]
print(ans) | FUNC_DEF ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
class RangeMinimumQuery:
def __init__(self, n, F=min, e=float("inf"), fill=None):
self.n = n
self.n0 = 2 ** (n - 1).bit_length()
self.F = F
self.e = e
if fill is None:
fill = e
self.data = [fill] * (2 * self.n0)
def construct(self, a):
for i, x in enumerate(a):
self.data[i + self.n0 - 1] = x
for i in range(self.n0 - 2, -1, -1):
self.data[i] = self.F(self.data[2 * i + 1], self.data[2 * i + 2])
def query(self, l, r):
l += self.n0
r += self.n0
res = self.e
while l < r:
if r & 1:
r -= 1
res = self.F(res, self.data[r - 1])
if l & 1:
res = self.F(res, self.data[l - 1])
l += 1
l >>= 1
r >>= 1
return res
def update(self, i, x):
i += self.n0 - 1
self.data[i] = x
while i:
i = ~-i // 2
self.data[i] = self.F(self.data[2 * i + 1], self.data[2 * i + 2])
def show(self):
ret = [self.query(i, i + 1) for i in range(self.n)]
print(ret)
def solve():
n, m, k = map(int, input().split())
k = min(k, m - 1)
a = list(map(int, input().split()))
b = []
for i in range(m):
left = i
right = n - m + i
cost = max(a[left], a[right])
b.append(cost)
n = len(b)
RMQ = RangeMinimumQuery(n, fill=0)
RMQ.construct(b)
ans = 0
w = n - k
for i in range(n - w + 1):
ma = RMQ.query(i, i + w)
ans = max(ans, ma)
print(ans)
t = int(input())
for i in range(t):
solve() | IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR CLASS_DEF FUNC_DEF VAR FUNC_CALL VAR STRING NONE ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP NUMBER VAR FUNC_DEF FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | t = int(input())
for _ in range(t):
ans = 0
n, pos, control = map(int, input().split())
control = min(control, pos - 1)
not_control = pos - control - 1
num = n - control - not_control
a = list(map(int, input().split()))
for i in range(control + 1):
tmp = 10**10 + 1
for j in range(not_control + 1):
try:
tmp = min(tmp, max(a[i + j], a[i + j + num - 1]))
except ReferenceError:
pass
ans = max(ans, tmp)
if ans == 10**10:
ans = max(a[0], a[-1])
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | def Solve(k, m, n, V):
a = None
m = m - 1
k = min(m, k)
for i in range(k + 1):
b = max(V[i], V[n - 1 - (k - i) - (m - k)])
for j in range(m - k + 1):
left = i + j
right = n - 1 - (k - i) - (m - k - j)
b = min(b, max(V[left], V[right]))
if a == None:
a = b
else:
a = max(a, b)
return a
cases = int(input())
for i in range(cases):
n, m, k = map(int, input().split())
V = list(map(int, input().split()))
print(Solve(k, m, n, V)) | FUNC_DEF ASSIGN VAR NONE ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | for _ in range(int(input())):
n, m, k = map(int, input().split())
arr = list(map(int, input().split()))
if k >= m - 1:
print(max(max(arr[:m]), max(arr[n - m : n])))
continue
remaining = m - k
ans = []
for l in range(k + 1):
temp = []
i = l
j = n - (k - l) - 1
for index in range(remaining):
temp.append(max(arr[i + index], arr[j - remaining + 1 + index]))
if temp:
ans.append(min(temp))
print(max(ans)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | t = int(input())
for _ in range(t):
n, m, K = map(int, input().split())
a = [int(x) for x in input().split()]
k = min(K, m - 1)
ptr1 = k - 1
ptr2 = 0
ans = min(a)
for i in range(k + 1):
e1 = ptr1 + (m - k - 1)
e2 = ptr2
c = max(a)
for j in range(m - k):
c = min(c, max(a[e1 + 1], a[e2 - 1]))
e1 -= 1
e2 -= 1
ans = max(ans, c)
ptr1 -= 1
ptr2 -= 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | for _ in range(int(input())):
n, m, k = list(map(int, input().split()))
k = min(m - 1, k)
randomDelete = m - k - 1
a = list(map(int, input().split()))
maxm = 0
for i in range(k + 1):
mini = 10**10
for j in range(i, i + randomDelete + 1):
mini = min(mini, max(a[j], a[j + (n - randomDelete - k) - 1]))
if mini != 10**10:
maxm = max(mini, maxm)
print(maxm) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | t = int(input())
while t > 0:
t -= 1
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
v = [0] * m
for i in range(m):
v[i] = max(a[i], a[i - m])
if k >= m - 1:
print(max(v))
elif k == 0:
print(min(v))
else:
w = [0] * (k + 1)
for j in range(k + 1):
w[j] = min(v[j : m - k + j])
print(max(w)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | import sys
t = int(input())
for testo in range(t):
n, m, k = list(map(int, input().split(" ")))
array = list(map(int, input().split(" ")))
k += 1
k = min(k, m)
import sys
ans = 0
for i in range(k):
val = sys.maxsize
for j in range(m - k + 1):
val = min(val, max(array[i + j], array[-m + (i + j)]))
ans = max(ans, val)
print(ans) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IMPORT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | import sys
input = sys.stdin.readline
t = int(input())
for tests in range(t):
n, m, k = map(int, input().split())
A = list(map(int, input().split()))
k = min(k, m - 1)
ANS = -1
LEN = n - k
for start in range(k + 1):
ANSSTART = 1 << 31
last = start + LEN
s = start
l = last - (m - k)
while l < last:
ANSSTART = min(ANSSTART, max(A[s], A[l]))
s += 1
l += 1
ANS = max(ANS, ANSSTART)
print(ANS) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | T = int(input())
for case in range(T):
n, m, k = map(int, input().strip().split())
a = list(map(int, input().strip().split()))
k = min(m - 1, k)
ans = 0
for x in range(k + 1):
res = int(2000000000.0)
for y in range(m - k):
res = min(res, max(a[x + y], a[x + y + n - m]))
ans = max(ans, res)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | from sys import stdin
for _ in range(int(input())):
n, m, k = map(int, input().split())
l = list(map(int, input().split()))
k = min(k, m - 1)
b = [max(l[i], l[n + i - m]) for i in range(m)]
x = max(min(b[i : i + m - k]) for i in range(k + 1))
print(x) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | t = int(input())
for i in range(t):
n, m, k = map(int, input().split())
s = list(map(int, input().split()))
ss = list(reversed(s))
if m <= k:
print(max(max(s[:m]), max(ss[:m])))
else:
D = {}
for j in range(m):
a = max(s[j], ss[m - 1 - j])
D[j, m - 1 - j] = a
B = 0
for j in range(k + 1):
A = 10000000000
for u in range(m - k):
a = D[j + u, m - 1 - j - u]
if A > a:
A = a
if B < A:
B = A
print(B) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n, m, k = map(int, input().split())
List = [int(x) for x in input().split()]
ans = 0
k = min(k, m - 1)
a = [max(List[i], List[i + n - m]) for i in range(m)]
x = 0
for i in range(k + 1):
x = max(x, min(a[i : i + (m - k)]))
print(x) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | q = int(input())
for rwewe in range(q):
n, m, k = list(map(int, input().split()))
k = min(k, m - 1)
l = list(map(int, input().split()))
cyk = []
for i in range(n):
try:
cyk.append(max(l[i], l[n - m + i]))
except Exception:
xx = 0
p = len(cyk) - k
wyn = -1231314134411312
i = 0
while i + p <= len(cyk):
wyn = max(wyn, min(cyk[i : i + p]))
i += 1
print(wyn) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | t = int(input())
for _ in range(t):
N, m, k = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
for i in range(min(k + 1, m)):
j = k - i
tmp = 10**9
for l in range(max(1, m - k)):
r = m - k - 1 - l
left = i + l
right = -j - r - 1
tmp = min(tmp, max(a[left], a[right]))
ans = max(ans, tmp)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | a = [(0) for i in range(3505)]
q = int(input())
for _ in range(q):
n, m, k = map(int, input().split())
a[1:] = list(map(int, input().split()))
k = min(k, m - 1)
re, ans = n - k, 0
for l in range(1, n - re + 2):
r, x, len = l + re - 1, int(1000000000.0), n - m + 1
for i in range(l, r - len + 2):
x = min(x, max(a[i], a[i + len - 1]))
ans = max(ans, x)
print(ans) | ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | import sys
readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep="\n")
def solve():
ans = 0
n, m, k = nm()
a = nl()
g = max(m - k - 1, 0)
for i in range(min(k, m - 1) + 1):
lidx = i
ridx = n - min(k, m - 1) - 1 + i
cur = min(max(a[lidx + j], a[ridx - g + j]) for j in range(g + 1))
ans = max(cur, ans)
print(ans)
return
T = ni()
for _ in range(T):
solve() | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | def solve(n, m, k, a):
prior_selections = m - 1
forced = min(k, prior_selections)
free_choice = prior_selections - forced
best_selection = [0] * (prior_selections + 1)
for selected_first in range(prior_selections + 1):
first_element = a[selected_first]
selected_last = m - 1 - selected_first
last_element = a[-(selected_last + 1)]
selection = max(first_element, last_element)
best_selection[selected_first] = selection
best_of_worst = None
for forced_first in range(forced + 1):
worst_selection = None
for picked_first in range(free_choice + 1):
selected_first = forced_first + picked_first
selection = best_selection[selected_first]
if worst_selection is None or selection < worst_selection:
worst_selection = selection
if best_of_worst is None or worst_selection > best_of_worst:
best_of_worst = worst_selection
return best_of_worst
for T in range(int(input())):
n, m, k = map(int, input().split())
print(solve(n, m, k, list(map(int, input().split())))) | FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NONE FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF VAR NONE VAR VAR ASSIGN VAR VAR IF VAR NONE VAR VAR ASSIGN VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | t = int(input())
for i in range(t):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
if k >= m - 1:
for j in range(m):
ans = max(ans, max(a[j], a[n - m + j]))
else:
for j in range(k + 1):
tmp = 99999999999
for l in range(m - k):
tmp = min(tmp, max(a[j + l], a[n + j - m + l]))
ans = max(ans, tmp)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | for _ in range(int(input())):
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
mx = -1
if k >= m:
for i in range(n):
if i < m or i > n - m - 1:
mx = max(mx, a[i])
print(mx)
else:
ans = 0
for t in range(k + 1):
val = 10000000000
for i in range(m - k):
val = min(val, max(a[t + i], a[n - 1 - k + t - (m - k - 1) + i]))
ans = max(ans, val)
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | t = int(input())
while t:
n, m, k = map(int, input().split())
a = input().split()
for i in range(n):
a[i] = int(a[i])
if k >= m - 1:
ans = 0
for i in range(min(m, n)):
ans = max(ans, a[i])
i = n - 1
while n - i <= m and i >= 0:
ans = max(ans, a[i])
i -= 1
print(ans)
else:
ans = 0
for i in range(k + 1):
temp = 10**9 + 1
for j in range(min(m - k, n - i)):
temp = min(temp, max(a[i + j], a[n - m + i + j]))
ans = max(ans, temp)
print(ans)
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | t = int(input())
for _ in range(t):
n, m, k = map(int, input().split())
b = list(map(int, input().split()))
ans = 0
if k <= m - 1:
for i in range(k + 1):
q = float("inf")
for j in range(m - k):
q = min(q, max(b[i + j], b[n - m + (i + j)]))
ans = max(ans, q)
else:
for i in range(m):
ans = max(ans, max(b[i], b[n - 1 - (m - 1 - i)]))
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | t = int(input())
while t > 0:
n, m, k = map(int, input().split())
a = list(map(int, input().split()))
x = 0
if m - k - 1 > 0:
for i in range(k + 1):
bad = 99999999999
for j in range(m - k):
bad = min(bad, max(a[m - 1 - i - j], a[n - 1 - i - j]))
x = max(x, bad)
else:
for i in range(m):
y = max(a[m - 1 - i], a[n - 1 - i])
x = max(y, x)
print(x)
t -= 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
You and your $n - 1$ friends have found an array of integers $a_1, a_2, \dots, a_n$. You have decided to share it in the following way: All $n$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process.
You are standing in the $m$-th position in the line. Before the process starts, you may choose up to $k$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded.
Suppose that you're doing your choices optimally. What is the greatest integer $x$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $x$?
Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available.
-----Input-----
The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) Β β the number of test cases. The description of the test cases follows.
The first line of each test case contains three space-separated integers $n$, $m$ and $k$ ($1 \le m \le n \le 3500$, $0 \le k \le n - 1$) Β β the number of elements in the array, your position in line and the number of people whose choices you can fix.
The second line of each test case contains $n$ positive integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) Β β elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3500$.
-----Output-----
For each test case, print the largest integer $x$ such that you can guarantee to obtain at least $x$.
-----Example-----
Input
4
6 4 2
2 9 2 3 8 5
4 4 1
2 13 60 4
4 1 3
1 2 2 1
2 2 0
1 2
Output
8
4
1
1
-----Note-----
In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($5$) because he or she was forced by you to take the last element. After this turn the remaining array will be $[2, 9, 2, 3, 8]$; the second person will take the first element ($2$) because he or she was forced by you to take the first element. After this turn the remaining array will be $[9, 2, 3, 8]$; if the third person will choose to take the first element ($9$), at your turn the remaining array will be $[2, 3, 8]$ and you will take $8$ (the last element); if the third person will choose to take the last element ($8$), at your turn the remaining array will be $[9, 2, 3]$ and you will take $9$ (the first element).
Thus, this strategy guarantees to end up with at least $8$. We can prove that there is no strategy that guarantees to end up with at least $9$. Hence, the answer is $8$.
In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $4$. | import sys
input = sys.stdin.readline
Q = int(input())
Query = []
for _ in range(Q):
N, M, K = map(int, input().split())
A = list(map(int, input().split()))
Query.append((N, M, K, A))
INF = 10**14
for N, M, K, A in Query:
ans = 0
M -= 1
if K <= M:
remain = M - K
for left in range(K + 1):
length = N - left - (K - left) - remain
score = INF
for r in range(remain + 1):
score = min(max(A[left + r], A[left + r + length - 1]), score)
ans = max(ans, score)
else:
ans = max(A[: M + 1] + A[N - M - 1 :])
print(ans) | IMPORT ASSIGN 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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Little Vlad is fond of popular computer game Bota-2. Recently, the developers announced the new add-on named Bota-3. Of course, Vlad immediately bought only to find out his computer is too old for the new game and needs to be updated.
There are n video cards in the shop, the power of the i-th video card is equal to integer value a_{i}. As Vlad wants to be sure the new game will work he wants to buy not one, but several video cards and unite their powers using the cutting-edge technology. To use this technology one of the cards is chosen as the leading one and other video cards are attached to it as secondary. For this new technology to work it's required that the power of each of the secondary video cards is divisible by the power of the leading video card. In order to achieve that the power of any secondary video card can be reduced to any integer value less or equal than the current power. However, the power of the leading video card should remain unchanged, i.e. it can't be reduced.
Vlad has an infinite amount of money so he can buy any set of video cards. Help him determine which video cards he should buy such that after picking the leading video card and may be reducing some powers of others to make them work together he will get the maximum total value of video power.
-----Input-----
The first line of the input contains a single integer n (1 β€ n β€ 200 000)Β β the number of video cards in the shop.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 200 000)Β β powers of video cards.
-----Output-----
The only line of the output should contain one integer valueΒ β the maximum possible total power of video cards working together.
-----Examples-----
Input
4
3 2 15 9
Output
27
Input
4
8 2 2 7
Output
18
-----Note-----
In the first sample, it would be optimal to buy video cards with powers 3, 15 and 9. The video card with power 3 should be chosen as the leading one and all other video cards will be compatible with it. Thus, the total power would be 3 + 15 + 9 = 27. If he buys all the video cards and pick the one with the power 2 as the leading, the powers of all other video cards should be reduced by 1, thus the total power would be 2 + 2 + 14 + 8 = 26, that is less than 27. Please note, that it's not allowed to reduce the power of the leading video card, i.e. one can't get the total power 3 + 1 + 15 + 9 = 28.
In the second sample, the optimal answer is to buy all video cards and pick the one with the power 2 as the leading. The video card with the power 7 needs it power to be reduced down to 6. The total power would be 8 + 2 + 2 + 6 = 18. | n = int(input())
s = list(map(int, input().split()))
h = max(s)
ss = [0] * (h + 1)
for i in s:
ss[i] += 1
f, x = [0] * h, 0
for j in reversed(ss):
x += j
f.append(x)
f.reverse()
res = []
for i, x in enumerate(ss):
if x:
summ, x = 0, f[i]
for j in range(i, h + 1, i):
o = f[j + i]
summ += (x - o) * j
x = o
res.append(summ)
print(max(res)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Little Vlad is fond of popular computer game Bota-2. Recently, the developers announced the new add-on named Bota-3. Of course, Vlad immediately bought only to find out his computer is too old for the new game and needs to be updated.
There are n video cards in the shop, the power of the i-th video card is equal to integer value a_{i}. As Vlad wants to be sure the new game will work he wants to buy not one, but several video cards and unite their powers using the cutting-edge technology. To use this technology one of the cards is chosen as the leading one and other video cards are attached to it as secondary. For this new technology to work it's required that the power of each of the secondary video cards is divisible by the power of the leading video card. In order to achieve that the power of any secondary video card can be reduced to any integer value less or equal than the current power. However, the power of the leading video card should remain unchanged, i.e. it can't be reduced.
Vlad has an infinite amount of money so he can buy any set of video cards. Help him determine which video cards he should buy such that after picking the leading video card and may be reducing some powers of others to make them work together he will get the maximum total value of video power.
-----Input-----
The first line of the input contains a single integer n (1 β€ n β€ 200 000)Β β the number of video cards in the shop.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 200 000)Β β powers of video cards.
-----Output-----
The only line of the output should contain one integer valueΒ β the maximum possible total power of video cards working together.
-----Examples-----
Input
4
3 2 15 9
Output
27
Input
4
8 2 2 7
Output
18
-----Note-----
In the first sample, it would be optimal to buy video cards with powers 3, 15 and 9. The video card with power 3 should be chosen as the leading one and all other video cards will be compatible with it. Thus, the total power would be 3 + 15 + 9 = 27. If he buys all the video cards and pick the one with the power 2 as the leading, the powers of all other video cards should be reduced by 1, thus the total power would be 2 + 2 + 14 + 8 = 26, that is less than 27. Please note, that it's not allowed to reduce the power of the leading video card, i.e. one can't get the total power 3 + 1 + 15 + 9 = 28.
In the second sample, the optimal answer is to buy all video cards and pick the one with the power 2 as the leading. The video card with the power 7 needs it power to be reduced down to 6. The total power would be 8 + 2 + 2 + 6 = 18. | n = int(input())
l = list(map(int, input().split()))
l1 = [0] * 200001
for i in l:
l1[i] += 1
for i in range(1, 200001):
l1[i] += l1[i - 1]
m = 0
l = list(set(l))
for i in l:
s = 0
for j in range(i, 200001, i):
s += (l1[min(200000, i + j - 1)] - l1[j - 1]) * j
if s > m:
m = s
print(m) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Little Vlad is fond of popular computer game Bota-2. Recently, the developers announced the new add-on named Bota-3. Of course, Vlad immediately bought only to find out his computer is too old for the new game and needs to be updated.
There are n video cards in the shop, the power of the i-th video card is equal to integer value a_{i}. As Vlad wants to be sure the new game will work he wants to buy not one, but several video cards and unite their powers using the cutting-edge technology. To use this technology one of the cards is chosen as the leading one and other video cards are attached to it as secondary. For this new technology to work it's required that the power of each of the secondary video cards is divisible by the power of the leading video card. In order to achieve that the power of any secondary video card can be reduced to any integer value less or equal than the current power. However, the power of the leading video card should remain unchanged, i.e. it can't be reduced.
Vlad has an infinite amount of money so he can buy any set of video cards. Help him determine which video cards he should buy such that after picking the leading video card and may be reducing some powers of others to make them work together he will get the maximum total value of video power.
-----Input-----
The first line of the input contains a single integer n (1 β€ n β€ 200 000)Β β the number of video cards in the shop.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 200 000)Β β powers of video cards.
-----Output-----
The only line of the output should contain one integer valueΒ β the maximum possible total power of video cards working together.
-----Examples-----
Input
4
3 2 15 9
Output
27
Input
4
8 2 2 7
Output
18
-----Note-----
In the first sample, it would be optimal to buy video cards with powers 3, 15 and 9. The video card with power 3 should be chosen as the leading one and all other video cards will be compatible with it. Thus, the total power would be 3 + 15 + 9 = 27. If he buys all the video cards and pick the one with the power 2 as the leading, the powers of all other video cards should be reduced by 1, thus the total power would be 2 + 2 + 14 + 8 = 26, that is less than 27. Please note, that it's not allowed to reduce the power of the leading video card, i.e. one can't get the total power 3 + 1 + 15 + 9 = 28.
In the second sample, the optimal answer is to buy all video cards and pick the one with the power 2 as the leading. The video card with the power 7 needs it power to be reduced down to 6. The total power would be 8 + 2 + 2 + 6 = 18. | N = 200005
n = int(input())
A = input().split()
A = list(map(lambda x: int(x), A))
pref = [0] * N
for a in A:
pref[a] += 1
for i in range(1, N):
pref[i] += pref[i - 1]
ans = 0
for i in range(1, N):
if pref[i] != pref[i - 1]:
suma = 0
for j in range(i, N, i):
suma += j * (pref[min(N - 1, j + i - 1)] - pref[j - 1])
ans = max(ans, suma)
print(ans) | ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Little Vlad is fond of popular computer game Bota-2. Recently, the developers announced the new add-on named Bota-3. Of course, Vlad immediately bought only to find out his computer is too old for the new game and needs to be updated.
There are n video cards in the shop, the power of the i-th video card is equal to integer value a_{i}. As Vlad wants to be sure the new game will work he wants to buy not one, but several video cards and unite their powers using the cutting-edge technology. To use this technology one of the cards is chosen as the leading one and other video cards are attached to it as secondary. For this new technology to work it's required that the power of each of the secondary video cards is divisible by the power of the leading video card. In order to achieve that the power of any secondary video card can be reduced to any integer value less or equal than the current power. However, the power of the leading video card should remain unchanged, i.e. it can't be reduced.
Vlad has an infinite amount of money so he can buy any set of video cards. Help him determine which video cards he should buy such that after picking the leading video card and may be reducing some powers of others to make them work together he will get the maximum total value of video power.
-----Input-----
The first line of the input contains a single integer n (1 β€ n β€ 200 000)Β β the number of video cards in the shop.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 200 000)Β β powers of video cards.
-----Output-----
The only line of the output should contain one integer valueΒ β the maximum possible total power of video cards working together.
-----Examples-----
Input
4
3 2 15 9
Output
27
Input
4
8 2 2 7
Output
18
-----Note-----
In the first sample, it would be optimal to buy video cards with powers 3, 15 and 9. The video card with power 3 should be chosen as the leading one and all other video cards will be compatible with it. Thus, the total power would be 3 + 15 + 9 = 27. If he buys all the video cards and pick the one with the power 2 as the leading, the powers of all other video cards should be reduced by 1, thus the total power would be 2 + 2 + 14 + 8 = 26, that is less than 27. Please note, that it's not allowed to reduce the power of the leading video card, i.e. one can't get the total power 3 + 1 + 15 + 9 = 28.
In the second sample, the optimal answer is to buy all video cards and pick the one with the power 2 as the leading. The video card with the power 7 needs it power to be reduced down to 6. The total power would be 8 + 2 + 2 + 6 = 18. | n = int(input())
a = list(map(int, input().split()))
N = 2 * 10**5
cnt = [(0) for i in range(N + 1)]
S = [(0) for i in range(N + 1)]
for x in a:
cnt[x] += 1
S[x] += x
for i in range(1, N + 1):
cnt[i] += cnt[i - 1]
S[i] += S[i - 1]
ans = 10**18
used = [(0) for i in range(N + 1)]
for i in a:
if used[i]:
continue
used[i] = 1
prv, cur = 0, 0
di = list(range(i, N + 1, i))
di.append(N + 1)
for x in di:
l, r = prv + 1, x - 1
cur += S[r] - S[l - 1]
cur -= prv * (cnt[r] - cnt[l - 1])
prv = x
ans = min(ans, cur)
print(sum(a) - ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR |
Little Vlad is fond of popular computer game Bota-2. Recently, the developers announced the new add-on named Bota-3. Of course, Vlad immediately bought only to find out his computer is too old for the new game and needs to be updated.
There are n video cards in the shop, the power of the i-th video card is equal to integer value a_{i}. As Vlad wants to be sure the new game will work he wants to buy not one, but several video cards and unite their powers using the cutting-edge technology. To use this technology one of the cards is chosen as the leading one and other video cards are attached to it as secondary. For this new technology to work it's required that the power of each of the secondary video cards is divisible by the power of the leading video card. In order to achieve that the power of any secondary video card can be reduced to any integer value less or equal than the current power. However, the power of the leading video card should remain unchanged, i.e. it can't be reduced.
Vlad has an infinite amount of money so he can buy any set of video cards. Help him determine which video cards he should buy such that after picking the leading video card and may be reducing some powers of others to make them work together he will get the maximum total value of video power.
-----Input-----
The first line of the input contains a single integer n (1 β€ n β€ 200 000)Β β the number of video cards in the shop.
The second line contains n integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 200 000)Β β powers of video cards.
-----Output-----
The only line of the output should contain one integer valueΒ β the maximum possible total power of video cards working together.
-----Examples-----
Input
4
3 2 15 9
Output
27
Input
4
8 2 2 7
Output
18
-----Note-----
In the first sample, it would be optimal to buy video cards with powers 3, 15 and 9. The video card with power 3 should be chosen as the leading one and all other video cards will be compatible with it. Thus, the total power would be 3 + 15 + 9 = 27. If he buys all the video cards and pick the one with the power 2 as the leading, the powers of all other video cards should be reduced by 1, thus the total power would be 2 + 2 + 14 + 8 = 26, that is less than 27. Please note, that it's not allowed to reduce the power of the leading video card, i.e. one can't get the total power 3 + 1 + 15 + 9 = 28.
In the second sample, the optimal answer is to buy all video cards and pick the one with the power 2 as the leading. The video card with the power 7 needs it power to be reduced down to 6. The total power would be 8 + 2 + 2 + 6 = 18. | def main():
n = int(input())
aa = list(map(int, input().split()))
aa.sort()
lim = aa[-1] + 1
cnt, a = [0] * lim, aa[0] - 1
for i, b in zip(range(n, -1, -1), aa):
if a != b:
cnt[a + 1 : b + 1] = [i] * (b - a)
a = b
avail, res = [True] * lim, []
for i, a in enumerate(aa):
if avail[a]:
avail[a] = False
res.append(a * sum(cnt[a::a]))
print(max(res))
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP LIST VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
Example:
Input: [1,1,2]
Output:
[
[1,1,2],
[1,2,1],
[2,1,1]
] | class Solution:
def permuteUnique(self, nums):
res = []
nums.sort()
def swap(a, i, j):
temp = a[i]
a[i] = a[j]
a[j] = temp
def helper(index, path):
if index == len(nums) - 1:
res.append(path.copy())
for i in range(index, len(nums)):
if i != index and path[i] == path[index]:
continue
swap(path, index, i)
helper(index + 1, path.copy())
helper(0, nums)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR RETURN VAR |
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
Example:
Input: [1,1,2]
Output:
[
[1,1,2],
[1,2,1],
[2,1,1]
] | class Solution:
def permuteUnique(self, nums):
res = []
nums.sort()
self.dfs(nums, [], res, [False] * len(nums))
return res
def dfs(self, nums, path, res, used):
if len(path) == len(nums):
res.append(path)
else:
for i in range(len(nums)):
if used[i] or i > 0 and nums[i] == nums[i - 1] and not used[i - 1]:
continue
used[i] = True
self.dfs(nums, path + [nums[i]], res, used)
used[i] = False | CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR LIST VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR LIST VAR VAR VAR VAR ASSIGN VAR VAR NUMBER |
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
Example:
Input: [1,1,2]
Output:
[
[1,1,2],
[1,2,1],
[2,1,1]
] | class Solution:
def permuteUnique(self, nums):
if not nums:
return []
nums.sort()
n = len(nums)
res = [nums[:]]
i = n - 1
while i > 0:
if nums[i - 1] < nums[i]:
j = n - 1
while nums[j] <= nums[i - 1]:
j -= 1
nums[i - 1], nums[j] = nums[j], nums[i - 1]
nums[i:] = sorted(nums[i:])
res.append(nums[:])
i = n - 1
else:
i -= 1
return res | CLASS_DEF FUNC_DEF IF VAR RETURN LIST EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR |
Given a collection of numbers that might contain duplicates, return all possible unique permutations.
Example:
Input: [1,1,2]
Output:
[
[1,1,2],
[1,2,1],
[2,1,1]
] | class Solution:
def permuteUnique(self, nums):
def dfs(nums):
if not nums:
return [[]]
dic = set()
new = []
for i in range(len(nums)):
if nums[i] not in dic:
dic.add(nums[i])
new += [
([nums[i]] + item) for item in dfs(nums[:i] + nums[i + 1 :])
]
return new
return dfs(nums) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN LIST LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP LIST VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Given n words w[1..n], which originate from the same stem (e.g. grace, graceful, disgraceful, gracefully), we are interested in the original stem. To simplify the problem, we define the stem as the longest consecutive substring that occurs in all the n words. If there are ties, we will choose the smallest one in the alphabetical (lexicographic) order.
------ Input ------
The first line contains an integer T denoting the total number of test cases.
In each test cases, the first line contains an integer n denoting the number of words. In the second line, n words w[1..n] consisting of lower case characters are given as a single space-spearated list.
------ Output ------
For each test case, output the stem in a new line.
------ Constraints ------
$1 β€ T β€ 10$
$1 β€ n β€ 10$
$1 β€ |w[i]| β€ 20$
----- Sample Input 1 ------
1
4
grace graceful disgraceful gracefully
----- Sample Output 1 ------
grace
----- explanation 1 ------
The stem is grace. | T = int(input())
for i in range(T):
n = int(input())
w, l = [], []
S = input()
w = S.split()
for j in w:
l.append(len(j))
ppe = w[l.index(min(l))]
w.remove(ppe)
a, b = 0, len(ppe)
ppeTemp = ""
while a != len(ppe):
test = True
for k in w:
if not ppe[a:b] in k:
test = False
break
if test and len(ppe[a:b]) > len(ppeTemp):
ppeTemp = ppe[a:b]
elif b != a:
b = b - 1
else:
a = a + 1
b = len(ppe)
print(ppeTemp) | 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 LIST LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR STRING WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Given n words w[1..n], which originate from the same stem (e.g. grace, graceful, disgraceful, gracefully), we are interested in the original stem. To simplify the problem, we define the stem as the longest consecutive substring that occurs in all the n words. If there are ties, we will choose the smallest one in the alphabetical (lexicographic) order.
------ Input ------
The first line contains an integer T denoting the total number of test cases.
In each test cases, the first line contains an integer n denoting the number of words. In the second line, n words w[1..n] consisting of lower case characters are given as a single space-spearated list.
------ Output ------
For each test case, output the stem in a new line.
------ Constraints ------
$1 β€ T β€ 10$
$1 β€ n β€ 10$
$1 β€ |w[i]| β€ 20$
----- Sample Input 1 ------
1
4
grace graceful disgraceful gracefully
----- Sample Output 1 ------
grace
----- explanation 1 ------
The stem is grace. | for t in range(int(input())):
n = int(input())
a = list(input().split(" "))
d = ""
count = 0
max = 0
mx = ""
for i in range(len(a[0])):
count = 0
for k in range(i, len(a[0])):
d += a[0][k]
count = 0
for j in a[1:]:
if d in j:
count += 1
if count == n - 1:
if len(d) > max:
max = len(d)
mx = d
elif len(d) == max:
mx = min(d, mx)
else:
d = ""
print(mx) | 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 FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Given n words w[1..n], which originate from the same stem (e.g. grace, graceful, disgraceful, gracefully), we are interested in the original stem. To simplify the problem, we define the stem as the longest consecutive substring that occurs in all the n words. If there are ties, we will choose the smallest one in the alphabetical (lexicographic) order.
------ Input ------
The first line contains an integer T denoting the total number of test cases.
In each test cases, the first line contains an integer n denoting the number of words. In the second line, n words w[1..n] consisting of lower case characters are given as a single space-spearated list.
------ Output ------
For each test case, output the stem in a new line.
------ Constraints ------
$1 β€ T β€ 10$
$1 β€ n β€ 10$
$1 β€ |w[i]| β€ 20$
----- Sample Input 1 ------
1
4
grace graceful disgraceful gracefully
----- Sample Output 1 ------
grace
----- explanation 1 ------
The stem is grace. | import sys
T = sys.stdin.readline()
for i in range(int(T)):
root = ""
n = sys.stdin.readline()
words = sys.stdin.readline().split()
for j in range(len(words[0])):
for k in range(len(words[0])):
pot_root = words[0][j : k + 1]
r = True
for item in words:
if pot_root not in item:
r = False
if r == True and len(pot_root) >= len(root):
if len(root) == len(pot_root):
if pot_root < root:
root = pot_root
else:
root = pot_root
print(root) | IMPORT ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Given n words w[1..n], which originate from the same stem (e.g. grace, graceful, disgraceful, gracefully), we are interested in the original stem. To simplify the problem, we define the stem as the longest consecutive substring that occurs in all the n words. If there are ties, we will choose the smallest one in the alphabetical (lexicographic) order.
------ Input ------
The first line contains an integer T denoting the total number of test cases.
In each test cases, the first line contains an integer n denoting the number of words. In the second line, n words w[1..n] consisting of lower case characters are given as a single space-spearated list.
------ Output ------
For each test case, output the stem in a new line.
------ Constraints ------
$1 β€ T β€ 10$
$1 β€ n β€ 10$
$1 β€ |w[i]| β€ 20$
----- Sample Input 1 ------
1
4
grace graceful disgraceful gracefully
----- Sample Output 1 ------
grace
----- explanation 1 ------
The stem is grace. | def smallest_word(arr, n):
w = arr[0]
length = len(arr[0])
for word in arr:
if len(word) < length:
w = word
length = len(word)
return w
def solve(arr, n):
w = smallest_word(arr, n)
length = 0
s = ""
for i in range(0, len(w)):
for j in range(i + 1, len(w) + 1):
substring = w[i:j]
if j - i < length:
continue
flag = True
for word in arr:
if substring not in word:
flag = False
break
if flag and (j - i > len(s) or substring < s and len(substring) == len(s)):
s = substring
print(s)
t = int(input())
for i in range(t):
n = int(input())
arr = input().split()
solve(arr, n) | FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Given n words w[1..n], which originate from the same stem (e.g. grace, graceful, disgraceful, gracefully), we are interested in the original stem. To simplify the problem, we define the stem as the longest consecutive substring that occurs in all the n words. If there are ties, we will choose the smallest one in the alphabetical (lexicographic) order.
------ Input ------
The first line contains an integer T denoting the total number of test cases.
In each test cases, the first line contains an integer n denoting the number of words. In the second line, n words w[1..n] consisting of lower case characters are given as a single space-spearated list.
------ Output ------
For each test case, output the stem in a new line.
------ Constraints ------
$1 β€ T β€ 10$
$1 β€ n β€ 10$
$1 β€ |w[i]| β€ 20$
----- Sample Input 1 ------
1
4
grace graceful disgraceful gracefully
----- Sample Output 1 ------
grace
----- explanation 1 ------
The stem is grace. | for _ in range(int(input())):
n = int(input())
z = input().strip().split()
w = list(z[:n])
wlen = [len(aw) for aw in w]
mlen = min(wlen)
ref = w[wlen.index(mlen)]
subs = []
for pos in range(mlen):
if all(ref[pos] in aw for aw in w):
subs.append(pos)
if len(subs) == 0:
print()
continue
tlen = 2
while True:
nsubs = []
for pos in subs:
if pos + 1 in subs:
if all(ref[pos : pos + tlen] in aw for aw in w):
nsubs.append(pos)
if len(nsubs) == 0:
break
subs = nsubs
tlen += 1
tlen -= 1
ans = [ref[pos : pos + tlen] for pos in subs]
ans.sort()
print(ans[0]) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR LIST FOR VAR VAR IF BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER |
Read problems statements in Mandarin Chinese and Russian as well.
Given n words w[1..n], which originate from the same stem (e.g. grace, graceful, disgraceful, gracefully), we are interested in the original stem. To simplify the problem, we define the stem as the longest consecutive substring that occurs in all the n words. If there are ties, we will choose the smallest one in the alphabetical (lexicographic) order.
------ Input ------
The first line contains an integer T denoting the total number of test cases.
In each test cases, the first line contains an integer n denoting the number of words. In the second line, n words w[1..n] consisting of lower case characters are given as a single space-spearated list.
------ Output ------
For each test case, output the stem in a new line.
------ Constraints ------
$1 β€ T β€ 10$
$1 β€ n β€ 10$
$1 β€ |w[i]| β€ 20$
----- Sample Input 1 ------
1
4
grace graceful disgraceful gracefully
----- Sample Output 1 ------
grace
----- explanation 1 ------
The stem is grace. | t = int(input())
for tt in range(t):
n = int(input())
s = input().split()
r = ""
for i in range(len(s[0])):
for j in range(i + 1, len(s[0]) + 1):
t = s[0][i:j]
for p in s:
if p.count(t) == 0:
t = ""
if (len(t), r) > (len(r), t):
r = t
print(r) | 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 STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Given n words w[1..n], which originate from the same stem (e.g. grace, graceful, disgraceful, gracefully), we are interested in the original stem. To simplify the problem, we define the stem as the longest consecutive substring that occurs in all the n words. If there are ties, we will choose the smallest one in the alphabetical (lexicographic) order.
------ Input ------
The first line contains an integer T denoting the total number of test cases.
In each test cases, the first line contains an integer n denoting the number of words. In the second line, n words w[1..n] consisting of lower case characters are given as a single space-spearated list.
------ Output ------
For each test case, output the stem in a new line.
------ Constraints ------
$1 β€ T β€ 10$
$1 β€ n β€ 10$
$1 β€ |w[i]| β€ 20$
----- Sample Input 1 ------
1
4
grace graceful disgraceful gracefully
----- Sample Output 1 ------
grace
----- explanation 1 ------
The stem is grace. | T = int(input())
for t in range(T):
n = int(input())
words = input().split()
L = len(words[0])
ans = ""
for i in range(L):
for j in range(L):
s = words[0][j : i + 1]
for word in words:
if s not in word:
break
else:
if len(s) > len(ans) or len(s) == len(ans) and s < ans:
ans = s
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 VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Given n words w[1..n], which originate from the same stem (e.g. grace, graceful, disgraceful, gracefully), we are interested in the original stem. To simplify the problem, we define the stem as the longest consecutive substring that occurs in all the n words. If there are ties, we will choose the smallest one in the alphabetical (lexicographic) order.
------ Input ------
The first line contains an integer T denoting the total number of test cases.
In each test cases, the first line contains an integer n denoting the number of words. In the second line, n words w[1..n] consisting of lower case characters are given as a single space-spearated list.
------ Output ------
For each test case, output the stem in a new line.
------ Constraints ------
$1 β€ T β€ 10$
$1 β€ n β€ 10$
$1 β€ |w[i]| β€ 20$
----- Sample Input 1 ------
1
4
grace graceful disgraceful gracefully
----- Sample Output 1 ------
grace
----- explanation 1 ------
The stem is grace. | import sys
t = int(sys.stdin.readline())
for it in range(t):
n = int(sys.stdin.readline())
strs = sys.stdin.readline().split(" ")
strs.sort(key=len)
start = mxlen = 0
stop = 1
mxterm = ""
while start < len(strs[0]) and stop < len(strs[0]) + 1:
temp = strs[0][start:stop]
valid = True
for ph in strs[1:]:
if temp not in ph:
valid = False
break
if not valid:
start += 1
if stop <= start:
stop = start + 1
else:
if mxlen < len(temp):
mxlen = len(temp)
mxterm = temp
stop += 1
print(mxterm) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Given n words w[1..n], which originate from the same stem (e.g. grace, graceful, disgraceful, gracefully), we are interested in the original stem. To simplify the problem, we define the stem as the longest consecutive substring that occurs in all the n words. If there are ties, we will choose the smallest one in the alphabetical (lexicographic) order.
------ Input ------
The first line contains an integer T denoting the total number of test cases.
In each test cases, the first line contains an integer n denoting the number of words. In the second line, n words w[1..n] consisting of lower case characters are given as a single space-spearated list.
------ Output ------
For each test case, output the stem in a new line.
------ Constraints ------
$1 β€ T β€ 10$
$1 β€ n β€ 10$
$1 β€ |w[i]| β€ 20$
----- Sample Input 1 ------
1
4
grace graceful disgraceful gracefully
----- Sample Output 1 ------
grace
----- explanation 1 ------
The stem is grace. | def producesubstrings(s, x):
subs = []
for i in range(0, len(s) - x + 1):
subs.append(s[i : x + i])
return subs
t = int(input())
while t:
answer = []
t = t - 1
n = int(input())
s = input()
list = s.split()
min = len(list[0])
smalleststring = list[0]
smallstrings = []
for s in list:
if len(s) < min:
min = len(s)
smalleststring = s
for s in list:
if len(s) == len(smalleststring):
smallstrings.append(s)
for ss in smallstrings:
for i in range(len(ss), 0, -1):
flag = 0
subs = producesubstrings(ss, i)
for s in subs:
flag = 0
for x in list:
if not s in x:
flag = 1
break
if flag == 0:
answer.append(s)
if flag == 0:
break
max = len(answer[0])
for a in answer:
if len(a) > max:
max = len(a)
answer1 = []
for a in answer:
if len(a) == max:
answer1.append(a)
answer1.sort()
print(answer1[0]) | FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER |
Read problems statements in Mandarin Chinese and Russian as well.
Given n words w[1..n], which originate from the same stem (e.g. grace, graceful, disgraceful, gracefully), we are interested in the original stem. To simplify the problem, we define the stem as the longest consecutive substring that occurs in all the n words. If there are ties, we will choose the smallest one in the alphabetical (lexicographic) order.
------ Input ------
The first line contains an integer T denoting the total number of test cases.
In each test cases, the first line contains an integer n denoting the number of words. In the second line, n words w[1..n] consisting of lower case characters are given as a single space-spearated list.
------ Output ------
For each test case, output the stem in a new line.
------ Constraints ------
$1 β€ T β€ 10$
$1 β€ n β€ 10$
$1 β€ |w[i]| β€ 20$
----- Sample Input 1 ------
1
4
grace graceful disgraceful gracefully
----- Sample Output 1 ------
grace
----- explanation 1 ------
The stem is grace. | for _ in range(int(input())):
n = int(input())
s = [s for s in input().split()]
s.sort(key=len)
s1 = s[0]
l = []
for i in range(0, len(s1)):
for j in range(i + 1, len(s1) + 1):
l.append(s1[i:j])
ans = ""
for ele in l:
for words in s:
if not ele in words:
break
else:
if len(ele) > len(ans):
ans = ele
if len(ele) == len(ans):
if ele < ans:
ans = ele
print(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 FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR STRING FOR VAR VAR FOR VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Given n words w[1..n], which originate from the same stem (e.g. grace, graceful, disgraceful, gracefully), we are interested in the original stem. To simplify the problem, we define the stem as the longest consecutive substring that occurs in all the n words. If there are ties, we will choose the smallest one in the alphabetical (lexicographic) order.
------ Input ------
The first line contains an integer T denoting the total number of test cases.
In each test cases, the first line contains an integer n denoting the number of words. In the second line, n words w[1..n] consisting of lower case characters are given as a single space-spearated list.
------ Output ------
For each test case, output the stem in a new line.
------ Constraints ------
$1 β€ T β€ 10$
$1 β€ n β€ 10$
$1 β€ |w[i]| β€ 20$
----- Sample Input 1 ------
1
4
grace graceful disgraceful gracefully
----- Sample Output 1 ------
grace
----- explanation 1 ------
The stem is grace. | test = int(input())
for i in range(test):
n = int(input())
string = input().split(" ")
string.sort(key=len)
subst = ""
for j in range(len(string[0])):
for k in range(j + 1, len(string[0]) + 1):
word = string[0][j:k]
value = True
for l in string[1:]:
if word not in l:
value = False
break
if value:
if len(word) > len(subst):
subst = word
elif len(word) == len(subst) and subst > word:
subst = word
print(subst) | 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 STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Given n words w[1..n], which originate from the same stem (e.g. grace, graceful, disgraceful, gracefully), we are interested in the original stem. To simplify the problem, we define the stem as the longest consecutive substring that occurs in all the n words. If there are ties, we will choose the smallest one in the alphabetical (lexicographic) order.
------ Input ------
The first line contains an integer T denoting the total number of test cases.
In each test cases, the first line contains an integer n denoting the number of words. In the second line, n words w[1..n] consisting of lower case characters are given as a single space-spearated list.
------ Output ------
For each test case, output the stem in a new line.
------ Constraints ------
$1 β€ T β€ 10$
$1 β€ n β€ 10$
$1 β€ |w[i]| β€ 20$
----- Sample Input 1 ------
1
4
grace graceful disgraceful gracefully
----- Sample Output 1 ------
grace
----- explanation 1 ------
The stem is grace. | import sys
T = int(sys.stdin.readline())
for i in range(T):
n = int(sys.stdin.readline())
s = sys.stdin.readline().strip()
a = s.split()
min_string = min(a, key=len)
length = 0
sub_str = ""
length_sub_str = 0
longest_sub_str = ""
for i in range(0, len(min_string)):
for j in range(i + 1, len(min_string) + 1):
sub_str = min_string[i:j]
flag = False
for ele in a:
if sub_str in ele:
flag = True
else:
flag = False
break
if flag == True:
if length_sub_str < len(sub_str):
length_sub_str = len(sub_str)
longest_sub_str = sub_str
elif length_sub_str == len(sub_str):
if longest_sub_str > sub_str:
longest_sub_str = sub_str
print(longest_sub_str) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Given n words w[1..n], which originate from the same stem (e.g. grace, graceful, disgraceful, gracefully), we are interested in the original stem. To simplify the problem, we define the stem as the longest consecutive substring that occurs in all the n words. If there are ties, we will choose the smallest one in the alphabetical (lexicographic) order.
------ Input ------
The first line contains an integer T denoting the total number of test cases.
In each test cases, the first line contains an integer n denoting the number of words. In the second line, n words w[1..n] consisting of lower case characters are given as a single space-spearated list.
------ Output ------
For each test case, output the stem in a new line.
------ Constraints ------
$1 β€ T β€ 10$
$1 β€ n β€ 10$
$1 β€ |w[i]| β€ 20$
----- Sample Input 1 ------
1
4
grace graceful disgraceful gracefully
----- Sample Output 1 ------
grace
----- explanation 1 ------
The stem is grace. | t = int(input())
for i in range(t):
n = int(input())
l = list(map(str, input().split()))
for i in range(n):
y = l[i]
x = list()
for j in range(len(y)):
for k in range(j + 1, len(y)):
x.append(y[j : k + 1])
if i == 0:
xl = set(x)
else:
ans = set(x)
xl = xl.intersection(ans)
xl = list(xl)
xl.sort()
a = 0
s = ""
for i in xl:
if len(i) > a:
a = len(i)
s = i
print(s) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Given n words w[1..n], which originate from the same stem (e.g. grace, graceful, disgraceful, gracefully), we are interested in the original stem. To simplify the problem, we define the stem as the longest consecutive substring that occurs in all the n words. If there are ties, we will choose the smallest one in the alphabetical (lexicographic) order.
------ Input ------
The first line contains an integer T denoting the total number of test cases.
In each test cases, the first line contains an integer n denoting the number of words. In the second line, n words w[1..n] consisting of lower case characters are given as a single space-spearated list.
------ Output ------
For each test case, output the stem in a new line.
------ Constraints ------
$1 β€ T β€ 10$
$1 β€ n β€ 10$
$1 β€ |w[i]| β€ 20$
----- Sample Input 1 ------
1
4
grace graceful disgraceful gracefully
----- Sample Output 1 ------
grace
----- explanation 1 ------
The stem is grace. | def check(x, l):
count = 0
for i in range(len(l)):
if x in l[i]:
count += 1
return count
for j in range(int(input())):
n = int(input())
l = input().split(" ")
min_len = 30
index_min = 0
for i in l:
if len(i) < min_len:
min_len = len(i)
index_min = l.index(i)
x = l[index_min]
i = 0
j = len(x) - 1
m = n = 0
p = 0
while i < len(x):
j = len(x)
while j > -1:
c = check(x[i:j], l)
if c == len(l):
if p < len(x[i:j]):
p = len(x[i:j])
m = i
n = j
j -= 1
i += 1
print(x[m:n]) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR |
Read problems statements in Mandarin Chinese and Russian as well.
Given n words w[1..n], which originate from the same stem (e.g. grace, graceful, disgraceful, gracefully), we are interested in the original stem. To simplify the problem, we define the stem as the longest consecutive substring that occurs in all the n words. If there are ties, we will choose the smallest one in the alphabetical (lexicographic) order.
------ Input ------
The first line contains an integer T denoting the total number of test cases.
In each test cases, the first line contains an integer n denoting the number of words. In the second line, n words w[1..n] consisting of lower case characters are given as a single space-spearated list.
------ Output ------
For each test case, output the stem in a new line.
------ Constraints ------
$1 β€ T β€ 10$
$1 β€ n β€ 10$
$1 β€ |w[i]| β€ 20$
----- Sample Input 1 ------
1
4
grace graceful disgraceful gracefully
----- Sample Output 1 ------
grace
----- explanation 1 ------
The stem is grace. | def rec(L, index, common):
ans = []
for i in range(0, len(common)):
if L[index].find(common[i], 0, len(L[index])) > -1:
ans.append(common[i])
return ans
t = int(input())
for deju in range(t):
L = []
n = int(input())
L = input().split()
if n == 1:
print(L[0])
continue
common = []
for i in range(0, len(L[0])):
for j in range(i, len(L[0])):
if L[1].find(L[0][i : j + 1], 0, len(L[1])) > -1:
common.append(L[0][i : j + 1])
for i in range(2, n):
common = rec(L, i, common)
maxi = 0
ans = []
for i in range(len(common)):
maxi = max(maxi, len(common[i]))
for i in range(len(common)):
if len(common[i]) == maxi:
ans.append(common[i])
ans.sort()
print(ans[0]) | FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER |
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
While studying representation of numbers in various bases, Chef discovered an interesting properties of some positive numbers, which Chef believed are somewhat easy to memorize. Chef called such positive numbers easy numbers.
These numbers had an interesting property: Let the base Chef is currently studying be b. Represent the number in base b (without leading zeros). For each prefix of length k (from 1 to number of digits in number in the base), the number formed by considering the digits of the prefix in base b is divisible by k.
For example, let base Chef is studying be 2.
1 is an easy number as its prefix of length 1 (i.e. 1) is divisible by 1.
2 is also an easy number, as its prefix of length 1 (i.e. 1) is divisible by 1 and the prefix of length 2 (i.e. 2, represented as 10 in base 2) is also divisible by 2.
3 is not an easy number, as its prefix of length 2 (i.e. 3, represented as 11 in base 2) is not divisible by 2.
Now Chef finds these numbers easy and thinks that following question related to easy numbers will be easy for you too. He asks you to find the number of easy numbers represented in base b and consisting of exactly d digits. As the answer could be quite large, output it modulo 10^{9} + 7.
------ Input ------
First line of the input contains a single integer T denoting the number of test cases.
For each test case, there will be a single line containing two space separated integers b, d, denoting the base and number of digits respectively.
------ Output ------
For each test case, output a single integer denoting the number of easy numbers in base b consisting of d digits, modulo 10^{9} + 7.
------ Constraints ------
$1 β€ T β€ 100$
$2 β€ b β€ 16$
$1 β€ d β€ 10^{9}$
----- Sample Input 1 ------
2
2 2
10 3
----- Sample Output 1 ------
1
150
----- explanation 1 ------
Example case 1. There are 2 numbers in base 2 consisting of 2 digits. Those are 2 (represented as 10 in base 2) and 3 (represented as 11 in base 2). 2 is an easy number, whereas 3 is not (see the explanation about it in the problem statement). Hence the number of easy numbers consisting of 2 digits in base 2 are 1. | def find(b):
a = [0]
if b < 2:
return []
v = [i for i in range(1, b)]
i = 2
while True:
a.append(len(v))
if i != b:
v2 = []
for x in v:
t = i - x * b % i
if t == i:
t = 0
while t < b:
v2.append(x * b + t)
t += i
if len(v2) == 0:
return a
v = []
for j in v2:
v.append(j)
else:
for j in range(len(v)):
v[j] *= b
i += 1
t = 1
t = int(input())
ans = [find(i) for i in range(15)]
ans.append(
[
0,
14,
105,
525,
1995,
5985,
15960,
34199,
64409,
107154,
171563,
233954,
296371,
342124,
368134,
368134,
368134,
324876,
281553,
222037,
171005,
122130,
84431,
54937,
34827,
20907,
12333,
7023,
3880,
2057,
1167,
564,
260,
129,
62,
20,
7,
1,
1,
]
)
ans.append(
[
0,
15,
120,
640,
2560,
8192,
21888,
50030,
100060,
178236,
285074,
414662,
554018,
681778,
779164,
831886,
831886,
782347,
701717,
591178,
473607,
361604,
263114,
183069,
121870,
78013,
48065,
28497,
16204,
8852,
4742,
2457,
1216,
565,
259,
124,
46,
21,
10,
3,
]
)
for _test_ in range(t):
b, d = map(int, input().split())
if len(ans[b]) <= d:
print(0)
else:
print(ans[b][d]) | FUNC_DEF ASSIGN VAR LIST NUMBER IF VAR NUMBER RETURN LIST ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER WHILE NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST 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 NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST 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 NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR |
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections. [Image]
Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events:
1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars.
2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections.
Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes).
-----Input-----
The first line of input contains a single integer q (1 β€ q β€ 1 000).
The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u.
1 β€ v, u β€ 10^18, v β u, 1 β€ w β€ 10^9 states for every description line.
-----Output-----
For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events.
-----Example-----
Input
7
1 3 4 30
1 4 1 2
1 3 6 8
2 4 3
1 6 1 40
2 3 7
2 2 4
Output
94
0
32
-----Note-----
In the example testcase:
Here are the intersections used: [Image] Intersections on the path are 3, 1, 2 and 4. Intersections on the path are 4, 2 and 1. Intersections on the path are only 3 and 6. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. Intersections on the path are 6, 3 and 1. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second). | q = int(input())
def full_way(u):
res = set()
while u >= 1:
res.add(u)
u //= 2
return res
def get_way(u, v):
res1 = full_way(u)
res2 = full_way(v)
m = max(res1 & res2)
res = set()
for x in res1 | res2:
if x > m:
res.add(x)
return res
d = {}
for i in range(q):
a = input().split()
if a[0] == "1":
v, u, w = list(map(int, a[1:]))
for x in get_way(u, v):
if x not in d:
d[x] = 0
d[x] += w
else:
v, u = list(map(int, a[1:]))
res = 0
for x in get_way(u, v):
if x in d:
res += d[x]
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections. [Image]
Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events:
1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars.
2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections.
Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes).
-----Input-----
The first line of input contains a single integer q (1 β€ q β€ 1 000).
The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u.
1 β€ v, u β€ 10^18, v β u, 1 β€ w β€ 10^9 states for every description line.
-----Output-----
For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events.
-----Example-----
Input
7
1 3 4 30
1 4 1 2
1 3 6 8
2 4 3
1 6 1 40
2 3 7
2 2 4
Output
94
0
32
-----Note-----
In the example testcase:
Here are the intersections used: [Image] Intersections on the path are 3, 1, 2 and 4. Intersections on the path are 4, 2 and 1. Intersections on the path are only 3 and 6. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. Intersections on the path are 6, 3 and 1. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second). | w = {}
def up(a, b, d):
while a != b:
if a < b:
a, b = b, a
if a not in w:
w[a] = 0
w[a] += d
a = a // 2
def get(a, b):
res = 0
while a != b:
if a < b:
a, b = b, a
if a in w:
res += w[a]
a = a // 2
return res
q = int(input())
for i in range(q):
a = [int(b) for b in input().split()]
if a[0] == 1:
up(a[1], a[2], a[3])
else:
print(get(a[1], a[2])) | ASSIGN VAR DICT FUNC_DEF WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER |
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections. [Image]
Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events:
1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars.
2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections.
Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes).
-----Input-----
The first line of input contains a single integer q (1 β€ q β€ 1 000).
The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u.
1 β€ v, u β€ 10^18, v β u, 1 β€ w β€ 10^9 states for every description line.
-----Output-----
For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events.
-----Example-----
Input
7
1 3 4 30
1 4 1 2
1 3 6 8
2 4 3
1 6 1 40
2 3 7
2 2 4
Output
94
0
32
-----Note-----
In the example testcase:
Here are the intersections used: [Image] Intersections on the path are 3, 1, 2 and 4. Intersections on the path are 4, 2 and 1. Intersections on the path are only 3 and 6. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. Intersections on the path are 6, 3 and 1. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second). | def UCLN(a, b):
while a != b:
if a > b:
a = a // 2
else:
b = b // 2
return a
def find_path(u, v):
ucln = UCLN(u, v)
arr1 = []
arr2 = []
while u >= ucln:
arr1.append(u)
u = u // 2
while v > ucln:
arr2.append(v)
v = v // 2
return arr1 + arr2[::-1]
p = int(input())
dic = {}
for i in range(p):
value = list(map(int, input().split()))
action = value[0]
if action == 1:
path = find_path(value[1], value[2])
for i in range(len(path) - 1):
if (
"{}{}".format(path[i], path[i + 1]) not in dic
and "{}{}".format(path[i + 1], path[i]) not in dic
):
dic["{}{}".format(path[i], path[i + 1])] = value[3]
dic["{}{}".format(path[i + 1], path[i])] = value[3]
else:
dic["{}{}".format(path[i], path[i + 1])] += value[3]
dic["{}{}".format(path[i + 1], path[i])] += value[3]
else:
total = 0
path = find_path(value[1], value[2])
for i in range(len(path) - 1):
if "{}{}".format(path[i], path[i + 1]) in dic:
total += dic["{}{}".format(path[i], path[i + 1])]
print(total) | FUNC_DEF WHILE VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL STRING VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL STRING VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR FUNC_CALL STRING VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL STRING VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL STRING VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL STRING VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections. [Image]
Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events:
1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars.
2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections.
Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes).
-----Input-----
The first line of input contains a single integer q (1 β€ q β€ 1 000).
The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u.
1 β€ v, u β€ 10^18, v β u, 1 β€ w β€ 10^9 states for every description line.
-----Output-----
For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events.
-----Example-----
Input
7
1 3 4 30
1 4 1 2
1 3 6 8
2 4 3
1 6 1 40
2 3 7
2 2 4
Output
94
0
32
-----Note-----
In the example testcase:
Here are the intersections used: [Image] Intersections on the path are 3, 1, 2 and 4. Intersections on the path are 4, 2 and 1. Intersections on the path are only 3 and 6. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. Intersections on the path are 6, 3 and 1. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second). | d = {}
def lca(x, y, w):
res = 0
while x != y:
if x < y:
x, y = y, x
d[x] = d.get(x, 0) + w
res += d[x]
x //= 2
return res
q = int(input())
while q > 0:
q -= 1
a = list(map(int, input().split()))
if a[0] == 1:
lca(a[1], a[2], a[3])
else:
print(lca(a[1], a[2], 0)) | ASSIGN VAR DICT FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER |
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections. [Image]
Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events:
1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars.
2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections.
Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes).
-----Input-----
The first line of input contains a single integer q (1 β€ q β€ 1 000).
The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u.
1 β€ v, u β€ 10^18, v β u, 1 β€ w β€ 10^9 states for every description line.
-----Output-----
For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events.
-----Example-----
Input
7
1 3 4 30
1 4 1 2
1 3 6 8
2 4 3
1 6 1 40
2 3 7
2 2 4
Output
94
0
32
-----Note-----
In the example testcase:
Here are the intersections used: [Image] Intersections on the path are 3, 1, 2 and 4. Intersections on the path are 4, 2 and 1. Intersections on the path are only 3 and 6. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. Intersections on the path are 6, 3 and 1. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second). | def path_to_root(n):
path = [n]
while n != 1:
if n % 2:
path.append((n - 1) // 2)
n = (n - 1) // 2
else:
path.append(n // 2)
n //= 2
return path
def path_beetwen(a, b):
p1 = path_to_root(a)
p2 = path_to_root(b)
l1 = len(p1)
l2 = len(p2)
x = 0
while x < l2:
if p2[x] in p1:
break
x += 1
path = p1[: p1.index(p2[x]) + 1] + p2[:x][::-1]
return path
def fee_on_path(fees, a, b):
path = path_beetwen(a, b)
total_fee = 0
for x in range(len(path) - 1):
fee = str(path[x]) + "_" + str(path[x + 1])
if fee in fees.keys():
total_fee += fees[fee]
return total_fee
def update_fees(fees, a, b, w):
path = path_beetwen(a, b)
for x in range(len(path) - 1):
fee = str(path[x]) + "_" + str(path[x + 1])
fee2 = str(path[x + 1]) + "_" + str(path[x])
if fee in fees.keys():
fees[fee] += w
else:
fees[fee] = w
if fee2 in fees.keys():
fees[fee2] += w
else:
fees[fee2] = w
class CodeforcesTask696ASolution:
def __init__(self):
self.result = ""
self.events_count = 0
self.events = []
def read_input(self):
self.events_count = int(input())
for x in range(self.events_count):
self.events.append([int(y) for y in input().split(" ")])
def process_task(self):
fees = {}
for x in range(self.events_count):
if self.events[x][0] == 1:
update_fees(
fees, self.events[x][1], self.events[x][2], self.events[x][3]
)
else:
print(fee_on_path(fees, self.events[x][1], self.events[x][2]))
def get_result(self):
return self.result
Solution = CodeforcesTask696ASolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result()) | FUNC_DEF ASSIGN VAR LIST VAR WHILE VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR STRING FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR STRING FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER STRING FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER FUNC_DEF RETURN VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections. [Image]
Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events:
1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars.
2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections.
Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes).
-----Input-----
The first line of input contains a single integer q (1 β€ q β€ 1 000).
The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u.
1 β€ v, u β€ 10^18, v β u, 1 β€ w β€ 10^9 states for every description line.
-----Output-----
For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events.
-----Example-----
Input
7
1 3 4 30
1 4 1 2
1 3 6 8
2 4 3
1 6 1 40
2 3 7
2 2 4
Output
94
0
32
-----Note-----
In the example testcase:
Here are the intersections used: [Image] Intersections on the path are 3, 1, 2 and 4. Intersections on the path are 4, 2 and 1. Intersections on the path are only 3 and 6. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. Intersections on the path are 6, 3 and 1. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second). | cost, res = {}, []
t = int(input())
for i in range(t):
arr = list(map(int, input().split()))
if len(arr) == 4:
_, u, v, w = arr
while u != v:
if u > v:
cost[u] = cost.get(u, 0) + w
u //= 2
else:
cost[v] = cost.get(v, 0) + w
v //= 2
else:
_, u, v = arr
curr_res = 0
while u != v:
if u > v:
curr_res += cost.get(u, 0)
u //= 2
else:
curr_res += cost.get(v, 0)
v //= 2
res.append(str(curr_res))
print("\n".join(res)) | ASSIGN VAR VAR DICT LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections. [Image]
Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events:
1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars.
2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections.
Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes).
-----Input-----
The first line of input contains a single integer q (1 β€ q β€ 1 000).
The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u.
1 β€ v, u β€ 10^18, v β u, 1 β€ w β€ 10^9 states for every description line.
-----Output-----
For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events.
-----Example-----
Input
7
1 3 4 30
1 4 1 2
1 3 6 8
2 4 3
1 6 1 40
2 3 7
2 2 4
Output
94
0
32
-----Note-----
In the example testcase:
Here are the intersections used: [Image] Intersections on the path are 3, 1, 2 and 4. Intersections on the path are 4, 2 and 1. Intersections on the path are only 3 and 6. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. Intersections on the path are 6, 3 and 1. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second). | def find_path(x, y):
p1, p2 = [], []
while x != 0:
p1.append(x)
x = x // 2
while y != 0:
p2.append(y)
y = y // 2
p1 = p1[::-1]
p2 = p2[::-1]
for i in range(min(len(p1), len(p2))):
if p1[i] == p2[i]:
ind = i
else:
break
path = []
for i in range(ind, len(p1)):
path.append(p1[i])
path = path[::-1]
for i in range(ind + 1, len(p2)):
path.append(p2[i])
return path
q = int(input())
cost = {}
for i in range(q):
a = list(map(int, input().split()))
b = find_path(a[1], a[2])
if a[0] == 1:
w = a[-1]
for j in range(1, len(b)):
if (b[j], b[j - 1]) not in cost:
cost[b[j], b[j - 1]] = w
cost[b[j - 1], b[j]] = w
else:
cost[b[j], b[j - 1]] += w
cost[b[j - 1], b[j]] += w
else:
ans = 0
for j in range(1, len(b)):
if (b[j], b[j - 1]) in cost:
ans += cost[b[j], b[j - 1]]
print(ans) | FUNC_DEF ASSIGN VAR VAR LIST LIST WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections. [Image]
Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events:
1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars.
2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections.
Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes).
-----Input-----
The first line of input contains a single integer q (1 β€ q β€ 1 000).
The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u.
1 β€ v, u β€ 10^18, v β u, 1 β€ w β€ 10^9 states for every description line.
-----Output-----
For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events.
-----Example-----
Input
7
1 3 4 30
1 4 1 2
1 3 6 8
2 4 3
1 6 1 40
2 3 7
2 2 4
Output
94
0
32
-----Note-----
In the example testcase:
Here are the intersections used: [Image] Intersections on the path are 3, 1, 2 and 4. Intersections on the path are 4, 2 and 1. Intersections on the path are only 3 and 6. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. Intersections on the path are 6, 3 and 1. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second). | def add_edges(s1, s2, w):
global edges
i = 0
while i < len(s1) and i < len(s2) and s1[i] == s2[i]:
i += 1
j = len(s1)
while j > i:
if s1[:j] in edges:
edges[s1[:j]] += w
else:
edges[s1[:j]] = w
j -= 1
j = len(s2)
while j > i:
if s2[:j] in edges:
edges[s2[:j]] += w
else:
edges[s2[:j]] = w
j -= 1
def way(s1, s2):
global edges
i = 0
while i < len(s1) and i < len(s2) and s1[i] == s2[i]:
i += 1
w = 0
j = len(s1)
while j > i:
if s1[:j] in edges:
w += edges[s1[:j]]
j -= 1
j = len(s2)
while j > i:
if s2[:j] in edges:
w += edges[s2[:j]]
j -= 1
return w
q = int(input())
edges = dict()
for i in range(q):
l = list(map(int, input().split()))
if l[0] == 1:
l[1] = bin(l[1])
l[1] = l[1][2:]
l[2] = bin(l[2])
l[2] = l[2][2:]
add_edges(l[1], l[2], l[3])
if l[0] == 2:
l[1] = bin(l[1])
l[1] = l[1][2:]
l[2] = bin(l[2])
l[2] = l[2][2:]
print(way(l[1], l[2])) | FUNC_DEF ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER |
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections i and 2i and another road between i and 2i + 1 for every positive integer i. You can clearly see that there exists a unique shortest path between any two intersections. [Image]
Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will q consecutive events happen soon. There are two types of events:
1. Government makes a new rule. A rule can be denoted by integers v, u and w. As the result of this action, the passing fee of all roads on the shortest path from u to v increases by w dollars.
2. Barney starts moving from some intersection v and goes to intersection u where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections.
Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes).
-----Input-----
The first line of input contains a single integer q (1 β€ q β€ 1 000).
The next q lines contain the information about the events in chronological order. Each event is described in form 1 v u w if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from u to v by w dollars, or in form 2 v u if it's an event when Barnie goes to cuddle from the intersection v to the intersection u.
1 β€ v, u β€ 10^18, v β u, 1 β€ w β€ 10^9 states for every description line.
-----Output-----
For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events.
-----Example-----
Input
7
1 3 4 30
1 4 1 2
1 3 6 8
2 4 3
1 6 1 40
2 3 7
2 2 4
Output
94
0
32
-----Note-----
In the example testcase:
Here are the intersections used: [Image] Intersections on the path are 3, 1, 2 and 4. Intersections on the path are 4, 2 and 1. Intersections on the path are only 3 and 6. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32 + 32 + 30 = 94. Intersections on the path are 6, 3 and 1. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second). | I = input
n = int(I())
d = {}
def lca(u, v, w):
res = 0
while u != v:
if u < v:
u, v = v, u
d[u] = d.get(u, 0) + w
res += d[u]
u = u // 2
return res
for i in range(n):
l = list(map(int, I().split()))
if l[0] == 1:
lca(l[1], l[2], l[3])
else:
print(lca(l[1], l[2], 0)) | ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.