description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Polycarp takes part in a math show. He is given n tasks, each consists of k subtasks, numbered 1 through k. It takes him t_{j} minutes to solve the j-th subtask of any task. Thus, time required to solve a subtask depends only on its index, but not on the task itself. Polycarp can solve subtasks in any order.
By solving subtask of arbitrary problem he earns one point. Thus, the number of points for task is equal to the number of solved subtasks in it. Moreover, if Polycarp completely solves the task (solves all k of its subtasks), he recieves one extra point. Thus, total number of points he recieves for the complete solution of the task is k + 1.
Polycarp has M minutes of time. What is the maximum number of points he can earn?
-----Input-----
The first line contains three integer numbers n, k and M (1 ≤ n ≤ 45, 1 ≤ k ≤ 45, 0 ≤ M ≤ 2·10^9).
The second line contains k integer numbers, values t_{j} (1 ≤ t_{j} ≤ 1000000), where t_{j} is the time in minutes required to solve j-th subtask of any task.
-----Output-----
Print the maximum amount of points Polycarp can earn in M minutes.
-----Examples-----
Input
3 4 11
1 2 3 4
Output
6
Input
5 5 10
1 2 4 8 16
Output
7
-----Note-----
In the first example Polycarp can complete the first task and spend 1 + 2 + 3 + 4 = 10 minutes. He also has the time to solve one subtask of the second task in one minute.
In the second example Polycarp can solve the first subtask of all five tasks and spend 5·1 = 5 minutes. Also he can solve the second subtasks of two tasks and spend 2·2 = 4 minutes. Thus, he earns 5 + 2 = 7 points in total. | n, k, M = map(int, input().split())
t = sorted(map(int, input().split()))
def calc(x):
tot = x * sum(t)
if tot > M:
return 0
tans = x * (k + 1)
for i in range(k - 1):
if t[i] * (n - x) + tot <= M:
tot += t[i] * (n - x)
tans += n - x
else:
tans += (M - tot) // t[i]
break
return tans
print(max([calc(x) for x in range(n + 1)])) | 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 FUNC_DEF ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER |
Polycarp takes part in a math show. He is given n tasks, each consists of k subtasks, numbered 1 through k. It takes him t_{j} minutes to solve the j-th subtask of any task. Thus, time required to solve a subtask depends only on its index, but not on the task itself. Polycarp can solve subtasks in any order.
By solving subtask of arbitrary problem he earns one point. Thus, the number of points for task is equal to the number of solved subtasks in it. Moreover, if Polycarp completely solves the task (solves all k of its subtasks), he recieves one extra point. Thus, total number of points he recieves for the complete solution of the task is k + 1.
Polycarp has M minutes of time. What is the maximum number of points he can earn?
-----Input-----
The first line contains three integer numbers n, k and M (1 ≤ n ≤ 45, 1 ≤ k ≤ 45, 0 ≤ M ≤ 2·10^9).
The second line contains k integer numbers, values t_{j} (1 ≤ t_{j} ≤ 1000000), where t_{j} is the time in minutes required to solve j-th subtask of any task.
-----Output-----
Print the maximum amount of points Polycarp can earn in M minutes.
-----Examples-----
Input
3 4 11
1 2 3 4
Output
6
Input
5 5 10
1 2 4 8 16
Output
7
-----Note-----
In the first example Polycarp can complete the first task and spend 1 + 2 + 3 + 4 = 10 minutes. He also has the time to solve one subtask of the second task in one minute.
In the second example Polycarp can solve the first subtask of all five tasks and spend 5·1 = 5 minutes. Also he can solve the second subtasks of two tasks and spend 2·2 = 4 minutes. Thus, he earns 5 + 2 = 7 points in total. | n, k, m = map(int, input().split())
l = list(map(int, input().split()))
l.sort()
sm = sum(l)
ans = 0
for i in range(n + 1):
if sm * i > m:
break
m1 = m - sm * i
cnt = [n - i] * k
cur = 0
res = (k + 1) * i
while m1 > 0:
if cur >= k or l[cur] > m1:
break
x = min(m1 // l[cur], cnt[cur])
res += x
m1 -= x * l[cur]
cur += 1
ans = max(res, ans)
print(ans) | 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 ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR WHILE VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Polycarp takes part in a math show. He is given n tasks, each consists of k subtasks, numbered 1 through k. It takes him t_{j} minutes to solve the j-th subtask of any task. Thus, time required to solve a subtask depends only on its index, but not on the task itself. Polycarp can solve subtasks in any order.
By solving subtask of arbitrary problem he earns one point. Thus, the number of points for task is equal to the number of solved subtasks in it. Moreover, if Polycarp completely solves the task (solves all k of its subtasks), he recieves one extra point. Thus, total number of points he recieves for the complete solution of the task is k + 1.
Polycarp has M minutes of time. What is the maximum number of points he can earn?
-----Input-----
The first line contains three integer numbers n, k and M (1 ≤ n ≤ 45, 1 ≤ k ≤ 45, 0 ≤ M ≤ 2·10^9).
The second line contains k integer numbers, values t_{j} (1 ≤ t_{j} ≤ 1000000), where t_{j} is the time in minutes required to solve j-th subtask of any task.
-----Output-----
Print the maximum amount of points Polycarp can earn in M minutes.
-----Examples-----
Input
3 4 11
1 2 3 4
Output
6
Input
5 5 10
1 2 4 8 16
Output
7
-----Note-----
In the first example Polycarp can complete the first task and spend 1 + 2 + 3 + 4 = 10 minutes. He also has the time to solve one subtask of the second task in one minute.
In the second example Polycarp can solve the first subtask of all five tasks and spend 5·1 = 5 minutes. Also he can solve the second subtasks of two tasks and spend 2·2 = 4 minutes. Thus, he earns 5 + 2 = 7 points in total. | n, k, m = map(int, input().split())
nums = list(map(int, input().split()))
result = -1
alltime = sum(nums)
for made in range(n + 1):
if alltime * made > m:
break
currentres = (k + 1) * made
currenttime = m - made * alltime
available = []
for item in nums:
available.extend([item] * (n - made))
available = sorted(available)
for item in available:
if currenttime < item:
break
currenttime -= item
currentres += 1
result = max(result, currentres)
print(result) | 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 ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
s = input()
cr = []
if n % 2 == 0:
if p > n // 2:
p = n - p + 1
else:
p = p
elif p > n // 2 + 1:
p = n - p + 1
else:
p = p
for i in range(n // 2):
if s[i] != s[n - 1 - i]:
cr.append(i)
count = 0
if len(cr) == 0:
pass
elif p - 1 > cr[-1]:
count = count + p - 1 - cr[0]
elif p - 1 < cr[0]:
count = count + cr[-1] - p + 1
elif abs(cr[0] - p + 1) < abs(cr[-1] - p + 1):
count = count + 2 * abs(cr[0] - p + 1) + abs(cr[-1] - p + 1)
elif abs(cr[0] - p + 1) == abs(cr[-1] - p + 1):
count = count + abs(cr[0] - p + 1)
else:
count = count + 2 * abs(cr[-1] - p + 1) + abs(cr[0] - p + 1)
for i in cr:
if abs(ord(s[i]) - ord(s[n - 1 - i])) <= 26 - abs(ord(s[i]) - ord(s[n - 1 - i])):
count = count + abs(ord(s[i]) - ord(s[n - 1 - i]))
else:
count = count + 26 - abs(ord(s[i]) - ord(s[n - 1 - i]))
print(count) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
p -= 1
string_orig = input()
if p >= (n + 1) // 2:
string_orig = string_orig[::-1]
p = n - p - 1
steps_rl = 0
string = list(string_orig)
pos = p
for i in range(p, (n + 1) // 2):
c1, c2 = string[i], string[n - i - 1]
if c1 != c2:
diff = abs(ord(c1) - ord(c2))
steps_rl += min(diff, 26 - diff) + i - pos
string[i] = string[n - i - 1]
pos = i
cur = pos
for i in range(cur, -1, -1):
c1, c2 = string[i], string[n - i - 1]
if c1 != c2:
diff = abs(ord(c1) - ord(c2))
steps_rl += min(diff, 26 - diff) + pos - i
string[i] = string[n - i - 1]
pos = i
string = list(string_orig)
steps_lr = 0
pos = p
cur = pos
for i in range(cur, -1, -1):
c1, c2 = string[i], string[n - i - 1]
if c1 != c2:
diff = abs(ord(c1) - ord(c2))
steps_lr += min(diff, 26 - diff) + pos - i
string[i] = string[n - i - 1]
pos = i
for i in range(p, (n + 1) // 2):
c1, c2 = string[i], string[n - i - 1]
if c1 != c2:
diff = abs(ord(c1) - ord(c2))
steps_lr += min(diff, 26 - diff) + i - pos
string[i] = string[n - i - 1]
pos = i
print(min(steps_rl, steps_lr)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, x = map(int, input().split())
s = input()
a = [0] * (n // 2)
if n % 2 == 0:
s1 = s[: n // 2]
s2 = s[n // 2 :]
else:
s1 = s[: n // 2]
s2 = s[n // 2 + 1 :]
s2 = s2[::-1]
if s1 == s2:
print(0)
exit()
for i in range(n // 2):
c = abs(ord(s1[i]) - ord(s2[i]))
a[i] = min(26 - c, c)
ans = sum(a)
l = r = -1
if x > n // 2:
x = abs(x - n)
else:
x -= 1
for i in range(x):
if a[i] != 0:
l = i
break
for i in range(x, n // 2):
if a[i] != 0:
r = i
if l == -1:
ans += abs(x - r)
elif r == -1:
ans += abs(x - l)
elif r != -1 and l != -1:
ans += min(abs(x - r), abs(x - l))
ans += abs(r - l)
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
ans = [0] * n
if p > (n + 1) // 2:
p = ~(p - 1) % n + 1
s = input()
for i in range(n // 2):
ans[i] = min((ord(s[i]) - ord(s[~i])) % 26, (ord(s[~i]) - ord(s[i])) % 26)
fz = 0
while fz < n and ans[fz] == 0:
fz += 1
lz = n // 2 - 1
while lz >= 0 and ans[lz] == 0:
lz -= 1
lz = n // 2 - 1 - lz
if fz == n:
exit(print(0))
gl = max(0, p - 1 - fz)
gr = max(0, n // 2 - p - lz)
print(sum(ans) + min(gl * 2 + gr, gr * 2 + gl)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, k = map(int, input().split(" "))
s = input()
cnt = 0
hs = [0] * n
for i in range(n // 2):
if s[i] != s[n - i - 1]:
hs[i] = min(
abs(ord(s[i]) - ord(s[n - i - 1])), 26 - abs(ord(s[i]) - ord(s[n - i - 1]))
)
hs[n - i - 1] = hs[i]
cnt = 0
mind = -1
mxind = -1
if k <= n // 2:
for i in range(n // 2):
if hs[i] != 0:
mxind = i
cnt += hs[i]
if hs[i] != 0 and mind == -1:
mind = i
if mxind != -1:
print(cnt + mxind - mind + min(abs(mxind - k + 1), abs(mind - k + 1)))
else:
print(0)
else:
for i in range(n // 2, n):
if hs[i] != 0:
mxind = i
cnt += hs[i]
if hs[i] != 0 and mind == -1:
mind = i
if mxind != -1:
print(cnt + mxind - mind + min(abs(mxind - k + 1), abs(mind - k + 1)))
else:
print(0) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = [int(x) for x in input().split()]
p -= 1
atz = "qwertyuiopasdfghjklzxcvbnm"
if len(atz) != 26:
print("gone wrong")
d = {}
for i in atz:
for j in atz:
x = abs(ord(i) - ord(j))
x = min(x, 26 - x)
d[i, j] = x
s = input()
i = 0
j = len(s) - 1
m = (n + 1) // 2
if p >= m:
p = n - 1 - p
s = s[::-1]
revs = s[::-1]
cost = 0
while i < j:
cost += d[s[i], s[j]]
i += 1
j -= 1
if cost == 0:
print(cost)
return
l = 0
while d[s[l], revs[l]] == 0:
l += 1
r = n // 2 - 1
while d[s[r], revs[r]] == 0:
r -= 1
cost += r - l + min(abs(l - p), abs(r - p))
print(cost) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | from sys import stdin, stdout
nmbr = lambda: int(stdin.readline())
lst = lambda: list(map(int, input().split()))
def fn(ch1, ch2):
m1 = (ord(ch1) - ord(ch2) + 26) % 26
m2 = (ord(ch2) - ord(ch1) + 26) % 26
return min(m1, m2, 26 - m1, 26 - m2)
for _ in range(1):
n, p = lst()
s = input()
hf = (n - 1) // 2
p -= 1
if p > hf:
p = n - p - 1
p1 = ans = 0
while p1 < p and s[p1] == s[n - p1 - 1]:
p1 += 1
p2 = hf
while p2 > p and s[p2] == s[n - p2 - 1]:
p2 -= 1
for i in range(p1, p2 + 1):
ans += fn(s[i], s[n - i - 1])
ans += min(2 * abs(p - p1) + abs(p - p2), abs(p - p1) + 2 * abs(p - p2))
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, c = map(int, input().split())
s = input()
ans = 0
first = n - 1
last = 0
for i in range(n // 2):
if s[i] != s[n - i - 1]:
first = min(first, i)
last = max(last, i)
a = ord(s[i]) - 97
b = ord(s[n - i - 1]) - 97
change = min(a, b) + 26 - max(a, b)
ans += min(abs(b - a), change)
if c == n // 2:
ans += n // 2 - first
elif c < n // 2:
c -= 1
a = c - min(c, first)
b = max(c, last) - c
ans += a + b + min(a, b)
else:
c = n - c
a = c - min(c, first)
b = max(c, last) - c
ans += a + b + min(a, b)
print(max(0, ans)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | def main():
def parser():
while 1:
data = list(input().split(" "))
for number in data:
if len(number) > 0:
yield number
input_parser = parser()
gets = lambda: next(input_parser)
def getNum():
data = gets()
try:
return int(data)
except ValueError:
return float(data)
ALP_SIZE = 26
n, p = getNum(), getNum()
p -= 1
s = gets()
res, l, r = 0, n, -1
for i in range(n // 2):
t = abs(ord(s[i]) - ord(s[n - i - 1]))
res += min(t, ALP_SIZE - t)
if t > 0:
pt = i
if p >= n // 2:
pt = n - i - 1
l = min(l, pt)
r = max(r, pt)
if r < p:
t = p - l
elif p < l:
t = r - p
else:
t = r - l + min(r - p, p - l)
res = max(res + t, 0)
print(res)
main() | FUNC_DEF FUNC_DEF WHILE NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split(" "))
s = list(input())
change = 0
leftchange = set()
rightchange = set()
p -= 1
for x in range(n // 2):
if s[x] != s[n - x - 1]:
leftchange.add(x)
rightchange.add(n - x - 1)
change += min(
abs(ord(s[x]) - ord(s[n - x - 1])),
ord(s[x]) + ord("z") - ord(s[n - x - 1]) - ord("a") + 1,
ord("z") - ord(s[x]) + ord(s[n - x - 1]) - ord("a") + 1,
)
if len(leftchange) == 0:
print(0)
elif p <= n // 2 - 1:
if max(leftchange) <= p:
print(p - min(leftchange) + change)
elif min(leftchange) >= p:
print(max(leftchange) - p + change)
else:
print(
min(
max(leftchange) - p + max(leftchange) - min(leftchange),
p - min(leftchange) + max(leftchange) - min(leftchange),
)
+ change
)
elif max(rightchange) <= p:
print(p - min(rightchange) + change)
elif min(rightchange) >= p:
print(max(rightchange) - p + change)
else:
print(
min(
max(rightchange) - p + max(rightchange) - min(rightchange),
p - min(rightchange) + max(rightchange) - min(rightchange),
)
+ change
) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
temp = list(input())
check = []
if p > n // 2:
p = n + 1 - p
cnt = 0
for i in range(n // 2):
if temp[i] != temp[n - 1 - i]:
check += [i + 1]
a = min(
abs(ord(temp[i]) - ord(temp[n - 1 - i])),
26 - abs(ord(temp[i]) - ord(temp[n - 1 - i])),
)
cnt += a
if len(check) == 0:
print(0)
else:
if p <= check[0]:
cnt += check[-1] - p
elif p >= check[-1]:
cnt += p - check[0]
else:
cnt += min(p - check[0], check[-1] - p) + min(
abs(check[0] - check[-1]), n - abs(check[0] - check[-1])
)
print(cnt) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST IF 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 IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR LIST BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, pos = map(int, input().split())
s = input()
c = 0
indxi = []
indxj = []
if n > 1:
i = 0
j = n - 1
while i < n // 2 and j >= n // 2:
if s[i] != s[j]:
indxi.append(i)
indxj.append(j)
c += min(abs(ord(s[i]) - ord(s[j])), abs(abs(ord(s[i]) - ord(s[j])) - 26))
i += 1
j -= 1
if pos - 1 >= n // 2:
if len(indxj) == 1:
if indxj[0] != pos - 1:
c += abs(indxj[0] - (pos - 1))
elif len(indxj) > 1:
minj = min(abs(min(indxj) - (pos - 1)), abs(max(indxj) - (pos - 1)))
maxj = max(abs(min(indxj) - (pos - 1)), abs(max(indxj) - (pos - 1)))
if min(indxj) < pos - 1 and pos - 1 < max(indxj):
c += 2 * minj + maxj
elif pos - 1 == min(indxj) or pos - 1 == max(indxj):
c += abs(min(indxj) - max(indxj))
elif pos - 1 > max(indxj):
c += abs(pos - 1 - min(indxj))
else:
c += abs(pos - 1 - max(indxj))
elif len(indxi) == 1:
if indxi[0] != pos - 1:
c += abs(indxi[0] - (pos - 1))
elif len(indxi) > 1:
mini = min(abs(min(indxi) - (pos - 1)), abs(max(indxi) - (pos - 1)))
maxi = max(abs(min(indxi) - (pos - 1)), abs(max(indxi) - (pos - 1)))
if min(indxi) < pos - 1 and pos - 1 < max(indxi):
c += 2 * mini + maxi
elif pos - 1 == min(indxi) or pos - 1 == max(indxi):
c += abs(min(indxi) - max(indxi))
elif pos - 1 > max(indxi):
c += abs(pos - 1 - min(indxi))
else:
c += abs(pos - 1 - max(indxi))
print(c)
else:
print(0) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
text = input()
p -= 1
if p >= len(text) // 2:
text = text[-1::-1]
p = len(text) - p - 1
maxr = p
minl = p
for i in range(p, len(text) // 2):
if text[i] != text[-i - 1]:
maxr = i
flag = 0
for i in range(p, -1, -1):
if text[i] != text[-i - 1]:
minl = i
ans = 0
a = maxr - p + maxr - minl
b = p - minl + maxr - minl
ans += min(a, b)
for i in range(0, len(text) // 2):
ans += min(
(ord(text[-i - 1]) - ord(text[i])) % 26, (ord(text[i]) - ord(text[-i - 1])) % 26
)
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
s = input()
l = 0
r = n - 1
ans = 0
a = "abcdefghijklmnopqrstuvwxyz"
dic = {}
for i in range(26):
dic[a[i]] = i + 1
dis = []
left = {}
right = {}
while l < r:
if s[l] != s[r]:
ans += min(abs(dic[s[l]] - dic[s[r]]), 26 - abs(dic[s[l]] - dic[s[r]]))
left[l] = 1
right[r] = 1
l += 1
r -= 1
l = 0
r = 0
p -= 1
if p <= n // 2 - 1:
for i in range(n // 2):
if i <= p and i in left and p - i > l:
l = p - i
elif i > p and i in left and i - p > r:
r = i - p
ans += min(2 * r + l, 2 * l + r)
elif p >= n // 2:
for i in range(n // 2, n):
if i <= p and i in right and p - i > l:
l = p - i
elif i > p and i in right and i - p > r:
r = i - p
ans += min(2 * r + l, 2 * l + r)
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR DICT WHILE VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, pos = (int(x) for x in input("").split())
s = input("")
def dis(a, b):
a = ord(a)
b = ord(b)
return min(abs(a - b), abs(a + 26 - b), abs(b + 26 - a))
d = [dis(s[i], s[len(s) - i - 1]) for i in range((len(s) + 1) // 2)]
pos -= 1
if pos >= (len(s) + 1) // 2:
pos = len(s) - pos - 1
for rightmost, v in enumerate(reversed(d)):
if v != 0:
rightmost = len(d) - rightmost - 1
break
for leftmost, v in enumerate(d):
if v != 0:
break
ans = min(
abs(rightmost - pos) + rightmost - leftmost,
abs(pos - leftmost) + rightmost - leftmost,
) + sum(d)
if sum(d) == 0:
print(0)
else:
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | import sys
def dbg(*args):
print("D:", *args, file=sys.stderr)
n, p = map(int, input().split())
s = input()
if s == s[::-1]:
print("0")
else:
sz = len(s)
if p > sz // 2:
p = sz - p
else:
p -= 1
l, r = 0, sz // 2 - 1
while s[l] == s[-l - 1]:
l += 1
while s[r] == s[-r - 1]:
r -= 1
dbg(l, r, p)
sum = r - l
sum += min(abs(p - l), abs(r - p))
for i in range(l, r + 1):
a1 = ord(s[i])
a2 = ord(s[-i - 1])
if a1 > a2:
a1, a2 = a2, a1
sum += min(a2 - a1, a1 + 26 - a2)
print(sum) | IMPORT FUNC_DEF EXPR FUNC_CALL VAR STRING VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
p -= 1
s = input()
arr = [
(lambda x, y: min(abs(x - y), 26 - abs(y - x)))(ord(s[i]), ord(s[n - i - 1]))
for i in range(n // 2)
]
p = min(p, n - p - 1)
f, l = 0, len(arr) - 1
for a in arr:
if a != 0:
break
else:
f += 1
for a in reversed(arr):
if a != 0:
break
else:
l -= 1
print(sum(arr) + l - f + min(abs(p - l), abs(p - f)) if sum(arr) > 0 else 0) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | def palindrome_transformation(n, p, nams):
current = p - 1
half = n // 2
if current >= half:
current = n - current - 1
weight = [0] * half
for i in range(half):
diff = abs(ord(nams[i]) - ord(nams[n - i - 1]))
if diff > 13:
diff = 26 - diff
weight[i] = diff
smallest_index = None
biggest_index = None
for i, v in enumerate(weight):
if v != 0:
smallest_index = i if smallest_index is None else min(i, smallest_index)
biggest_index = i if biggest_index is None else max(i, biggest_index)
if smallest_index is None or biggest_index is None:
return 0
return (
sum(weight)
+ biggest_index
- smallest_index
+ min(abs(biggest_index - current), abs(current - smallest_index))
)
n, p = map(int, input().strip().split())
nams = input().strip()
print(palindrome_transformation(n, p, nams)) | FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NONE VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NONE VAR FUNC_CALL VAR VAR VAR IF VAR NONE VAR NONE RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | def piche_se(n, p, s):
s = [i for i in s]
sol = 0
if 1 == 1:
if n % 2 == 0:
if p < n // 2:
tota = n // 2
last = -1
else:
tota = n
last = n // 2 - 1
elif p <= n // 2:
tota = n // 2 + 1
last = -1
else:
tota = n
last = n // 2
temp = 0
prev = p
soty = prev
if abs(p - tota) > abs(p - last):
for i in range(soty, last, -1):
if s[i] != s[n - i - 1]:
sol += abs(i - prev)
sol += min(
(ord(s[i]) - ord(s[n - i - 1])) % 26,
(ord(s[n - i - 1]) - ord(s[i])) % 26,
)
prev = i
s[n - i - 1] = s[i]
soty = prev
for i in range(soty, tota):
if s[i] != s[n - i - 1]:
sol += abs(i - prev)
sol += min(
(ord(s[i]) - ord(s[n - i - 1])) % 26,
(ord(s[n - i - 1]) - ord(s[i])) % 26,
)
prev = i
s[n - i - 1] = s[i]
return sol
for i in range(p, tota):
if s[i] != s[n - i - 1]:
sol += abs(i - prev)
sol += min(
(ord(s[i]) - ord(s[n - i - 1])) % 26,
(ord(s[n - i - 1]) - ord(s[i])) % 26,
)
prev = i
s[n - i - 1] = s[i]
soty = prev
for i in range(soty, last, -1):
if s[i] != s[n - i - 1]:
sol += abs(i - prev)
sol += min(
(ord(s[i]) - ord(s[n - i - 1])) % 26,
(ord(s[n - i - 1]) - ord(s[i])) % 26,
)
prev = i
s[n - i - 1] = s[i]
return sol
n, p = list(map(int, input().split()))
s = input().rstrip()
print(piche_se(n, p - 1, s)) | FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER IF NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = list(map(int, input().split()))
a = input()
if p > n // 2:
a = a[::-1]
p = n - p
else:
p = p - 1
ans = 0
lo = hi = p
i = p
while i >= 0:
if a[i] != a[n - i - 1]:
lo = i
mx = max(ord(a[i]) - ord("a") + 1, ord(a[n - i - 1]) - ord("a") + 1)
mi = min(ord(a[i]) - ord("a") + 1, ord(a[n - i - 1]) - ord("a") + 1)
ans += min(mx - mi, 26 - mx + mi)
i -= 1
i = p + 1
while i < n // 2:
if a[i] != a[n - i - 1]:
hi = i
mx = max(ord(a[i]) - ord("a") + 1, ord(a[n - i - 1]) - ord("a") + 1)
mi = min(ord(a[i]) - ord("a") + 1, ord(a[n - i - 1]) - ord("a") + 1)
ans += min(mx - mi, 26 - mx + mi)
i += 1
xx = max(abs(lo - p), abs(hi - p))
yy = min(abs(lo - p), abs(hi - p))
ans += xx + 2 * yy
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR STRING NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | from sys import stdin
n, p = map(int, input().split())
s = stdin.readline().rstrip("\n")
l = [0] * n
for i in range(n // 2):
if s[i] != s[n - 1 - i]:
a = abs(ord(s[i]) - ord(s[n - 1 - i]))
b = ord(s[i]) - 97 + 1 + 122 - ord(s[n - 1 - i])
c = ord(s[n - 1 - i]) - 97 + 1 + 122 - ord(s[i])
l[i] = l[n - 1 - i] = min(a, b, c)
c = 0
for i in range(n // 2):
c += l[i]
p = p - 1
if p >= n // 2:
p = n - 1 - p
if c != 0:
a = 0
for i in range(n // 2 - 1, p, -1):
if l[i] != 0:
a = i - p
break
b = 2 * a
d = 0
for i in range(p):
if l[i] != 0:
d = p - i
break
b += d
a += 2 * d
print(min(a, b) + c)
else:
print(0) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | MOD = 10**9 + 7
I = lambda: list(map(int, input().split()))
n, p = I()
s = input()
a = [0] * n
p -= 1
if p >= n // 2:
p = n - p - 1
for i in range(n // 2):
a[i] = abs(ord(s[i]) - ord(s[n - i - 1]))
a[i] = a[n - i - 1] = min(a[i], 26 - a[i])
ind = 0
s = sum(a)
s = s // 2
if s == 0:
print(0)
else:
l = r = -1
for i in range(n // 2):
if a[i]:
l = i
break
for j in range(n // 2 - 1, -1, -1):
if a[j]:
r = j
break
if p == l or p == r:
print(s + (r - l))
else:
print(min(abs(l - p) + s + (r - l), abs(r - p) + s + (r - l))) | ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | def finddistance(a, b):
k1 = abs(122 - ord(b) + ord(a) - 96)
k2 = abs(ord(b) - ord(a))
k3 = abs(122 - ord(a) + ord(b) - 96)
k4 = abs(ord(a) - ord(b))
return min([k1, k2, k3, k4])
n, p = map(int, input().split())
s = list(input())
a = []
c = 0
d = 0
count = 0
for i in range(n // 2):
if i == n - i - 1:
pass
elif s[i] != s[n - i - 1]:
a.append(i)
div = n // 2
if p > div:
p = n - p + 1
if len(a) == 0:
print(0)
else:
p -= 1
last = 10**6
first = 10**6
if a[-1] >= p:
last = a[-1] - p
if a[0] <= p:
first = p - a[0]
mini = min(last, first)
p1 = p
if mini == first:
c = 0
while p1 >= a[0]:
if count == 0:
count = 1
else:
c += 1
if s[p1] != s[n - p1 - 1]:
c += finddistance(s[p1], s[n - p1 - 1])
s[p1] = s[n - p1 - 1]
p1 -= 1
d = 0
count = 0
p1 += 1
if a[-1] > p:
while p1 <= a[-1]:
if count == 0:
count = 1
else:
d += 1
if s[p1] != s[n - p1 - 1]:
d += finddistance(s[p1], s[n - p1 - 1])
s[p1] = s[n - p1 - 1]
p1 += 1
else:
c = 0
while p1 <= a[-1]:
if count == 0:
count = 1
else:
c += 1
if s[p1] != s[n - p1 - 1]:
c += finddistance(s[p1], s[n - p1 - 1])
s[p1] = s[n - p1 - 1]
p1 += 1
d = 0
p1 -= 1
count = 0
if a[0] < p:
while p1 >= a[0]:
if count == 0:
count = 1
else:
d += 1
if s[p1] != s[n - p1 - 1]:
d += finddistance(s[p1], s[n - p1 - 1])
s[p1] = s[n - p1 - 1]
p1 -= 1
print(c + d) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR LIST VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER IF VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR WHILE VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR WHILE VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
string = input()
def palindrome_transformer(string, n, p):
if n % 2 == 0:
s_1 = string[0 : int(n / 2)]
s_2 = string[int(n / 2) : len(string)]
s_1_reversed = ""
for numbers in range(int(len(string) / 2) - 1, -1, -1):
s_1_reversed += s_1[numbers]
score = 0
different_elements = []
for i in range(len(s_1)):
if abs(ord(s_1_reversed[i]) - ord(s_2[i])) > 12:
score += 26 - abs(ord(s_1_reversed[i]) - ord(s_2[i]))
different_elements.append(1)
else:
if abs(ord(s_1_reversed[i]) - ord(s_2[i])) == 0:
different_elements.append(0)
else:
different_elements.append(1)
score += abs(ord(s_1_reversed[i]) - ord(s_2[i]))
if p <= int(len(string) / 2):
different_elements.reverse()
start = 1
for elements in different_elements:
if elements == 1:
break
else:
start += 1
different_elements.reverse()
end = int(len(string) / 2)
for elements in different_elements:
if elements == 1:
break
else:
end -= 1
if start == int(len(string) / 2) + 1 and end == 0:
return 0
score += (
max(p, end) - min(start, p) + min(max(end - p, 0), max(p - start, 0))
)
else:
p -= int(len(string) / 2)
start = 1
for elements in different_elements:
if elements == 1:
break
else:
start += 1
different_elements.reverse()
end = int(len(string) / 2)
for elements in different_elements:
if elements == 1:
break
else:
end -= 1
if start == int(len(string) / 2) + 1 and end == 0:
return 0
score += (
max(p, end) - min(start, p) + min(max(end - p, 0), max(p - start, 0))
)
return score
if n % 2 == 1:
string_new = ""
for i in range(len(string)):
if i == int(len(string) / 2):
continue
else:
string_new += string[i]
if p > int(n / 2):
p -= 1
return palindrome_transformer(string_new, n - 1, p)
elif p == int(n / 2):
return palindrome_transformer(string_new, n - 1, p)
else:
return palindrome_transformer(string_new, n - 1, p)
print(palindrome_transformer(string, n, p)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER RETURN NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER RETURN NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR IF VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | ALPHA = ord("z") - ord("a") + 1
def is_problem(s):
return [int(s[i] != s[-i - 1]) for i in range(len(s))]
def count_problems(s):
return sum(is_problem(s))
def change(a, b):
return min(abs(a - b), ALPHA - abs(a - b))
def solve(p, s):
if p >= len(s) // 2:
s = list(reversed(s))
p = len(s) - p - 1
ss = is_problem(s)
first_problem = p
last_problem = p
answer = 0
for i, sss in enumerate(ss[: len(s) // 2]):
if sss:
first_problem = min(i, first_problem)
last_problem = max(i, last_problem)
answer += change(s[i], s[-i - 1])
answer += min(p - first_problem, last_problem - p) + (last_problem - first_problem)
return answer
def main():
n, p = map(int, input().split())
p -= 1
s = list(map(ord, input()))
print(solve(p, s))
assert 1 == change(ord("z"), ord("a"))
assert 1 == change(ord("b"), ord("c"))
assert 1 == solve(5, list(map(ord, "abcdefdcba")))
assert 6 == solve(2, list(map(ord, "aeabcaez")))
main() | ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR STRING NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR STRING NUMBER FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | def findMirror(counter, n):
return n - counter - 1
def checkForward(counter, n, st):
total = 0
done = [0] * (n + 1)
mid = int(n / 2)
high = 0
if counter <= mid:
high = mid
else:
high = n - 1
i = 0
steps = 0
while counter <= high:
i = i + 1
if done[counter] == 0:
mirror = findMirror(counter, n)
temp = abs(ord(st[counter]) - ord(st[mirror]))
total = total + min(temp, 26 - temp)
done[mirror] = 1
if min(temp, 26 - temp) != 0:
steps = i
counter = counter + 1
return total, max(steps - 1, 0)
def checkBack(counter, n, st):
total = 0
done = [0] * (n + 1)
mid = int(n / 2)
low = 0
if counter <= mid:
low = 0
else:
low = mid
i = 0
steps = 0
while counter >= low:
i = i + 1
if done[counter] == 0:
mirror = findMirror(counter, n)
temp = abs(ord(st[counter]) - ord(st[mirror]))
total = total + min(temp, 26 - temp)
done[mirror] = 1
if min(temp, 26 - temp) != 0:
steps = i
counter = counter - 1
return total, max(steps - 1, 0)
def main():
n, start = list(map(int, input().split()))
st = input()
a, left = checkBack(start - 1, n, st)
b, right = checkForward(start - 1, n, st)
startVal = abs(ord(st[start - 1]) - ord(st[findMirror(start - 1, n)]))
startVal = min(startVal, 26 - startVal)
print(a + b + left + right + min(left, right) - startVal)
main() | FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = list(map(int, input().split()))
l = input()
i = 0
j = n - 1
pos = []
ans = 0
while i <= j:
if l[i] != l[j]:
if abs(p - i) < abs(p - j):
pos.append(i + 1)
else:
pos.append(j + 1)
x = max(ord(l[i]), ord(l[j]))
y = min(ord(l[i]), ord(l[j]))
ans = ans + min(abs(x - y), abs(x - y - 26))
i = i + 1
j = j - 1
if len(pos) == 0:
print(ans)
else:
pmin = min(pos)
pmax = max(pos)
if pmin <= p and pmax <= p or pmin >= p and pmax >= p:
d = max(abs(p - pmin), abs(p - pmax))
ans = ans + d
else:
d2 = min(abs(p - pmin), abs(p - pmax))
d1 = max(abs(p - pmax), abs(p - pmin))
ans = ans + 2 * d2 + d1
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
w = list(input())
d = []
done = []
for i in range(n // 2):
if w[i] != w[-i - 1]:
temp = int(abs(ord(w[i]) - ord(w[-i - 1])))
d.append(min(temp, 26 - temp))
done.append(i)
if p <= n // 2:
p -= 1
else:
p = n - p
if len(done) == 0:
print(0)
else:
a = min(done)
b = max(done)
ans = sum(d)
ans += min(abs(p - a), abs(b - p)) + (b - a)
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
s = input()
ans = 0
if p > n // 2:
s = s[::-1]
p = n - p + 1
l = []
last = p
temp = p
l.extend(s)
while temp > 0:
yo = min(
abs(ord(s[temp - 1]) - ord(s[n - temp])),
26 - abs(ord(s[temp - 1]) - ord(s[n - temp])),
)
ans += yo
l[temp - 1] = l[n - temp]
if yo:
last = temp
temp -= 1
last2 = p
temp = p + 1
while temp <= n // 2:
yo = min(
abs(ord(s[temp - 1]) - ord(s[n - temp])),
26 - abs(ord(s[temp - 1]) - ord(s[n - temp])),
)
ans += yo
if yo:
last2 = temp
temp += 1
ans += min(abs(p - last) * 2 + abs(last2 - p), abs(last2 - p) * 2 + abs(p - last))
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR IF VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR IF VAR ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | I = lambda: list(map(int, input().split()))
def ge(x):
return ord(x) - 97
n, k = I()
s = input()
x = 0
ind = []
k -= 1
for i in range(n // 2):
p = min((ge(s[i]) - ge(s[n - i - 1])) % 26, (ge(s[n - i - 1]) - ge(s[i])) % 26)
if p != 0:
ind.append(i if k < n // 2 else n - i - 1)
x += p
if ind != []:
x += min(
abs(k - ind[-1]) + abs(ind[-1] - ind[0]),
abs(k - ind[0]) + abs(ind[-1] - ind[0]),
)
print(x) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR LIST VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | def cost(a, b):
return min(abs(ord(b) - ord(a)), 26 - abs(ord(b) - ord(a)))
def pal(x, n):
return n - x - 1
n, pt = map(int, input().split())
pt -= 1
s = input()
if pt >= n // 2:
s = s[::-1]
pt = pal(pt, n)
cos = [cost(s[i], s[pal(i, n)]) for i in range(n)]
start = -1
end = -1
for i in range(n):
if cos[i]:
start = i
break
for i in range(int(n // 2) - 1, -1, -1):
if cos[i]:
end = i
break
turn = sum(cos[: int(n // 2)])
walk = 0
if start != -1:
if pt <= start:
walk = end - pt
elif pt > start and pt < end:
walk = min(end - pt + end - pt + pt - start, pt - start + pt - start + end - pt)
else:
walk = pt - start
print(walk + turn) | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, s = [int(i) for i in input().split()]
st = input()
mid = n // 2
if s > mid:
s = n - s
else:
s -= 1
diff = 0
lis = []
for i in range(mid):
if st[i] != st[n - 1 - i]:
t1 = ord(st[i]) - 97
t2 = ord(st[n - 1 - i]) - 97
test1 = t2 - t1
if test1 < 0:
test1 += 26
test2 = t1 - t2
if test2 < 0:
test2 += 26
diff += min(test1, test2)
lis.append(i)
before = []
after = []
for i in lis:
if i < s:
before.append(i)
elif i > s:
after.append(i)
mi = ma = -1
if len(before) > 0:
mi = min(before)
if len(after) > 0:
ma = max(after)
if mi == -1 or ma == -1:
m = max(mi, ma)
if m != -1:
diff += abs(s - m)
else:
d1 = s - mi
d2 = ma - s
if d2 <= d1:
diff += 2 * d2 + d1
else:
diff += 2 * d1 + d2
print(diff) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | s = input()
p = int(s.split()[1])
s = input()
n = int(len(s))
a = []
if p > n // 2:
p = n - p + 1
sum = 0
if n == 1:
print("0")
quit()
for i in range(n // 2):
j = int(n - i - 1)
q = abs(ord(s[j]) - ord(s[i]))
a.append(min(q, 26 - q))
sum += a[i]
i = 0
while (a[i] == 0) & (i < p - 1):
i += 1
if i == p - 1:
i = 0
else:
i = p - i - 1
j = n // 2 - 1
while (a[j] == 0) & (j > p - 1):
j -= 1
if j == p - 1:
j = 0
else:
j = j - p + 1
sum += min(i, j) * 2 + max(i, j)
print(str(sum)) | ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | import sys
def find_cost(a, b):
ch = "abcdefghijklmnopqrstuvwxyz"
min_cost = sys.maxsize
a, b = min(a, b), max(a, b)
c = ch.index(b) - ch.index(a)
d = 25 - ch.index(b) + (ch.index(a) - 0) + 1
return int(min(c, d))
n, k = map(int, input().split())
st = input()
mid = (n + 1) // 2
s, e = int(), int()
ans = 0
if k <= mid:
s, e = 1, mid
if n % 2:
e -= 1
if k == mid:
k -= 1
ans += 1
else:
s, e = mid + 1, n
j = 0
if s == 1:
j = n
elif n % 2:
j = s - 2
else:
j = s - 1
cost = [0] * (n + 5)
s = int(s)
e = int(e)
for i in range(int(s), int(e + 1)):
cost[i] = find_cost(st[i - 1], st[-i])
if sum(cost) == 0:
print(0)
exit(0)
f, l = 0, 0
ff, ll = k, k
for i in range(k, e + 1):
if cost[i] != 0:
ll = i
for i in range(s, k + 1):
if cost[i] != 0:
ff = i
break
cst = sum(cost[ff : ll + 1]) + ll - ff + min(k - ff, ll - k)
ans += cst
print(ans) | IMPORT FUNC_DEF ASSIGN VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
s = input()
main, m = "abcdefghijklmnopqrstuvwxyz", dict()
for i in range(26):
m[main[i]] = i
p = min(p - 1, n - p)
b, a, ans, f, ff = -1, -1, 0, True, True
for i in range(n // 2):
if s[i] != s[n - 1 - i]:
ans += min(abs(m[s[i]] - m[s[n - i - 1]]), 26 - abs(m[s[i]] - m[s[n - i - 1]]))
if i < p and ff:
b = i
ff = False
if i > p:
a = i
if b != -1 and a != -1:
ans += abs(a - b) + min(p - b, a - p)
if b == -1 or a == -1:
if b == -1 and a == -1:
ans = ans
else:
ans += abs(p - max(a, b))
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR STRING FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
s = input()
res = 0
left = 10**9
right = 0
for i in range(n // 2):
if s[i] != s[n - i - 1]:
if p < n // 2:
if i < p:
left = min(left, i)
else:
right = max(right, i)
elif i < n - p + 1:
left = min(left, i)
else:
right = max(right, i)
res += min(
abs(ord(s[i]) - ord(s[n - i - 1])),
abs(ord(s[i]) - ord("a") + 1 + ord("z") - ord(s[n - i - 1])),
abs(ord("z") - ord(s[i]) + 1 + ord(s[n - i - 1]) - ord("a")),
)
if p > n // 2:
p = n - p + 1
if left == 10**9 and right == 0:
pass
elif left == 10**9:
res += right - p + 1
elif right == 0:
res += p - left - 1
else:
res += right - left + min(right + 1 - p, p - 1 - left)
print(res) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR STRING FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP NUMBER NUMBER VAR NUMBER IF VAR BIN_OP NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = [int(x) for x in input().split()]
Estrin = input()
steps = 0
pwasmid = False
parin = 0
if len(Estrin) > 1:
if len(Estrin) % 2 == 0:
par = True
else:
par = False
parin = 1
if par and p > n // 2:
p = n - p + 1
if par == False and p > n // 2 + 1:
p = n - p + 1
first, second = Estrin[: len(Estrin) // 2], Estrin[len(Estrin) // 2 + parin :]
first = first[::-1]
p = len(first) - p
x = [False] * len(first)
flag = False
minerr = 0
maxerr = p
flagmin = False
flagmax = False
for i in range(0, len(first)):
if ord(first[i]) != ord(second[i]):
x[i] = True
if abs(ord(first[i]) - ord(second[i])) > 13:
steps += 26 - abs(ord(first[i]) - ord(second[i]))
else:
steps += abs(ord(first[i]) - ord(second[i]))
if flagmax == False:
flagmax = True
maxerr = i
if flagmin == False:
flagmin = True
minerr = i
if flagmin == False or flagmax == False:
if flagmin == False:
minerr = p
if flagmax == False:
maxerr = p
if par == False and (flagmin == True or flagmax == True) and pwasmid:
steps += 1
if minerr <= p and maxerr >= p:
right = maxerr - p + (maxerr - minerr)
left = p - minerr + (maxerr - minerr)
steps += min(right, left)
elif minerr < p and maxerr < p:
steps += p - minerr
elif minerr > p and maxerr > p:
steps += maxerr - p
else:
steps = 0
print(steps) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
p = min(p, n + 1 - p)
p -= 1
s = input()
ans = 0
t = []
for i in range(n // 2):
if s[i] != s[-i - 1]:
ans += min(
abs(ord(s[i]) - ord(s[-i - 1])), 26 - abs(ord(s[i]) - ord(s[-i - 1]))
)
t.append(i)
if not t:
ans = 0
elif t[0] >= p:
ans += t[-1] - p
elif t[-1] <= p:
ans += p - t[0]
else:
m = min(p - t[0] + t[-1] - t[0], t[-1] - p + t[-1] - t[0])
ans += m
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | from sys import stdin
def solve(tc):
n, p = map(int, stdin.readline().split())
n -= 1
p -= 1
s = stdin.readline()
half = n // 2
lo = 0
hi = n
if p <= half:
hi = half - 1 if n % 2 == 0 else half
else:
lo = half + 1
while lo < hi and s[lo] == s[n - lo]:
lo += 1
while lo < hi and s[hi] == s[n - hi]:
hi -= 1
if lo == hi and s[lo] == s[n - lo]:
print(0)
return
cnt = hi - lo
if p < lo:
cnt += lo - p
elif p > hi:
cnt += p - hi
else:
cnt += min(p - lo, hi - p)
for i in range(lo, hi + 1):
fwd = ord(s[i]) - ord(s[n - i])
if fwd < 0:
fwd += 26
rev = ord(s[n - i]) - ord(s[i])
if rev < 0:
rev += 26
cnt += min(fwd, rev)
print(cnt)
pass
LOCAL_TEST = not __debug__
if LOCAL_TEST:
infile = __file__.split(".")[0] + "-test.in"
stdin = open(infile, "r")
tcs = int(stdin.readline().strip()) if LOCAL_TEST else 1
tc = 1
while tc <= tcs:
solve(tc)
tc += 1 | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR BIN_OP FUNC_CALL VAR STRING NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
t = input()
k = n // 2
d = [0] * (k + 1)
for i in range(k):
d[i] = abs(ord(t[i]) - ord(t[n - i - 1]))
if 2 * d[i] > 26:
d[i] = 26 - d[i]
p = n - p if p > k else p - 1
if d.count(0) == k + 1:
print(0)
else:
i, j = 0, k
while d[i] == 0:
i += 1
while d[j] == 0:
j -= 1
if i > p:
i = p
if j < p:
j = p
print(sum(d) + min(p - 2 * i + j, 2 * j - i - p)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR NUMBER VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, k = map(int, input().split())
s = input()
k -= 1
k = min(k, n - k - 1)
a = -1
b = 0
ans = 0
l = 0
for i in range(n // 2):
j = abs(ord(s[i]) - ord(s[n - 1 - i]))
j = min(j, 26 - j)
if j > 0 and a == -1:
a = i
if j > 0:
b = i
l += j
a = min(a, n - a - 1)
b = min(b, n - b - 1)
if l == 0:
print(0)
else:
if (k - a) * (k - b) > 0:
ans = max(abs(k - a), abs(k - b))
else:
ans = abs(k - a) + abs(k - b) + min(abs(k - a), abs(k - b))
print(ans + l) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().strip().split())
p = p - 1
A = input().strip()
s1 = 0
p1 = 0
p2 = float("Inf")
for i in range(int(len(A) // 2)):
if A[i] != A[-1 * i - 1]:
if i > p1:
p1 = i
if i < p2:
p2 = i
a = ord(A[i]) - ord(A[-1 * i - 1])
s1 += min([a % 26, -1 * a % 26])
if p2 == float("Inf"):
print(0)
else:
if p >= len(A) // 2:
p2, p1 = len(A) - p1 - 1, len(A) - p2 - 1
if p > p1 and p > p2:
s1 += max([p - p2, p - p1])
elif p < p1 and p < p2:
s1 += max([p1 - p, p2 - p])
else:
s1 += 2 * min([p1 - p, p - p2]) + max([p1 - p, p - p2])
print(s1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR LIST BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
s = input()
ans = []
for i in range(len(s)):
ans.append(s[i])
sm1 = 0
la1 = 0
flag = 0
for i in range(len(ans) // 2):
if ans[i] != ans[-1 * (i + 1)] and flag == 0:
sm1 = i + 1
flag = 1
if ans[i] != ans[-1 * (i + 1)]:
la1 = i + 1
if sm1 == 0:
print(0)
exit()
if len(ans) == 1:
print(0)
exit()
if p > len(ans) // 2:
p = len(ans) + 1 - p
if p < sm1:
count = 0
for i in range(p - 1, la1):
diff = max(ord(ans[i]), ord(ans[-1 * (i + 1)])) - min(
ord(ans[i]), ord(ans[-1 * (i + 1)])
)
count += min(diff, 26 - diff)
print(count + la1 - p)
elif sm1 <= p <= la1:
count = la1 - sm1
for i in range(sm1 - 1, la1):
diff = max(ord(ans[i]), ord(ans[-1 * (i + 1)])) - min(
ord(ans[i]), ord(ans[-1 * (i + 1)])
)
count += min(diff, 26 - diff)
print(count + min(p - sm1, la1 - p))
else:
count = 0
for i in range(p - 1, sm1 - 2, -1):
diff = max(ord(ans[i]), ord(ans[-1 * (i + 1)])) - min(
ord(ans[i]), ord(ans[-1 * (i + 1)])
)
count += min(diff, 26 - diff)
print(count + p - sm1) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
p -= 1
if p >= n // 2:
p = n - 1 - p
chars = input()
tmp = [abs(ord(chars[i]) - ord(chars[n - 1 - i])) for i in range(n // 2)]
tmp = [min(x, 26 - x) for x in tmp]
val1, val2 = -1, -1
for i in range(len(tmp)):
if tmp[i] > 0:
if val1 == -1:
val1 = i
val2 = i
a = 0 if val1 == -1 else min(abs(p - val1), abs(p - val2)) + (val2 - val1) + sum(tmp)
print(a) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER NUMBER BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | def solve(n, p, s):
if p >= n // 2:
p = n + 1 - p
to_be_changed = [i for i in range(n // 2) if s[i] != s[n - i - 1]]
if not to_be_changed:
return 0
tmp = [
min((ord(s[i]) - ord(s[n - i - 1])) % 26, -(ord(s[i]) - ord(s[n - i - 1])) % 26)
for i in to_be_changed
]
up_down_cost = sum(tmp)
right_left_cost = (
to_be_changed[-1]
- to_be_changed[0]
+ min(abs(p - 1 - to_be_changed[0]), abs(p - 1 - to_be_changed[-1]))
)
return up_down_cost + right_left_cost
def test():
assert solve(8, 3, "aeabcaez")
n, p = map(int, input().split())
s = input()
print(solve(n, p, s)) | FUNC_DEF IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR FUNC_DEF FUNC_CALL VAR NUMBER NUMBER STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = list(map(int, input().split()))
string = input()
def calc_delta(s):
def get_min_shift(i):
ord_diff = abs(ord(s[i]) - ord(s[n - i - 1]))
return min(ord_diff, 26 - ord_diff)
return {i: get_min_shift(i) for i in range((n + 1) // 2)}
def palindrome(s):
deltas = calc_delta(s)
offset_indices = list(filter(lambda i: deltas[i] > 0, deltas.keys()))
if not offset_indices:
return 0
alpha_delta = sum(deltas.values())
mindex = min(offset_indices)
maxdex = max(offset_indices)
sindex = min(p - 1, n - p)
if mindex == maxdex or maxdex <= sindex:
travel_delta = abs(sindex - mindex)
return travel_delta + alpha_delta
if mindex >= sindex:
return maxdex - sindex + alpha_delta
min_delta = min(maxdex - sindex, sindex - mindex)
max_delta = max(maxdex - sindex, sindex - mindex)
travel_delta = 2 * min_delta + max_delta
return travel_delta + alpha_delta
print(palindrome(string)) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP NUMBER VAR RETURN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR IF VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR RETURN BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = [int(x) for x in input().split()]
l = list(input())
flag = 0
if p > n // 2:
flag = 1
ans = 0
p -= 1
ff = p
ffm = p
for i in range(n):
if i > n - i - 1:
break
x = ord(l[i]) - 97
y = ord(l[n - i - 1]) - 97
if x != y:
ans += min(abs(x - y), 26 - max(x, y) + min(x, y))
if abs(p - i) < abs(p - (n - i - 1)):
temp = i
else:
temp = n - i - 1
ff = max(ff, temp)
ffm = min(ffm, temp)
x = abs(p - ff)
y = abs(p - ffm)
res = min(x, y) * 2 + max(x, y)
print(ans + res) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
p -= 1
s = input()
ans = 0
idx = 0
pos = []
while idx < n // 2:
if s[idx] != s[n - 1 - idx]:
diff = abs(ord(s[idx]) - ord(s[n - 1 - idx]))
ans += min(diff, 26 - diff)
if p >= n // 2:
pos.append(n - 1 - idx)
else:
pos.append(idx)
idx += 1
pos.sort()
if ans == 0:
print(0)
elif len(pos) == 1:
ans += abs(p - pos[0])
print(ans)
else:
if abs(p - pos[0]) < abs(p - pos[-1]):
ans += abs(p - pos[0]) + (pos[-1] - pos[0])
else:
ans += abs(p - pos[-1]) + (pos[-1] - pos[0])
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | f = lambda c: ord(c) - ord("a")
dalp = lambda x, y: min(abs(x - y), abs(x + 26 - y), abs(y + 26 - x))
dist = lambda i, j: min(abs(i - j), abs(i + n - j), abs(j + n - i))
read = lambda: list(map(int, input().split()))
n, p = read()
a = [f(i) for i in input()]
pr, sf = [], []
cnt = 0
for i in range(n // 2):
if a[i] != a[n - i - 1]:
cnt += dalp(a[i], a[n - i - 1])
pr.append(i)
sf.append(n - i - 1)
if pr:
p -= 1
d1 = dist(pr[0], pr[-1])
d2 = min(dist(p, pr[0]), dist(p, pr[-1]), dist(p, sf[0]), dist(p, sf[-1]))
cnt += d1 + d2
print(cnt) | ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
s = input()
mid = n // 2 - 1
p -= 1
if p >= mid:
s = s[::-1]
p = n - p - 1
l = -1
r = -1
i = p
res = 0
while i >= 0:
if s[i] != s[n - i - 1]:
l = i
diff = min(
abs(ord(s[i]) - ord(s[n - i - 1])), 26 - abs(ord(s[i]) - ord(s[n - i - 1]))
)
res += diff
i -= 1
i = p + 1
while i <= mid:
if s[i] != s[n - i - 1]:
r = i
diff = min(
abs(ord(s[i]) - ord(s[n - i - 1])), 26 - abs(ord(s[i]) - ord(s[n - i - 1]))
)
res += diff
i += 1
if l == -1 and r == -1:
print(0)
else:
if r == -1:
r = p
if l == -1:
l = p
if p - l < r - p:
res += 2 * (p - l)
res += r - p
else:
res += p - l
res += 2 * (r - p)
print(res) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
p -= 1
s = input()
if p >= n // 2:
p = n - 1 - p
l = 0
r = n // 2 - 1
while l < n // 2 and s[l] == s[n - 1 - l]:
l += 1
while r >= 0 and s[r] == s[n - 1 - r]:
r -= 1
ans, ch = 0, 0
for i in range(l, r + 1):
temp = abs(ord(s[i]) - ord(s[n - 1 - i]))
ch += min(temp, 26 - temp)
if l < r:
ans += min(abs(p - r), abs(p - l)) + abs(r - l) + ch
elif l == r:
if ch:
ans += abs(p - l) + ch
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER WHILE VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = [int(i) for i in input().split()]
p -= 1
p = min(p, n - p - 1)
s = input()
ans = 0
m1 = None
m2 = 0
for i in range(n):
if n - i - 1 < i:
break
if s[i] != s[-i - 1]:
if m1 is None:
m1 = i
m2 = max(m2, i)
ans += min(abs(ord(s[i]) - ord(s[-i - 1])), 26 - abs(ord(s[i]) - ord(s[-i - 1])))
if m1 is None:
print(0)
exit(0)
if m2 <= p:
x = p - m1
elif m1 >= p:
x = m2 - p
else:
x = min(2 * (m2 - p) + p - m1, 2 * (p - m1) + m2 - p)
print(ans + x) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR NONE ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NONE EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
p -= 1
st = input().strip()
l = 0
h = n // 2 - 1
if p > h:
p = n - 1 - p
inside = False
ans = 0
i = 0
H = -1
while i <= h:
if st[i] != st[n - 1 - i]:
if not inside:
l = i
inside = True
H = i
ans += min(
abs(ord(st[i]) - ord(st[n - 1 - i])),
abs(ord("z") - ord("a") + 1 - abs(ord(st[i]) - ord(st[n - 1 - i]))),
)
i += 1
h = H
if h == -1:
print("0")
exit(0)
ans += h - l
ans += min(abs(l - p), abs(p - h))
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR STRING NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
ans = 0
s = input()
for i in range(n // 2):
a = []
a.append(s[i])
a.append(s[n - 1 - i])
a.sort()
ans += min(ord(a[1]) - ord(a[0]), ord("z") - ord(a[1]) + 1 + ord(a[0]) - ord("a"))
p -= 1
n -= 1
mid = n // 2
if p > mid:
p = n - p
st = 0
e = mid
while s[st] == s[n - st] and st < n:
st += 1
while s[e] == s[n - e] and e > 0:
e -= 1
if st > e:
print(0)
else:
ans += min(abs(p - st), abs(e - p))
ans += e - st
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | def main():
s = "abcdefghijklmnopqrstuvwxyz"
d = {
(a, b): min((ord(a) - ord(b)) % 26, (ord(b) - ord(a)) % 26)
for a in s
for b in s
}
n, m = map(int, input().split())
s, res, n, m = input(), 0, n - 1, min(m - 1, n - m)
idx = [i for i, a in zip(range((n + 1) // 2), s) if a != s[n - i]]
print(
sum(d[s[i], s[n - i]] for i in idx)
+ min(abs(m - idx[0]), abs(m - idx[-1]))
+ idx[-1]
- idx[0]
if idx
else 0
)
main() | FUNC_DEF ASSIGN VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | input_ = list(map(int, input().split()))
n, p = input_[0], input_[1]
pal_ = list(input())
pal_ = [(ord(letter) - 96) for letter in pal_]
diff_ = 0
dist_ = 0
p_down = p - 1 <= int(n / 2) - 1
if n % 2 == 0:
bad_ = list()
for i in range(int(n / 2)):
if pal_[int(n / 2) - 1 - i] != pal_[int(n / 2) + i]:
if p_down:
bad_.append(int(n / 2) - 1 - i)
else:
bad_.append(int(n / 2) + i)
max_pal = max(pal_[int(n / 2) - 1 - i], pal_[int(n / 2) + i])
min_pal = min(pal_[int(n / 2) - 1 - i], pal_[int(n / 2) + i])
diff_ += min(max_pal - min_pal, 26 - max_pal + min_pal)
else:
bad_ = list()
for i in range(int(n / 2) + 1):
if pal_[int(n / 2) - i] != pal_[int(n / 2) + i]:
if p_down:
bad_.append(int(n / 2) - i)
else:
bad_.append(int(n / 2) + i)
max_pal = max(pal_[int(n / 2) - i], pal_[int(n / 2) + i])
min_pal = min(pal_[int(n / 2) - i], pal_[int(n / 2) + i])
diff_ += min(max_pal - min_pal, 26 - max_pal + min_pal)
if len(bad_) == 0:
print(0)
else:
if p_down:
min_ = bad_[-1]
max_ = bad_[0]
if p - 1 >= max_:
dist_ = p - 1 - min_
elif p - 1 <= min_:
dist_ = max_ - p + 1
else:
dist_1, dist_2 = p - 1 - min_, max_ - p + 1
dist_ = 2 * min(dist_1, dist_2) + max(dist_1, dist_2)
else:
min_ = bad_[0]
max_ = bad_[-1]
if p - 1 <= min_:
dist_ = max_ - p + 1
elif p - 1 >= max_:
dist_ = p - 1 - min_
else:
dist_1, dist_2 = p - 1 - min_, max_ - p + 1
dist_ = 2 * min(dist_1, dist_2) + max(dist_1, dist_2)
print(dist_ + diff_) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
s = input()
mid = int(n / 2)
alphabets = "".join([chr(i) for i in range(97, 123)]) * 2
def min_diff(alp1, alp2, alphabets=alphabets):
alp1, alp2 = min(alp1, alp2), max(alp1, alp2)
return min(
abs(alphabets.find(alp1) - alphabets.find(alp2)),
abs(alphabets.find(alp2) - alphabets.replace(alp1, "0", 1).find(alp1)),
)
p = 1 + n - p if p > mid else p
i = 0
diff = []
steps = 0
while i <= mid - 1:
temp = min_diff(s[i], s[n - 1 - i])
diff.append(1 if temp > 0 else 0)
steps += temp
i += 1
l_l = diff[: p - 1]
l_r = diff[p:]
if sum(l_l) == 0 and sum(l_r) == 0:
pass
elif sum(l_l) == 0:
steps = steps + len(l_r)
elif sum(l_r) == 0:
steps = steps + len(l_l)
else:
l_l_index = len(l_l) - l_l.index(1)
l_r_index = len(l_r) - l_r[::-1].index(1)
steps = steps + 2 * min(l_l_index, l_r_index) + max((l_l_index, l_r_index))
print(0 if n == 1 else steps + 1 if p == int(n / 2) + 1 else steps) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FUNC_DEF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR STRING NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
s = list(map(ord, input()))
if p > n // 2:
p = n - p
else:
p -= 1
l = n // 2
r = -1
ans = 0
for i in range(n // 2):
d = abs(s[i] - s[n - i - 1])
d = min(d, 26 - d)
if d:
l = min(l, i)
r = max(r, i)
ans += d
print(ans + r - l + min(abs(r - p), abs(l - p)) if ans else 0) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR 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 FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
s = input()
ans = 0
p -= 1
if n % 2 == 1 and p == n // 2 and n != 1:
p -= 1
ans += 1
cnt = 0
for i in range(n):
if n - 1 - i <= i:
break
if s[i] != s[n - 1 - i]:
cnt += 1
if p >= n // 2 and n != 1:
h = ""
for i in range(n - 1, -1, -1):
h += s[i]
s = h
p = n - 1 - p
r = 0
l = 0
rr = 0
ll = 0
for i in range(p):
if s[i] != s[n - 1 - i]:
ans += min(
26 - abs(ord(s[i]) - ord(s[n - 1 - i])), abs(ord(s[i]) - ord(s[n - 1 - i]))
)
r += 1
rr = max(rr, p - i)
for i in range(p + 1, n // 2):
if s[i] != s[n - 1 - i]:
ans += min(
26 - abs(ord(s[i]) - ord(s[n - 1 - i])), abs(ord(s[i]) - ord(s[n - 1 - i]))
)
l += 1
ll = max(ll, i - p)
ans += min(26 - abs(ord(s[p]) - ord(s[n - 1 - p])), abs(ord(s[p]) - ord(s[n - 1 - p])))
ans += ll + rr + min(ll, rr)
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
s = input().rstrip()
p = min(p, n - p + 1)
Count = 0
M = l = r = X = p
for i in range(0, n // 2):
C = min(
abs(ord(s[i]) - ord(s[-i - 1])),
abs(
ord("z")
- max(ord(s[i]), ord(s[-i - 1]))
+ min(ord(s[i]), ord(s[-i - 1]))
- ord("a")
+ 1
)
% 26,
)
if C > 0:
if i + 1 < p:
if M == p:
M = i + 1
l = i + 1
elif i + 1 > p and r == p:
r = i + 1
X = i + 1
Count += C
print(Count + min(abs(X - p) + X - M, X + p - M * 2)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING NUMBER NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = [int(i) for i in input().split()]
str = input()
p -= 1
if p != n / 2 and p == n // 2:
press = 0
for i in range(p + 1, n):
press += 1
if str[i] != str[n - i - 1]:
s = min(ord(str[i]), ord(str[n - i - 1]))
g = max(ord(str[i]), ord(str[n - i - 1]))
press += min(g - s, 122 - g + s - 96)
print(press)
exit()
lL = 0
rL = n
if p < n // 2:
rL = n // 2
else:
lL = n // 2
if n % 2 == 1:
lL += 1
l, r = p, p
press = 0
for i in range(lL, rL):
if str[i] != str[n - i - 1]:
l = min(l, i)
r = max(r, i)
s = min(ord(str[i]), ord(str[n - i - 1]))
g = max(ord(str[i]), ord(str[n - i - 1]))
press += min(g - s, 122 - g + s - 96)
press += 2 * min(p - l, r - p) + max(p - l, r - p)
print(press) | ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | def dosomething(a, b):
m1 = min(a, b)
m2 = max(a, b)
m1 = ord(m1) - 97
m2 = 122 - ord(m2)
return min(abs(ord(a) - ord(b)), m1 + m2 + 1)
arr = [int(x) for x in input().split()]
n, k = arr[0], arr[1]
s = input()
mid = n // 2
if n == 1:
print(0)
else:
if n & 1 == 1:
if k == mid:
s1 = s[:mid]
s2 = s[mid + 1 :]
ans = 2
elif k > mid:
s1 = s[mid + 1 :]
k -= mid + 1
s2 = s[:mid]
ans = 0
else:
s1 = s[:mid]
s2 = s[mid + 1 :]
ans = 0
elif k <= mid:
s1 = s[:mid]
s2 = s[mid:]
ans = 0
else:
s1 = s[mid:]
s2 = s[:mid]
k -= mid
ans = 0
s2 = s2[::-1]
if k >= len(s1) // 2:
i = k - 1
c = 0
ans1 = ans
last = 0
while i < len(s1):
if s1[i] != s2[i]:
ans += c + dosomething(s1[i], s2[i])
last = i
c = 1
i += 1
else:
c += 1
i += 1
i = k - 2
c = 1
if last != 0:
ad = last - k + 1
else:
ad = 0
while i >= 0:
if s1[i] != s2[i]:
ans += c + dosomething(s1[i], s2[i]) + ad
ad = 0
i -= 1
c = 1
else:
c += 1
i -= 1
print(ans)
else:
i = k - 1
c = 0
ans1 = ans
last = k - 1
while i >= 0:
if s1[i] != s2[i]:
ans += c + dosomething(s1[i], s2[i])
last = i
c = 1
i -= 1
else:
c += 1
i -= 1
i = k
c = 1
if last != k - 1:
ad = k - 1 - last
else:
ad = 0
while i < len(s1):
if s1[i] != s2[i]:
ans += c + dosomething(s1[i], s2[i]) + ad
ad = 0
i += 1
c = 1
else:
c += 1
i += 1
print(ans) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
s = [ord(c) for c in input()]
ans = 0
left = right = -1
for i in range(n // 2):
diff = abs(s[i] - s[n - i - 1])
ans += min(diff, 26 - diff)
if s[i] != s[n - i - 1]:
if left == -1:
left = i
right = i
if left == -1:
print(0)
else:
p -= 1
n -= 1
if p > n // 2:
p = n - p
ans += min(abs(p - left), abs(right - p))
ans += right - left
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | ad = ord("z") - ord("a") + 1
def mc(a, b):
pd = abs(ord(a) - ord(b))
return min(pd, ad - pd)
def fd(v, r):
for c in r:
if not v[c]:
return c
def mp(s, n, p):
cl = n // 2
v = [(s[c] == s[-(c + 1)]) for c in range(cl)]
rv = range(cl)
if p >= cl:
p = n - p - 1
return fd(v, rv), fd(v, rv[::-1]), p
def d(s, n, p):
l, r, p = mp(s, n, p)
if l == None:
return 0
t = 0
for c in range(l, r + 1):
t += mc(s[c], s[-(c + 1)])
lp, rp = abs(p - l), abs(r - p)
if l < p < r:
if lp > rp:
lp, rp = rp, lp
t += 2 * lp + rp
else:
t += r - l + min(lp, rp)
return t
n, p = [int(c) for c in input().split()]
s = input()
print(d(s, n, p - 1)) | ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR STRING NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF FOR VAR VAR IF VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
p = p - 1
a = input()
fn = 0
ln = n // 2
sum_val = 0
allzero = True
flag = True
for i in range(n // 2):
diff = abs(ord(a[i]) - ord(a[n - i - 1]))
val = min(diff, 26 - diff)
sum_val += val
if val == 0 and flag:
fn += 1
else:
flag = False
if val != 0:
ln = i
allzero = False
if p >= n // 2:
p = n - p - 1
if allzero:
print(0)
elif p > ln:
print(sum_val + p - fn)
elif p < fn:
print(sum_val + ln - p)
else:
print(sum_val + min(ln - p, p - fn) + ln - fn) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | lis = "abcdefghijklmnopqrstuvwxyz"
def dis(a, b):
aa = lis.index(a)
bb = lis.index(b)
m = 27
m1 = 27
ans = 100
for i in range(26):
if aa < i:
m = min(i - aa, 26 - i + aa)
else:
m = min(aa - i, 26 - aa + i)
if bb < i:
m1 = min(i - bb, 26 - i + bb)
else:
m1 = min(bb - i, 26 - bb + i)
ans = min(ans, m + m1)
return ans
n, p = map(int, input().split())
s = input()
if n == 1 and p == 1:
print(0)
exit()
ans = 0
for i in range(n // 2):
ans += dis(s[i], s[n - i - 1])
i = 0
j = n // 2 - 1
if p > n // 2:
p = n - p
else:
p -= 1
while i < min(p, n // 2) and s[i] == s[n - i - 1]:
i += 1
while p < j and s[j] == s[n - j - 1]:
j -= 1
ans += min(p - i, j - p) + j - i
print(ans) | ASSIGN VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = input().split()
n = int(n)
p = int(p)
s = input()
p -= 1
c = 0
p = min(p, n - 1 - p)
l = p
r = p
for i in range(p - 1, -1, -1):
if s[i] != s[n - i - 1]:
l = i
c += min(
abs(ord(s[i]) - ord(s[n - i - 1])),
abs(26 - abs(ord(s[i]) - ord(s[n - i - 1]))),
)
for i in range(p, int(n / 2)):
if s[i] != s[n - i - 1]:
r = i
c += min(
abs(ord(s[i]) - ord(s[n - i - 1])),
abs(26 - abs(ord(s[i]) - ord(s[n - i - 1]))),
)
l = p - l
r = r - p
c += 2 * min(l, r) + max(l, r)
print(c) | ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
p -= 1
a = list(input())
if p >= n // 2:
p = n - 1 - p
leftSum = 0
mLeft = -1
rightSum = 0
mRight = p
for i in range((n + 1) // 2):
if a[i] != a[n - 1 - i]:
temp = min(
abs(ord(a[i]) - ord(a[n - 1 - i])),
1
+ ord("z")
- max(ord(a[i]), ord(a[n - 1 - i]))
+ min(ord(a[i]), ord(a[n - 1 - i]))
- ord("a"),
)
if i <= p:
if mLeft == -1:
mLeft = i
leftSum += temp
else:
mRight = i
rightSum += temp
tot = rightSum + leftSum
if mLeft == -1:
mLeft = p
print(min(2 * (p - mLeft) + mRight - p, p - mLeft + 2 * (mRight - p)) + tot) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR STRING IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP NUMBER BIN_OP VAR VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, pos = map(int, input().split())
pos -= 1
s = input().rstrip()
cost = [0] * n
left, right = [], []
for i in range(n // 2):
cost[i] = abs(ord(s[i]) - ord(s[n - i - 1]))
cost[i] = min(cost[i], abs(26 - cost[i]))
cost[n - i - 1] = cost[i]
if cost[i]:
left.append(i)
right.append(n - i - 1)
if pos > n // 2:
pos = n - pos - 1
if not left:
print(0)
exit()
if left[0] >= pos:
ret = left[-1] - pos
elif left[-1] <= pos:
ret = pos - left[0]
else:
ret = min(left[-1] - pos + left[-1] - left[0], pos - left[0] + left[-1] - left[0])
print(min(ret, right[0] - pos) + sum(cost) // 2) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | import sys
input = sys.stdin.readline
n, p = map(int, input().split())
s = input().rstrip()
if p >= n // 2:
s = s[::-1]
p = n + 1 - p
p -= 1
ans1 = 0
ans2 = 0
l = 10**6
r = -1
for i in range(n // 2):
if s[i] != s[n - 1 - i]:
l = min(l, i)
r = max(r, i)
use = set()
if l <= p <= r:
if r != -1:
for i in range(p, r + 1):
if s[i] != s[n - 1 - i] and i not in use:
ans1 += min(
abs(ord(s[i]) - ord(s[n - 1 - i])),
26 - abs(ord(s[i]) - ord(s[n - 1 - i])),
)
ans2 += min(
abs(ord(s[i]) - ord(s[n - 1 - i])),
26 - abs(ord(s[i]) - ord(s[n - 1 - i])),
)
use.add(i)
ans1 += 2 * abs(r - p)
ans2 += abs(r - p)
if l != 10**6:
for i in range(p, l - 1, -1):
if s[i] != s[n - 1 - i] and i not in use:
ans1 += min(
abs(ord(s[i]) - ord(s[n - 1 - i])),
26 - abs(ord(s[i]) - ord(s[n - 1 - i])),
)
ans2 += min(
abs(ord(s[i]) - ord(s[n - 1 - i])),
26 - abs(ord(s[i]) - ord(s[n - 1 - i])),
)
use.add(i)
ans1 += abs(p - l)
ans2 += 2 * abs(p - l)
ans = min(ans1, ans2)
print(ans)
elif p <= l:
if r != -1:
for i in range(p, r + 1):
if s[i] != s[n - 1 - i]:
ans2 += min(
abs(ord(s[i]) - ord(s[n - 1 - i])),
26 - abs(ord(s[i]) - ord(s[n - 1 - i])),
)
ans2 += abs(r - p)
print(ans2)
else:
if l != 10**6:
for i in range(p, l - 1, -1):
if s[i] != s[n - 1 - i]:
ans1 += min(
abs(ord(s[i]) - ord(s[n - 1 - i])),
26 - abs(ord(s[i]) - ord(s[n - 1 - i])),
)
ans1 += abs(p - l)
print(ans1) | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = list(map(int, input().split()))
string = input()
n, p = n - 1, p - 1
if p > n // 2:
p = n - p
ans = 0
left, right = -1, -1
for i in range(n // 2 + 1):
ch, corresponding = string[i], string[n - i]
if ch != corresponding:
if left == -1:
left = i
else:
right = i
t = abs(ord(ch) - ord(corresponding))
t = min(t, 26 - t)
ans += t
if left != -1 and right != -1:
ans += right - left
mn = float("inf")
if left != -1:
mn = min(mn, abs(p - left))
if right != -1:
mn = min(mn, abs(right - p))
if mn != float("inf"):
ans += mn
print(ans) | ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR STRING VAR VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | def change(a, b):
a, b = ord(a) - 96, ord(b) - 96
a, b = max(a, b), min(a, b)
return min(a - b, 26 + b - a)
def mini(arr, p):
if len(arr) == 0:
return 0
if arr[-1] <= p:
return p - arr[0]
if arr[0] >= p:
return arr[-1] - p
return min(arr[-1] - p, p - arr[0]) + (arr[-1] - arr[0])
def func(s, p):
mid = (len(s) + 1) / 2
if p > mid:
p = mid - (p - mid)
mid = int(len(s) / 2)
steps = 0
mis = []
for i in range(mid):
if s[i] != s[len(s) - 1 - i]:
steps += change(s[i], s[len(s) - 1 - i])
mis.append(i + 1)
steps = steps + mini(mis, p)
return steps
l = list(map(int, input().split()))
pos = l[1]
s = input()
ans = func(s, pos)
print(int(ans)) | FUNC_DEF ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR RETURN BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR RETURN BIN_OP VAR NUMBER VAR RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | def getUnpaired(s):
unpaired = set()
for i in range(0, n // 2):
if s[i] != s[n - i - 1]:
unpaired.add(i)
return unpaired
def getDist(c1, c2):
dist1 = abs(ord(c1) - ord(c2))
dist2 = abs(ord(c1) + 26 - ord(c2))
dist3 = abs(ord(c2) + 26 - ord(c1))
return min(dist1, dist2, dist3)
def tryLR(p, s, unpaired):
done = set()
moves = 0
try:
l, r = min(unpaired), max(unpaired)
except:
return 0
while p >= l:
if p in unpaired and p not in done:
moves += getDist(s[p], s[len(s) - p - 1])
done.add(p)
if p == l:
break
p -= 1
moves += 1
while p <= r:
if p in unpaired and p not in done:
moves += getDist(s[p], s[len(s) - p - 1])
done.add(p)
if p == r:
break
p += 1
moves += 1
return moves
def tryRL(p, s, unpaired):
done = set()
moves = 0
try:
l, r = min(unpaired), max(unpaired)
except:
return 0
while p <= r:
if p in unpaired and p not in done:
moves += getDist(s[p], s[len(s) - p - 1])
done.add(p)
if p == r:
break
p += 1
moves += 1
while p >= l:
if p in unpaired and p not in done:
moves += getDist(s[p], s[len(s) - p - 1])
done.add(p)
if p == l:
break
p -= 1
moves += 1
return moves
n, p = (int(x) for x in input().split())
s = input()
p -= 1
if p >= n // 2:
p = n - p - 1
unpaired = getUnpaired(s)
print(min(tryLR(p, s, unpaired), tryRL(p, s, unpaired))) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
l = list(input())
flag = 0
min1 = max1 = -1
sum = 0
for i in range(n // 2):
if l[i] != l[n - 1 - i]:
if flag == 0:
min1 = i + 1
flag = 1
max1 = i + 1
s1 = abs(ord(l[i]) - ord(l[n - 1 - i]))
s2 = 26 - s1
sum += min(s1, s2)
if p > n // 2:
p = n + 1 - p
if max1 != -1:
sum += min(abs(max1 - p), abs(p - min1))
sum += max1 - min1
print(sum) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | for _ in range(1):
n, p = [int(i) for i in input().split()]
if n - p < p - 1:
p = 1 + n - p
p -= 1
s = input()
to_go = int(n / 2) - 1
i = 0
j = n - 1
l = []
while i <= to_go:
if s[i] != s[j]:
l.append(i)
i += 1
j -= 1
if len(l) == 0:
print(0)
continue
elif len(l) == 1:
ind = l[0]
a = ord(s[ind])
b = ord(s[n - 1 - ind])
print(abs(p - l[0]) + min(abs(a - b), 26 - abs(a - b)))
continue
e1 = l[0]
e2 = l[-1]
ans = 0
if p == e1 or p == e2:
e1 -= p
e2 -= p
ans += abs(e1 + e2)
elif p < e1:
ans += e2 - p
elif p > e2:
ans += p - e1
elif p > e1 and p < e2:
d1 = e2 - p
d2 = p - e1
m = min(d1, d2)
ans += 2 * m
if d1 == m:
ans += d2
else:
ans += d1
for i in l:
i1 = n - 1 - i
x = abs(ord(s[i]) - ord(s[i1]))
ans += min(x, 26 - x)
print(ans) | FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP NUMBER VAR IF VAR VAR VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | def dc(c, d):
x, y = ord(c) - ord("a"), ord(d) - ord("a")
return min(abs(x - y), 26 - max(x, y) + min(x, y))
def ds(s1, s2):
r = 0
a, b = 10**5, -1
for i in range(len(s1)):
c, d = s1[i], s2[i]
r += dc(c, d)
if c != d:
a = min(a, i)
b = max(b, i)
return a, b, r
n, p = list(map(int, input().split()))
s = input()
p -= 1
m = n // 2
y = m + (1 if n % 2 else 0)
s1, s2 = s[:m], s[y:][::-1]
a, b, r = ds(s1, s2)
d1 = min(abs(p - a), abs(p - b)) + abs(a - b)
d2 = min(abs(p - y - m + 1 + a), abs(p - y - m + 1 + b)) + abs(a - b)
if r == 0:
print(0)
else:
print(min(d1, d2) + r) | FUNC_DEF ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
s = list(input())
p -= 1
d = [0] * (n // 2 + 1)
for i in range(n // 2):
x = ord(s[i]) - ord(s[n - i - 1])
d[i] = min(abs(x), 26 - abs(x))
if p >= n // 2:
p = n - 1 - p
if sum(d) == 0:
print(0)
exit()
i, j = 0, n // 2
while d[i] == 0:
i += 1
while d[j] == 0:
j -= 1
if i > p:
i = p
if j < p:
j = p
print(sum(d) + min(p - 2 * i + j, 2 * j - i - p)) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | def calc(c1, c2):
x = abs(ord(c1) - ord(c2))
return min(x, 26 - x)
a, b = map(int, input().split(" "))
strx = input()
if a % 2 == 0:
if b > a // 2:
b = a + 1 - b
left = strx[: a // 2]
right = strx[a // 2 :]
right = right[::-1]
dist = [calc(left[i], right[i]) for i in range(a // 2)]
indx = b - 1
rightpt = b - 1
leftpt = b - 1
for i in range(a // 2):
if dist[i] != 0:
leftpt = i
break
dist.reverse()
for i in range(a // 2):
if dist[i] != 0:
rightpt = a // 2 - 1 - i
break
sumlr = rightpt - leftpt + min(abs(leftpt - indx), abs(rightpt - indx))
print(sum(dist) + sumlr)
else:
if b > a // 2 + 1:
b = a + 1 - b
left = strx[: a // 2]
right = strx[a // 2 :][1:]
right = right[::-1]
dist = [calc(left[i], right[i]) for i in range(a // 2)]
indx = b - 1
rightpt = b - 1
leftpt = b - 1
for i in range(a // 2):
if dist[i] != 0:
leftpt = i
break
dist.reverse()
for i in range(a // 2):
if dist[i] != 0:
rightpt = a // 2 - 1 - i
break
sumlr = rightpt - leftpt + min(abs(leftpt - indx), abs(rightpt - indx))
print(sum(dist) + sumlr) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | def distchar(a, b):
a = ord(a) - ord("a")
b = ord(b) - ord("a")
if a > b:
a, b = b, a
return min(b - a, a + 26 - b)
def dist(i, p, n):
if p >= i:
sign = 1
else:
sign = -1
return sign * min(abs(p - i), abs(n - 1 - i - p))
[n, p] = [int(s) for s in input().split()]
s = input().strip()
p = p - 1
count1 = 0
d = [(0) for i in range(n)]
for i in range(n // 2):
di = distchar(s[i], s[n - 1 - i])
if di > 0:
d[i], d[n - 1 - i] = di, di
count1 += di
if p > n // 2:
p = n - 1 - p
i1 = 0
while i1 < p and d[i1] == 0:
i1 += 1
i2 = (n - 1) // 2
while i2 > p and d[i2] == 0:
i2 -= 1
d1 = dist(i1, p, n)
d2 = dist(i2, p, n)
if d1 * d2 > 0:
count = count1 + max(abs(d1), abs(d2))
else:
count = count1 + 2 * min(abs(d1), abs(d2)) + max(abs(d1), abs(d2))
print(count) | FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER RETURN BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | from sys import stdin, stdout
def main():
from sys import stdin, stdout
n, o = list(map(int, stdin.readline().split()))
string = stdin.readline().strip()
if n & 1:
s1 = string[: n // 2]
s2 = string[n // 2 + 1 :]
rev = s2[::-1]
minim = 0
indexlist = []
for i in range(n // 2):
k = abs(ord(s1[i]) - ord(rev[i]))
minim += min(k, 26 - k)
if k:
indexlist.append(i)
if o > n // 2:
o = n - o
else:
o -= 1
if len(indexlist):
indexlist += [o]
for i in range(len(indexlist)):
indexlist[i] = indexlist[i] - o
minia = min(indexlist)
maxia = max(indexlist)
minim += min(abs(minia), abs(maxia)) * 2 + max(abs(minia), abs(maxia))
stdout.write(str(minim))
else:
s1 = string[: n // 2]
s2 = string[n // 2 :]
rev = s2[::-1]
minim = 0
indexlist = []
for i in range(n // 2):
k = abs(ord(s1[i]) - ord(rev[i]))
minim += min(k, 26 - k)
if k:
indexlist.append(i)
if o > n // 2:
o = n - o
else:
o -= 1
if len(indexlist):
indexlist += [o]
for i in range(len(indexlist)):
indexlist[i] = indexlist[i] - o
minia = min(indexlist)
maxia = max(indexlist)
minim += min(abs(minia), abs(maxia)) * 2 + max(abs(minia), abs(maxia))
stdout.write(str(minim))
def __starting_point():
main()
__starting_point() | FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR LIST VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR LIST VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().strip().split())
arr = input()
lis = []
flag = 0
mins = -1
maxs = -1
f = 0
sums = 0
for x in range(n // 2):
if arr[x] != arr[-(x + 1)]:
if f == 0:
mins = x
f = 1
if maxs < x:
maxs = x
if abs(ord(arr[x]) - ord(arr[-(x + 1)])) <= 13:
sums += abs(ord(arr[x]) - ord(arr[-(x + 1)]))
else:
sums += 26 - abs(ord(arr[x]) - ord(arr[-(x + 1)]))
if n % 2 == 1:
if p > n // 2 + 1:
p = n - p + 1
elif p > n // 2:
p = n - p + 1
if mins == -1:
print("0")
else:
if mins < p - 1 < maxs:
sums += abs(maxs - mins) + min(abs(p - 1 - maxs), abs(p - 1 - mins))
elif p - 1 <= mins:
sums += abs(maxs - (p - 1))
elif p - 1 >= maxs:
sums += abs(p - 1 - mins)
print(sums) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, p = map(int, input().split())
s = input()
p = n - p if p > n // 2 else p - 1
left, right, steps = n // 2, -1, 0
for i in range(n // 2):
x = abs(ord(s[i]) - ord(s[-i - 1]))
steps += min(x, 26 - x)
if x != 0:
left = min(left, i)
right = max(right, i)
if steps:
print(steps + right - left + min(abs(p - left), abs(right - p)))
else:
print(0) | ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER |
Nam is playing with a string on his computer. The string consists of n lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down.
There is a cursor pointing at some symbol of the string. Suppose that cursor is at position i (1 ≤ i ≤ n, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position i - 1 if i > 1 or to the end of the string (i. e. position n) otherwise. The same holds when he presses the right arrow key (if i = n, the cursor appears at the beginning of the string).
When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key.
Initially, the text cursor is at position p.
Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome?
-----Input-----
The first line contains two space-separated integers n (1 ≤ n ≤ 10^5) and p (1 ≤ p ≤ n), the length of Nam's string and the initial position of the text cursor.
The next line contains n lowercase characters of Nam's string.
-----Output-----
Print the minimum number of presses needed to change string into a palindrome.
-----Examples-----
Input
8 3
aeabcaez
Output
6
-----Note-----
A string is a palindrome if it reads the same forward or reversed.
In the sample test, initial Nam's string is: $\text{aeabcaez}$ (cursor position is shown bold).
In optimal solution, Nam may do 6 following steps:[Image]
The result, $\text{zeaccaez}$, is now a palindrome. | n, m = input().split(" ")
n = int(n)
m = int(m)
s = input()
a = n // 2
ans = []
for i in range(0, a):
ans.append(
min(abs(ord(s[i]) - ord(s[n - i - 1])), 26 - abs(ord(s[i]) - ord(s[n - i - 1])))
)
x = sum(ans)
if m > a:
m = n + 1 - m
g1 = 0
g2 = a - 1
d = 0
for i in range(0, a):
if ans[i] != 0:
g1 = i
d = 1
break
for i in range(0, a):
if ans[a - i - 1] != 0:
g2 = a - i - 1
d = 1
break
g1 += 1
g2 += 1
if g1 == 1 and g2 == a and d == 0:
pass
elif m >= g2:
x += m - g1
elif m <= g1:
x += g2 - m
else:
x += g2 - g1 + min(m - g1, g2 - m)
print(x) | ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
An integer sequence is called beautiful if the difference between any two consecutive numbers is equal to $1$. More formally, a sequence $s_1, s_2, \ldots, s_{n}$ is beautiful if $|s_i - s_{i+1}| = 1$ for all $1 \leq i \leq n - 1$.
Trans has $a$ numbers $0$, $b$ numbers $1$, $c$ numbers $2$ and $d$ numbers $3$. He wants to construct a beautiful sequence using all of these $a + b + c + d$ numbers.
However, it turns out to be a non-trivial task, and Trans was not able to do it. Could you please help Trans?
-----Input-----
The only input line contains four non-negative integers $a$, $b$, $c$ and $d$ ($0 < a+b+c+d \leq 10^5$).
-----Output-----
If it is impossible to construct a beautiful sequence satisfying the above constraints, print "NO" (without quotes) in one line.
Otherwise, print "YES" (without quotes) in the first line. Then in the second line print $a + b + c + d$ integers, separated by spaces — a beautiful sequence. There should be $a$ numbers equal to $0$, $b$ numbers equal to $1$, $c$ numbers equal to $2$ and $d$ numbers equal to $3$.
If there are multiple answers, you can print any of them.
-----Examples-----
Input
2 2 2 1
Output
YES
0 1 0 1 2 3 2
Input
1 2 3 4
Output
NO
Input
2 2 2 3
Output
NO
-----Note-----
In the first test, it is easy to see, that the sequence is beautiful because the difference between any two consecutive numbers is equal to $1$. Also, there are exactly two numbers, equal to $0$, $1$, $2$ and exactly one number, equal to $3$.
It can be proved, that it is impossible to construct beautiful sequences in the second and third tests. | def solve():
s = input()
if "aa" in s or "bb" in s or "cc" in s:
print(-1)
return
syms = s + "@"
ans = ["@"]
for i, sym in enumerate(s):
if sym != "?":
ans.append(sym)
continue
for x in "abc":
if x != ans[-1] and x != syms[i + 1]:
ans.append(x)
break
print("".join(ans[1:]))
def solveb():
n = int(input())
perm = [int(x) for x in input().split()]
num___idx = [(-1) for _ in range(n + 1)]
for i, num in enumerate(perm):
num___idx[num] = i
curr_max = -1
curr_min = 2 * n
num___pretty = [(0) for _ in range(n + 1)]
for num in range(1, n + 1):
curr_max = max(num___idx[num], curr_max)
curr_min = min(num___idx[num], curr_min)
if curr_max - curr_min + 1 == num:
num___pretty[num] = 1
print(*num___pretty[1:], sep="")
def solvec():
n = int(input())
rank___problems_nr = [int(x) for x in input().split()]
weird_prefsums = [0]
last_num = rank___problems_nr[0]
for num in rank___problems_nr:
if num != last_num:
last_num = num
weird_prefsums.append(weird_prefsums[-1])
weird_prefsums[-1] += 1
gold = weird_prefsums[0]
silvers = 0
i = 1
for i in range(1, len(weird_prefsums)):
x = weird_prefsums[i]
if x - gold > gold:
silvers = x - gold
break
bronzes = 0
for j in range(i, len(weird_prefsums)):
x = weird_prefsums[j]
if x > n / 2:
break
if x - gold - silvers > gold:
bronzes = x - gold - silvers
if bronzes == 0 or silvers == 0:
print(0, 0, 0)
return
print(gold, silvers, bronzes)
def solved():
a, b, c, d = (int(x) for x in input().split())
ab_len = min(a, b)
a -= ab_len
b -= ab_len
cd_len = min(c, d)
c -= cd_len
d -= cd_len
if a == 1 and cd_len == 0 and d == 0 and c == 0:
print("YES")
print("0 1 " * ab_len + "0")
return
if d == 1 and ab_len == 0 and a == 0 and b == 0:
print("YES")
print("3 " + "2 3 " * cd_len)
return
if a > 0 or d > 0:
print("NO")
return
cb_len = min(b, c)
b -= cb_len
c -= cb_len
if b > 1 or c > 1:
print("NO")
return
print("YES")
print("1 " * b + "0 1 " * ab_len + "2 1 " * cb_len + "2 3 " * cd_len + "2" * c)
solved() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR IF STRING VAR STRING VAR STRING VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR LIST STRING FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR STRING IF VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER NUMBER RETURN EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING VAR STRING RETURN IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP STRING VAR RETURN IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR BIN_OP STRING VAR BIN_OP STRING VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR |
An integer sequence is called beautiful if the difference between any two consecutive numbers is equal to $1$. More formally, a sequence $s_1, s_2, \ldots, s_{n}$ is beautiful if $|s_i - s_{i+1}| = 1$ for all $1 \leq i \leq n - 1$.
Trans has $a$ numbers $0$, $b$ numbers $1$, $c$ numbers $2$ and $d$ numbers $3$. He wants to construct a beautiful sequence using all of these $a + b + c + d$ numbers.
However, it turns out to be a non-trivial task, and Trans was not able to do it. Could you please help Trans?
-----Input-----
The only input line contains four non-negative integers $a$, $b$, $c$ and $d$ ($0 < a+b+c+d \leq 10^5$).
-----Output-----
If it is impossible to construct a beautiful sequence satisfying the above constraints, print "NO" (without quotes) in one line.
Otherwise, print "YES" (without quotes) in the first line. Then in the second line print $a + b + c + d$ integers, separated by spaces — a beautiful sequence. There should be $a$ numbers equal to $0$, $b$ numbers equal to $1$, $c$ numbers equal to $2$ and $d$ numbers equal to $3$.
If there are multiple answers, you can print any of them.
-----Examples-----
Input
2 2 2 1
Output
YES
0 1 0 1 2 3 2
Input
1 2 3 4
Output
NO
Input
2 2 2 3
Output
NO
-----Note-----
In the first test, it is easy to see, that the sequence is beautiful because the difference between any two consecutive numbers is equal to $1$. Also, there are exactly two numbers, equal to $0$, $1$, $2$ and exactly one number, equal to $3$.
It can be proved, that it is impossible to construct beautiful sequences in the second and third tests. | a = list(map(int, input().split()))
ref = [0, 1, 2, 3]
x = sum(a)
for i in range(4):
b = a[:]
res = []
for j in range(x):
b[i] -= 1
res.append(ref[i])
if i == 0:
i = 1
elif i == 3:
i = 2
elif i == 1:
if b[0] > 0:
i = 0
else:
i = 2
elif b[3] > 0:
i = 3
else:
i = 1
if max(b) == 0 and min(b) == 0:
print("YES")
print(*res)
exit()
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING |
An integer sequence is called beautiful if the difference between any two consecutive numbers is equal to $1$. More formally, a sequence $s_1, s_2, \ldots, s_{n}$ is beautiful if $|s_i - s_{i+1}| = 1$ for all $1 \leq i \leq n - 1$.
Trans has $a$ numbers $0$, $b$ numbers $1$, $c$ numbers $2$ and $d$ numbers $3$. He wants to construct a beautiful sequence using all of these $a + b + c + d$ numbers.
However, it turns out to be a non-trivial task, and Trans was not able to do it. Could you please help Trans?
-----Input-----
The only input line contains four non-negative integers $a$, $b$, $c$ and $d$ ($0 < a+b+c+d \leq 10^5$).
-----Output-----
If it is impossible to construct a beautiful sequence satisfying the above constraints, print "NO" (without quotes) in one line.
Otherwise, print "YES" (without quotes) in the first line. Then in the second line print $a + b + c + d$ integers, separated by spaces — a beautiful sequence. There should be $a$ numbers equal to $0$, $b$ numbers equal to $1$, $c$ numbers equal to $2$ and $d$ numbers equal to $3$.
If there are multiple answers, you can print any of them.
-----Examples-----
Input
2 2 2 1
Output
YES
0 1 0 1 2 3 2
Input
1 2 3 4
Output
NO
Input
2 2 2 3
Output
NO
-----Note-----
In the first test, it is easy to see, that the sequence is beautiful because the difference between any two consecutive numbers is equal to $1$. Also, there are exactly two numbers, equal to $0$, $1$, $2$ and exactly one number, equal to $3$.
It can be proved, that it is impossible to construct beautiful sequences in the second and third tests. | from sys import stdin, stdout
read = lambda: map(int, stdin.readline().split())
I = lambda: stdin.readline()
a, b, c, d = read()
ind = 0 if a + c < b + d else 1
res = [(0) for i in range(a)] + [(2) for i in range(c)]
for i in range(b):
res.insert(ind, 1)
ind += 2
for j in range(d):
res.insert(ind, 3)
ind += 2
for i in range(1, len(res)):
if abs(res[i] - res[i - 1]) != 1:
print("NO")
break
else:
print("YES")
print(*res) | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
An integer sequence is called beautiful if the difference between any two consecutive numbers is equal to $1$. More formally, a sequence $s_1, s_2, \ldots, s_{n}$ is beautiful if $|s_i - s_{i+1}| = 1$ for all $1 \leq i \leq n - 1$.
Trans has $a$ numbers $0$, $b$ numbers $1$, $c$ numbers $2$ and $d$ numbers $3$. He wants to construct a beautiful sequence using all of these $a + b + c + d$ numbers.
However, it turns out to be a non-trivial task, and Trans was not able to do it. Could you please help Trans?
-----Input-----
The only input line contains four non-negative integers $a$, $b$, $c$ and $d$ ($0 < a+b+c+d \leq 10^5$).
-----Output-----
If it is impossible to construct a beautiful sequence satisfying the above constraints, print "NO" (without quotes) in one line.
Otherwise, print "YES" (without quotes) in the first line. Then in the second line print $a + b + c + d$ integers, separated by spaces — a beautiful sequence. There should be $a$ numbers equal to $0$, $b$ numbers equal to $1$, $c$ numbers equal to $2$ and $d$ numbers equal to $3$.
If there are multiple answers, you can print any of them.
-----Examples-----
Input
2 2 2 1
Output
YES
0 1 0 1 2 3 2
Input
1 2 3 4
Output
NO
Input
2 2 2 3
Output
NO
-----Note-----
In the first test, it is easy to see, that the sequence is beautiful because the difference between any two consecutive numbers is equal to $1$. Also, there are exactly two numbers, equal to $0$, $1$, $2$ and exactly one number, equal to $3$.
It can be proved, that it is impossible to construct beautiful sequences in the second and third tests. | a, b, c, d = map(int, input().split())
seq = []
nums = [a, b, c, d]
def solve(a, b, c, d, second):
for i in range(3):
while nums[i] != 0 and nums[i + 1] != 0:
if len(seq) == 0:
if second:
seq.append(i + 1)
nums[i + 1] -= 1
else:
seq.append(i)
nums[i] -= 1
elif nums[i] > 0 and (seq[-1] == i - 1 or seq[-1] == i + 1):
seq.append(i)
nums[i] -= 1
elif nums[i + 1] > 0 and seq[-1] == i:
seq.append(i + 1)
nums[i + 1] -= 1
else:
break
for i in range(4):
while nums[i] > 0:
if len(seq) == 0 or seq[-1] == i - 1 or seq[-1] == i + 1:
seq.append(i)
nums[i] -= 1
elif seq[0] == i - 1 or seq[0] == i + 1:
seq.insert(0, i)
nums[i] -= 1
else:
break
if sum(nums) == 0:
print("YES")
print(" ".join([str(a) for a in seq]))
return True
else:
return False
if not solve(a, b, c, d, True):
if not solve(a, b, c, d, False):
print("NO") | ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST VAR VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER WHILE VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER WHILE VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING |
An integer sequence is called beautiful if the difference between any two consecutive numbers is equal to $1$. More formally, a sequence $s_1, s_2, \ldots, s_{n}$ is beautiful if $|s_i - s_{i+1}| = 1$ for all $1 \leq i \leq n - 1$.
Trans has $a$ numbers $0$, $b$ numbers $1$, $c$ numbers $2$ and $d$ numbers $3$. He wants to construct a beautiful sequence using all of these $a + b + c + d$ numbers.
However, it turns out to be a non-trivial task, and Trans was not able to do it. Could you please help Trans?
-----Input-----
The only input line contains four non-negative integers $a$, $b$, $c$ and $d$ ($0 < a+b+c+d \leq 10^5$).
-----Output-----
If it is impossible to construct a beautiful sequence satisfying the above constraints, print "NO" (without quotes) in one line.
Otherwise, print "YES" (without quotes) in the first line. Then in the second line print $a + b + c + d$ integers, separated by spaces — a beautiful sequence. There should be $a$ numbers equal to $0$, $b$ numbers equal to $1$, $c$ numbers equal to $2$ and $d$ numbers equal to $3$.
If there are multiple answers, you can print any of them.
-----Examples-----
Input
2 2 2 1
Output
YES
0 1 0 1 2 3 2
Input
1 2 3 4
Output
NO
Input
2 2 2 3
Output
NO
-----Note-----
In the first test, it is easy to see, that the sequence is beautiful because the difference between any two consecutive numbers is equal to $1$. Also, there are exactly two numbers, equal to $0$, $1$, $2$ and exactly one number, equal to $3$.
It can be proved, that it is impossible to construct beautiful sequences in the second and third tests. | a, b, c, d = map(int, input().split())
p = 0
e = []
if a > b:
if c == 0 and d == 0 and a == b + 1:
for j in range(a + b):
if j % 2 == 0:
e.append(0)
else:
e.append(1)
else:
p = 1
elif d > c:
if a == 0 and b == 0 and d == c + 1:
for j in range(c + d):
if j % 2 == 0:
e.append(3)
else:
e.append(2)
else:
p = 1
else:
g = []
f = []
i = []
for j in range(2 * a):
if j % 2 == 0:
g.append(0)
else:
g.append(1)
for j in range(2 * d):
if j % 2 == 0:
f.append(2)
else:
f.append(3)
b += -a
c += -d
if abs(b - c) > 1:
p = 1
else:
if abs(b - c) == 0:
for j in range(b + c):
if j % 2 == 0:
i.append(2)
else:
i.append(1)
if b == c + 1:
for j in range(2 * c):
if j % 2 == 0:
i.append(2)
else:
i.append(1)
g.insert(0, 1)
elif c == b + 1:
for j in range(2 * b):
if j % 2 == 0:
i.append(2)
else:
i.append(1)
f.append(2)
e = g + i + f
if p == 1:
print("NO")
else:
print("YES")
print(*e) | ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST IF VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
An integer sequence is called beautiful if the difference between any two consecutive numbers is equal to $1$. More formally, a sequence $s_1, s_2, \ldots, s_{n}$ is beautiful if $|s_i - s_{i+1}| = 1$ for all $1 \leq i \leq n - 1$.
Trans has $a$ numbers $0$, $b$ numbers $1$, $c$ numbers $2$ and $d$ numbers $3$. He wants to construct a beautiful sequence using all of these $a + b + c + d$ numbers.
However, it turns out to be a non-trivial task, and Trans was not able to do it. Could you please help Trans?
-----Input-----
The only input line contains four non-negative integers $a$, $b$, $c$ and $d$ ($0 < a+b+c+d \leq 10^5$).
-----Output-----
If it is impossible to construct a beautiful sequence satisfying the above constraints, print "NO" (without quotes) in one line.
Otherwise, print "YES" (without quotes) in the first line. Then in the second line print $a + b + c + d$ integers, separated by spaces — a beautiful sequence. There should be $a$ numbers equal to $0$, $b$ numbers equal to $1$, $c$ numbers equal to $2$ and $d$ numbers equal to $3$.
If there are multiple answers, you can print any of them.
-----Examples-----
Input
2 2 2 1
Output
YES
0 1 0 1 2 3 2
Input
1 2 3 4
Output
NO
Input
2 2 2 3
Output
NO
-----Note-----
In the first test, it is easy to see, that the sequence is beautiful because the difference between any two consecutive numbers is equal to $1$. Also, there are exactly two numbers, equal to $0$, $1$, $2$ and exactly one number, equal to $3$.
It can be proved, that it is impossible to construct beautiful sequences in the second and third tests. | numbers = list(map(int, input().split(" ")))
def solve(numbers, x):
n = sum(numbers)
a = [None for i in range(n)]
for i in range(n):
if not numbers[x]:
return
a[i] = x
numbers[x] -= 1
if not sum(numbers):
break
if x and numbers[x - 1]:
x -= 1
elif x < 3 and numbers[x + 1]:
x += 1
else:
return None
return a
for i in range(4):
sol = solve(list(numbers), i)
if sol:
print("YES")
print(" ".join(map(str, sol)))
exit(0)
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NONE VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN ASSIGN VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN NONE RETURN VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING |
An integer sequence is called beautiful if the difference between any two consecutive numbers is equal to $1$. More formally, a sequence $s_1, s_2, \ldots, s_{n}$ is beautiful if $|s_i - s_{i+1}| = 1$ for all $1 \leq i \leq n - 1$.
Trans has $a$ numbers $0$, $b$ numbers $1$, $c$ numbers $2$ and $d$ numbers $3$. He wants to construct a beautiful sequence using all of these $a + b + c + d$ numbers.
However, it turns out to be a non-trivial task, and Trans was not able to do it. Could you please help Trans?
-----Input-----
The only input line contains four non-negative integers $a$, $b$, $c$ and $d$ ($0 < a+b+c+d \leq 10^5$).
-----Output-----
If it is impossible to construct a beautiful sequence satisfying the above constraints, print "NO" (without quotes) in one line.
Otherwise, print "YES" (without quotes) in the first line. Then in the second line print $a + b + c + d$ integers, separated by spaces — a beautiful sequence. There should be $a$ numbers equal to $0$, $b$ numbers equal to $1$, $c$ numbers equal to $2$ and $d$ numbers equal to $3$.
If there are multiple answers, you can print any of them.
-----Examples-----
Input
2 2 2 1
Output
YES
0 1 0 1 2 3 2
Input
1 2 3 4
Output
NO
Input
2 2 2 3
Output
NO
-----Note-----
In the first test, it is easy to see, that the sequence is beautiful because the difference between any two consecutive numbers is equal to $1$. Also, there are exactly two numbers, equal to $0$, $1$, $2$ and exactly one number, equal to $3$.
It can be proved, that it is impossible to construct beautiful sequences in the second and third tests. | T = 1
MAX = 3 * 10**5 + 10
for t in range(1, T + 1):
a, b, c, d = map(int, input().split())
a_, b_, c_, d_ = a, b, c, d
if a == 0 and b == 0:
if abs(c - d) > 1:
print("NO")
continue
if c > d:
print("YES")
ans = "2 " * (c - d > 0) + "3 2 " * d
else:
print("YES")
ans = "3 " * (d - c > 0) + "2 3 " * c
elif d == 0 and c == 0:
if abs(a - b) > 1:
print("NO")
continue
if a > b:
print("YES")
ans = "0 " * (a - b > 0) + "1 0 " * b
else:
print("YES")
ans = "1 " * (b - a > 0) + "0 1 " * a
else:
if b < a or c < d:
print("NO")
continue
b -= a
c -= d
minval = min(b, c)
b -= minval
c -= minval
if b > 1 or c > 1:
print("NO")
continue
print("YES")
ans = "1 " * b + "0 1 " * a + "2 1 " * minval + "2 3 " * d + "2 " * c
print(ans)
x = list(map(int, ans.split()))
assert (
x.count(0) == a_ and x.count(1) == b_ and x.count(2) == c_ and x.count(3) == d_
)
for i in range(1, len(x)):
assert abs(x[i] - x[i - 1]) == 1 | ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR NUMBER BIN_OP STRING VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR NUMBER BIN_OP STRING VAR IF VAR NUMBER VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR NUMBER BIN_OP STRING VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR NUMBER BIN_OP STRING VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR BIN_OP STRING VAR BIN_OP STRING VAR BIN_OP STRING VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER |
An integer sequence is called beautiful if the difference between any two consecutive numbers is equal to $1$. More formally, a sequence $s_1, s_2, \ldots, s_{n}$ is beautiful if $|s_i - s_{i+1}| = 1$ for all $1 \leq i \leq n - 1$.
Trans has $a$ numbers $0$, $b$ numbers $1$, $c$ numbers $2$ and $d$ numbers $3$. He wants to construct a beautiful sequence using all of these $a + b + c + d$ numbers.
However, it turns out to be a non-trivial task, and Trans was not able to do it. Could you please help Trans?
-----Input-----
The only input line contains four non-negative integers $a$, $b$, $c$ and $d$ ($0 < a+b+c+d \leq 10^5$).
-----Output-----
If it is impossible to construct a beautiful sequence satisfying the above constraints, print "NO" (without quotes) in one line.
Otherwise, print "YES" (without quotes) in the first line. Then in the second line print $a + b + c + d$ integers, separated by spaces — a beautiful sequence. There should be $a$ numbers equal to $0$, $b$ numbers equal to $1$, $c$ numbers equal to $2$ and $d$ numbers equal to $3$.
If there are multiple answers, you can print any of them.
-----Examples-----
Input
2 2 2 1
Output
YES
0 1 0 1 2 3 2
Input
1 2 3 4
Output
NO
Input
2 2 2 3
Output
NO
-----Note-----
In the first test, it is easy to see, that the sequence is beautiful because the difference between any two consecutive numbers is equal to $1$. Also, there are exactly two numbers, equal to $0$, $1$, $2$ and exactly one number, equal to $3$.
It can be proved, that it is impossible to construct beautiful sequences in the second and third tests. | a = list(map(int, input().split(" ")))
for i in range(4):
res = []
b = []
if a[i] != 0:
for j in range(4):
b.append(a[j])
j = i
b[j] -= 1
res.append(j)
while 1:
chk = 1
if j == 0:
if b[j + 1] > 0:
b[j + 1] -= 1
res.append(j + 1)
j += 1
chk = 0
elif j == 1:
if b[j - 1] > 0:
b[j - 1] -= 1
res.append(j - 1)
j -= 1
chk = 0
elif b[j + 1] > 0:
b[j + 1] -= 1
res.append(j + 1)
j += 1
chk = 0
elif j == 2:
if b[j + 1] > 0:
b[j + 1] -= 1
res.append(j + 1)
j += 1
chk = 0
elif b[j - 1] > 0:
b[j - 1] -= 1
res.append(j - 1)
j -= 1
chk = 0
elif j == 3:
if b[j - 1] > 0:
b[j - 1] -= 1
res.append(j - 1)
j -= 1
chk = 0
if chk:
break
f = 1
for i in range(4):
if b[i] != 0:
f = 0
if f:
print("YES")
print(*res)
exit()
print("NO") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING |
An integer sequence is called beautiful if the difference between any two consecutive numbers is equal to $1$. More formally, a sequence $s_1, s_2, \ldots, s_{n}$ is beautiful if $|s_i - s_{i+1}| = 1$ for all $1 \leq i \leq n - 1$.
Trans has $a$ numbers $0$, $b$ numbers $1$, $c$ numbers $2$ and $d$ numbers $3$. He wants to construct a beautiful sequence using all of these $a + b + c + d$ numbers.
However, it turns out to be a non-trivial task, and Trans was not able to do it. Could you please help Trans?
-----Input-----
The only input line contains four non-negative integers $a$, $b$, $c$ and $d$ ($0 < a+b+c+d \leq 10^5$).
-----Output-----
If it is impossible to construct a beautiful sequence satisfying the above constraints, print "NO" (without quotes) in one line.
Otherwise, print "YES" (without quotes) in the first line. Then in the second line print $a + b + c + d$ integers, separated by spaces — a beautiful sequence. There should be $a$ numbers equal to $0$, $b$ numbers equal to $1$, $c$ numbers equal to $2$ and $d$ numbers equal to $3$.
If there are multiple answers, you can print any of them.
-----Examples-----
Input
2 2 2 1
Output
YES
0 1 0 1 2 3 2
Input
1 2 3 4
Output
NO
Input
2 2 2 3
Output
NO
-----Note-----
In the first test, it is easy to see, that the sequence is beautiful because the difference between any two consecutive numbers is equal to $1$. Also, there are exactly two numbers, equal to $0$, $1$, $2$ and exactly one number, equal to $3$.
It can be proved, that it is impossible to construct beautiful sequences in the second and third tests. | import sys
input = sys.stdin.readline
a, b, c, d = map(int, input().split())
n = a + b + c + d
res = [-1] * n
if a + c + 1 == b + d:
for i in range(0, 2 * b - 1, 2):
res[i] = 1
for i in range(2 * b, n, 2):
res[i] = 3
for i in range(1, n, 2):
if (res[i - 1], res[i + 1]) == (1, 1):
if a > 0:
res[i] = 0
a -= 1
elif c > 0:
res[i] = 2
c -= 1
else:
print("NO")
exit()
elif (res[i - 1], res[i + 1]) == (1, 3):
if c > 0:
res[i] = 2
c -= 1
else:
print("NO")
exit()
elif c > 0:
res[i] = 2
c -= 1
else:
print("NO")
exit()
print("YES")
print(*res)
elif a + c == b + d + 1:
for i in range(0, 2 * a - 1, 2):
res[i] = 0
for i in range(2 * a, n, 2):
res[i] = 2
for i in range(1, n, 2):
if (res[i - 1], res[i + 1]) == (0, 0):
if b > 0:
res[i] = 1
b -= 1
else:
print("NO")
exit()
elif (res[i - 1], res[i + 1]) == (0, 2):
if b > 0:
res[i] = 1
b -= 1
else:
print("NO")
exit()
elif d > 0:
res[i] = 3
d -= 1
elif b > 0:
res[i] = 1
b -= 1
else:
print("NO")
exit()
print("YES")
print(*res)
elif a + c == b + d:
for i in range(0, 2 * a - 1, 2):
res[i] = 0
for i in range(2 * a, n, 2):
res[i] = 2
for i in range(1, n - 1, 2):
if (res[i - 1], res[i + 1]) == (0, 0):
if b > 0:
res[i] = 1
b -= 1
else:
print("NO")
exit()
elif (res[i - 1], res[i + 1]) == (0, 2):
if b > 0:
res[i] = 1
b -= 1
else:
print("NO")
exit()
elif d > 0:
res[i] = 3
d -= 1
elif b > 0:
res[i] = 1
b -= 1
else:
print("NO")
exit()
if c > 0:
if d > 0:
res[-1] = 3
elif b > 0:
res[-1] = 1
else:
print("NO")
exit()
elif b > 0:
res[-1] = 1
else:
print("NO")
exit()
print("YES")
print(*res)
else:
print("NO") | IMPORT ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING |
An integer sequence is called beautiful if the difference between any two consecutive numbers is equal to $1$. More formally, a sequence $s_1, s_2, \ldots, s_{n}$ is beautiful if $|s_i - s_{i+1}| = 1$ for all $1 \leq i \leq n - 1$.
Trans has $a$ numbers $0$, $b$ numbers $1$, $c$ numbers $2$ and $d$ numbers $3$. He wants to construct a beautiful sequence using all of these $a + b + c + d$ numbers.
However, it turns out to be a non-trivial task, and Trans was not able to do it. Could you please help Trans?
-----Input-----
The only input line contains four non-negative integers $a$, $b$, $c$ and $d$ ($0 < a+b+c+d \leq 10^5$).
-----Output-----
If it is impossible to construct a beautiful sequence satisfying the above constraints, print "NO" (without quotes) in one line.
Otherwise, print "YES" (without quotes) in the first line. Then in the second line print $a + b + c + d$ integers, separated by spaces — a beautiful sequence. There should be $a$ numbers equal to $0$, $b$ numbers equal to $1$, $c$ numbers equal to $2$ and $d$ numbers equal to $3$.
If there are multiple answers, you can print any of them.
-----Examples-----
Input
2 2 2 1
Output
YES
0 1 0 1 2 3 2
Input
1 2 3 4
Output
NO
Input
2 2 2 3
Output
NO
-----Note-----
In the first test, it is easy to see, that the sequence is beautiful because the difference between any two consecutive numbers is equal to $1$. Also, there are exactly two numbers, equal to $0$, $1$, $2$ and exactly one number, equal to $3$.
It can be proved, that it is impossible to construct beautiful sequences in the second and third tests. | import sys
a, b, c, d = map(int, input().split())
a0, b0, c0, d0 = a, b, c, d
ab, bc, cd = [], [], []
while a and b:
a -= 1
b -= 1
ab.extend([0, 1])
if a:
if not c0 and not d0 and a == 1:
ab.append(0)
print("YES")
print(*ab)
else:
print("NO")
sys.exit()
while c and d:
c -= 1
d -= 1
cd.extend([2, 3])
if d:
if not a0 and not b0 and d == 1:
cd.insert(0, 3)
print("YES")
print(*cd)
else:
print("NO")
sys.exit()
while b and c:
b -= 1
c -= 1
bc.extend([2, 1])
if b > 1 or c > 1:
print("NO")
sys.exit()
if b == 1:
ab.insert(0, 1)
if c == 1:
cd.append(2)
ans = ab + bc + cd
print("YES")
print(*ans) | IMPORT ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST LIST LIST WHILE VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER IF VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR WHILE VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER IF VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR WHILE VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST NUMBER NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR |
An integer sequence is called beautiful if the difference between any two consecutive numbers is equal to $1$. More formally, a sequence $s_1, s_2, \ldots, s_{n}$ is beautiful if $|s_i - s_{i+1}| = 1$ for all $1 \leq i \leq n - 1$.
Trans has $a$ numbers $0$, $b$ numbers $1$, $c$ numbers $2$ and $d$ numbers $3$. He wants to construct a beautiful sequence using all of these $a + b + c + d$ numbers.
However, it turns out to be a non-trivial task, and Trans was not able to do it. Could you please help Trans?
-----Input-----
The only input line contains four non-negative integers $a$, $b$, $c$ and $d$ ($0 < a+b+c+d \leq 10^5$).
-----Output-----
If it is impossible to construct a beautiful sequence satisfying the above constraints, print "NO" (without quotes) in one line.
Otherwise, print "YES" (without quotes) in the first line. Then in the second line print $a + b + c + d$ integers, separated by spaces — a beautiful sequence. There should be $a$ numbers equal to $0$, $b$ numbers equal to $1$, $c$ numbers equal to $2$ and $d$ numbers equal to $3$.
If there are multiple answers, you can print any of them.
-----Examples-----
Input
2 2 2 1
Output
YES
0 1 0 1 2 3 2
Input
1 2 3 4
Output
NO
Input
2 2 2 3
Output
NO
-----Note-----
In the first test, it is easy to see, that the sequence is beautiful because the difference between any two consecutive numbers is equal to $1$. Also, there are exactly two numbers, equal to $0$, $1$, $2$ and exactly one number, equal to $3$.
It can be proved, that it is impossible to construct beautiful sequences in the second and third tests. | import sys
from itertools import combinations
input = sys.stdin.readline
INF = 10**9
a, b, c, d = [int(item) for item in input().split()]
n = a + b + c + d
if n == 1 and a == 1:
print("YES")
print(0)
exit()
elif n == 1 and d == 1:
print("YES")
print(3)
exit()
diff = a + c - (b + d)
if abs(diff) > 1:
print("NO")
exit()
if c == 0 and d == 0:
if a > b:
ans = [0] * n
for i in range(b):
ans[2 * i + 1] = 1
else:
ans = [1] * n
for i in range(a):
ans[2 * i + 1] = 0
print("YES")
print(" ".join([str(item) for item in ans]))
exit()
if a == 0 and b == 0:
if c > d:
ans = [2] * n
for i in range(d):
ans[2 * i + 1] = 3
else:
ans = [3] * n
for i in range(c):
ans[2 * i + 1] = 2
print("YES")
print(" ".join([str(item) for item in ans]))
exit()
if diff == 1:
if d > c - 1:
print("NO")
exit()
ans = [2] * n
for i in range(d):
ans[i * 2 + 1] = 3
for i in range(d, d + b):
ans[i * 2 + 1] = 1
for i in range(c, c + a):
ans[i * 2] = 0
print("YES")
print(" ".join([str(item) for item in ans]))
exit()
elif diff == -1:
if a > b - 1:
print("NO")
exit()
ans = [1] * n
for i in range(a):
ans[i * 2 + 1] = 0
for i in range(a, a + c):
ans[i * 2 + 1] = 2
for i in range(b, b + d):
ans[i * 2] = 3
print("YES")
print(" ".join([str(item) for item in ans]))
exit()
elif diff == 0:
if a > b or d > c:
print("NO")
exit()
ans = [0] * n
for i in range(a, a + c):
ans[i * 2] = 2
for i in range(b):
ans[i * 2 + 1] = 1
for i in range(b, b + d):
ans[i * 2 + 1] = 3
print("YES")
print(" ".join([str(item) for item in ans]))
exit() | IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR IF VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR |
An integer sequence is called beautiful if the difference between any two consecutive numbers is equal to $1$. More formally, a sequence $s_1, s_2, \ldots, s_{n}$ is beautiful if $|s_i - s_{i+1}| = 1$ for all $1 \leq i \leq n - 1$.
Trans has $a$ numbers $0$, $b$ numbers $1$, $c$ numbers $2$ and $d$ numbers $3$. He wants to construct a beautiful sequence using all of these $a + b + c + d$ numbers.
However, it turns out to be a non-trivial task, and Trans was not able to do it. Could you please help Trans?
-----Input-----
The only input line contains four non-negative integers $a$, $b$, $c$ and $d$ ($0 < a+b+c+d \leq 10^5$).
-----Output-----
If it is impossible to construct a beautiful sequence satisfying the above constraints, print "NO" (without quotes) in one line.
Otherwise, print "YES" (without quotes) in the first line. Then in the second line print $a + b + c + d$ integers, separated by spaces — a beautiful sequence. There should be $a$ numbers equal to $0$, $b$ numbers equal to $1$, $c$ numbers equal to $2$ and $d$ numbers equal to $3$.
If there are multiple answers, you can print any of them.
-----Examples-----
Input
2 2 2 1
Output
YES
0 1 0 1 2 3 2
Input
1 2 3 4
Output
NO
Input
2 2 2 3
Output
NO
-----Note-----
In the first test, it is easy to see, that the sequence is beautiful because the difference between any two consecutive numbers is equal to $1$. Also, there are exactly two numbers, equal to $0$, $1$, $2$ and exactly one number, equal to $3$.
It can be proved, that it is impossible to construct beautiful sequences in the second and third tests. | a, b, c, d = map(int, input().split())
zeros, ones, twos, threes = a, b, c, d
s = a + b + c + d
r = [-1] * s
for i in range(0, s, 2):
if a > 0:
r[i] = 0
a -= 1
elif c > 0:
r[i] = 2
c -= 1
else:
break
for i in range(s - 1 - s % 2, -1, -2):
if d > 0:
r[i] = 3
d -= 1
elif b > 0:
r[i] = 1
b -= 1
else:
break
result = list(filter(lambda x: x >= 0, r))
penalty = 0
for _ in range(min(b, 2)):
result = [1] + result
penalty += 1
b -= penalty
penalty = 0
for _ in range(min(d, 2)):
result = [3] + result
penalty += 1
d -= penalty
if len(result) < s or b > 0 or d > 0:
print("NO")
else:
f = False
for i in range(1, s):
if abs(result[i] - result[i - 1]) != 1:
f = True
break
if not f:
print("YES")
print(" ".join(map(str, result)))
else:
print("NO") | ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING |
An integer sequence is called beautiful if the difference between any two consecutive numbers is equal to $1$. More formally, a sequence $s_1, s_2, \ldots, s_{n}$ is beautiful if $|s_i - s_{i+1}| = 1$ for all $1 \leq i \leq n - 1$.
Trans has $a$ numbers $0$, $b$ numbers $1$, $c$ numbers $2$ and $d$ numbers $3$. He wants to construct a beautiful sequence using all of these $a + b + c + d$ numbers.
However, it turns out to be a non-trivial task, and Trans was not able to do it. Could you please help Trans?
-----Input-----
The only input line contains four non-negative integers $a$, $b$, $c$ and $d$ ($0 < a+b+c+d \leq 10^5$).
-----Output-----
If it is impossible to construct a beautiful sequence satisfying the above constraints, print "NO" (without quotes) in one line.
Otherwise, print "YES" (without quotes) in the first line. Then in the second line print $a + b + c + d$ integers, separated by spaces — a beautiful sequence. There should be $a$ numbers equal to $0$, $b$ numbers equal to $1$, $c$ numbers equal to $2$ and $d$ numbers equal to $3$.
If there are multiple answers, you can print any of them.
-----Examples-----
Input
2 2 2 1
Output
YES
0 1 0 1 2 3 2
Input
1 2 3 4
Output
NO
Input
2 2 2 3
Output
NO
-----Note-----
In the first test, it is easy to see, that the sequence is beautiful because the difference between any two consecutive numbers is equal to $1$. Also, there are exactly two numbers, equal to $0$, $1$, $2$ and exactly one number, equal to $3$.
It can be proved, that it is impossible to construct beautiful sequences in the second and third tests. | def solve1(arr, start):
wynik = []
id = start
while True:
if arr[id] == 0:
if sum(arr) == 0:
break
return "NO\n"
arr[id] -= 1
wynik.append(str(id))
if id == 0:
id = 1
elif id == 1:
id = 0 if arr[0] > 0 else 2
elif id == 2:
id = 1 if arr[1] > 0 else 3
elif id == 3:
id = 2
return "YES\n" + " ".join(wynik)
def solve():
arr = [int(a) for a in input().split(" ")]
res = ""
for i in range(4):
res = solve1(arr.copy(), i)
if not res.startswith("NO"):
break
return res
print(solve()) | FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR WHILE NUMBER IF VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN STRING VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER RETURN BIN_OP STRING FUNC_CALL STRING VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR STRING RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
An integer sequence is called beautiful if the difference between any two consecutive numbers is equal to $1$. More formally, a sequence $s_1, s_2, \ldots, s_{n}$ is beautiful if $|s_i - s_{i+1}| = 1$ for all $1 \leq i \leq n - 1$.
Trans has $a$ numbers $0$, $b$ numbers $1$, $c$ numbers $2$ and $d$ numbers $3$. He wants to construct a beautiful sequence using all of these $a + b + c + d$ numbers.
However, it turns out to be a non-trivial task, and Trans was not able to do it. Could you please help Trans?
-----Input-----
The only input line contains four non-negative integers $a$, $b$, $c$ and $d$ ($0 < a+b+c+d \leq 10^5$).
-----Output-----
If it is impossible to construct a beautiful sequence satisfying the above constraints, print "NO" (without quotes) in one line.
Otherwise, print "YES" (without quotes) in the first line. Then in the second line print $a + b + c + d$ integers, separated by spaces — a beautiful sequence. There should be $a$ numbers equal to $0$, $b$ numbers equal to $1$, $c$ numbers equal to $2$ and $d$ numbers equal to $3$.
If there are multiple answers, you can print any of them.
-----Examples-----
Input
2 2 2 1
Output
YES
0 1 0 1 2 3 2
Input
1 2 3 4
Output
NO
Input
2 2 2 3
Output
NO
-----Note-----
In the first test, it is easy to see, that the sequence is beautiful because the difference between any two consecutive numbers is equal to $1$. Also, there are exactly two numbers, equal to $0$, $1$, $2$ and exactly one number, equal to $3$.
It can be proved, that it is impossible to construct beautiful sequences in the second and third tests. | from sys import stdin
def iinput():
return int(stdin.readline())
def sinput():
return input()
def minput():
return map(int, stdin.readline().split())
def linput():
return list(map(int, stdin.readline().split()))
a = linput()
ref = [0, 1, 2, 3]
x = sum(a)
for i in range(4):
b = a[:]
seq = []
for j in range(x):
b[i] -= 1
seq.append(ref[i])
if i == 0:
i = 1
elif i == 3:
i = 2
elif i == 1:
if b[0] > 0:
i = 0
else:
i = 2
elif b[3] > 0:
i = 3
else:
i = 1
if max(b) == 0 and min(b) == 0:
print("YES")
print(*seq)
exit()
print("NO") | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN 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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.