description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
Kate has a set $S$ of $n$ integers $\{1, \dots, n\} $.
She thinks that imperfection of a subset $M \subseteq S$ is equal to the maximum of $gcd(a, b)$ over all pairs $(a, b)$ such that both $a$ and $b$ are in $M$ and $a \neq b$.
Kate is a very neat girl and for each $k \in \{2, \dots, n\}$ she wants to find a subset that has the smallest imperfection among all subsets in $S$ of size $k$. There can be more than one subset with the smallest imperfection and the same size, but you don't need to worry about it. Kate wants to find all the subsets herself, but she needs your help to find the smallest possible imperfection for each size $k$, will name it $I_k$.
Please, help Kate to find $I_2$, $I_3$, ..., $I_n$.
-----Input-----
The first and only line in the input consists of only one integer $n$ ($2\le n \le 5 \cdot 10^5$) Β β the size of the given set $S$.
-----Output-----
Output contains only one line that includes $n - 1$ integers: $I_2$, $I_3$, ..., $I_n$.
-----Examples-----
Input
2
Output
1
Input
3
Output
1 1
-----Note-----
First sample: answer is 1, because $gcd(1, 2) = 1$.
Second sample: there are subsets of $S$ with sizes $2, 3$ with imperfection equal to 1. For example, $\{2,3\}$ and $\{1, 2, 3\}$.
|
n = int(input())
x = [0, 1] + [0] * (n - 1)
for i in range(2, n + 1):
if x[i]:
continue
for j in range(i, n + 1, i):
if x[j]:
continue
x[j] = j // i
print(*sorted(x[2:]))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
|
Kate has a set $S$ of $n$ integers $\{1, \dots, n\} $.
She thinks that imperfection of a subset $M \subseteq S$ is equal to the maximum of $gcd(a, b)$ over all pairs $(a, b)$ such that both $a$ and $b$ are in $M$ and $a \neq b$.
Kate is a very neat girl and for each $k \in \{2, \dots, n\}$ she wants to find a subset that has the smallest imperfection among all subsets in $S$ of size $k$. There can be more than one subset with the smallest imperfection and the same size, but you don't need to worry about it. Kate wants to find all the subsets herself, but she needs your help to find the smallest possible imperfection for each size $k$, will name it $I_k$.
Please, help Kate to find $I_2$, $I_3$, ..., $I_n$.
-----Input-----
The first and only line in the input consists of only one integer $n$ ($2\le n \le 5 \cdot 10^5$) Β β the size of the given set $S$.
-----Output-----
Output contains only one line that includes $n - 1$ integers: $I_2$, $I_3$, ..., $I_n$.
-----Examples-----
Input
2
Output
1
Input
3
Output
1 1
-----Note-----
First sample: answer is 1, because $gcd(1, 2) = 1$.
Second sample: there are subsets of $S$ with sizes $2, 3$ with imperfection equal to 1. For example, $\{2,3\}$ and $\{1, 2, 3\}$.
|
n = int(input())
div = [0] * (n + 5)
def sieve():
for i in range(2, n + 1):
if div[i] != 0:
continue
for j in range(i, n + 1, i):
if div[j] == 0:
div[j] = i
return
a = []
sieve()
b = []
for i in range(2, n + 1):
if div[i] == i:
b.append(i)
a.append(1)
for i in range(2, n + 1):
if i > n // 2:
break
for k in b:
if i * k > n:
break
if div[i] == i:
if k > i:
break
a.append(i)
else:
y = div[i * k]
if i * k / y > i:
break
a.append(i)
print(*a)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN ASSIGN VAR LIST EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR IF VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Kate has a set $S$ of $n$ integers $\{1, \dots, n\} $.
She thinks that imperfection of a subset $M \subseteq S$ is equal to the maximum of $gcd(a, b)$ over all pairs $(a, b)$ such that both $a$ and $b$ are in $M$ and $a \neq b$.
Kate is a very neat girl and for each $k \in \{2, \dots, n\}$ she wants to find a subset that has the smallest imperfection among all subsets in $S$ of size $k$. There can be more than one subset with the smallest imperfection and the same size, but you don't need to worry about it. Kate wants to find all the subsets herself, but she needs your help to find the smallest possible imperfection for each size $k$, will name it $I_k$.
Please, help Kate to find $I_2$, $I_3$, ..., $I_n$.
-----Input-----
The first and only line in the input consists of only one integer $n$ ($2\le n \le 5 \cdot 10^5$) Β β the size of the given set $S$.
-----Output-----
Output contains only one line that includes $n - 1$ integers: $I_2$, $I_3$, ..., $I_n$.
-----Examples-----
Input
2
Output
1
Input
3
Output
1 1
-----Note-----
First sample: answer is 1, because $gcd(1, 2) = 1$.
Second sample: there are subsets of $S$ with sizes $2, 3$ with imperfection equal to 1. For example, $\{2,3\}$ and $\{1, 2, 3\}$.
|
n = int(input())
i = 2
s = [1] * (n + 1)
s[0], s[1] = 0, 0
m = n / 2 + 1
while i <= m:
j = 2
while j * i <= n:
s[i * j] = i
j += 1
i += 1
s.sort()
for i in s[2:]:
print(i, end=" ")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
|
Kate has a set $S$ of $n$ integers $\{1, \dots, n\} $.
She thinks that imperfection of a subset $M \subseteq S$ is equal to the maximum of $gcd(a, b)$ over all pairs $(a, b)$ such that both $a$ and $b$ are in $M$ and $a \neq b$.
Kate is a very neat girl and for each $k \in \{2, \dots, n\}$ she wants to find a subset that has the smallest imperfection among all subsets in $S$ of size $k$. There can be more than one subset with the smallest imperfection and the same size, but you don't need to worry about it. Kate wants to find all the subsets herself, but she needs your help to find the smallest possible imperfection for each size $k$, will name it $I_k$.
Please, help Kate to find $I_2$, $I_3$, ..., $I_n$.
-----Input-----
The first and only line in the input consists of only one integer $n$ ($2\le n \le 5 \cdot 10^5$) Β β the size of the given set $S$.
-----Output-----
Output contains only one line that includes $n - 1$ integers: $I_2$, $I_3$, ..., $I_n$.
-----Examples-----
Input
2
Output
1
Input
3
Output
1 1
-----Note-----
First sample: answer is 1, because $gcd(1, 2) = 1$.
Second sample: there are subsets of $S$ with sizes $2, 3$ with imperfection equal to 1. For example, $\{2,3\}$ and $\{1, 2, 3\}$.
|
n = int(input())
sieve = [-1] * (n + 1)
primes = []
for i in range(2, n + 1):
if sieve[i] == -1:
sieve[i] = i
primes.append(i)
for x in range(2 * i, n + 1, i):
if sieve[x] == -1:
sieve[x] = i
answer = [1] * len(primes)
c = set(primes)
c.add(1)
toAdd = 2
while len(c) != n:
for k in primes:
if k > sieve[toAdd]:
break
y = toAdd * k
if y > n:
break
if y not in c:
c.add(y)
answer.append(toAdd)
toAdd += 1
print(*answer)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR FOR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Kate has a set $S$ of $n$ integers $\{1, \dots, n\} $.
She thinks that imperfection of a subset $M \subseteq S$ is equal to the maximum of $gcd(a, b)$ over all pairs $(a, b)$ such that both $a$ and $b$ are in $M$ and $a \neq b$.
Kate is a very neat girl and for each $k \in \{2, \dots, n\}$ she wants to find a subset that has the smallest imperfection among all subsets in $S$ of size $k$. There can be more than one subset with the smallest imperfection and the same size, but you don't need to worry about it. Kate wants to find all the subsets herself, but she needs your help to find the smallest possible imperfection for each size $k$, will name it $I_k$.
Please, help Kate to find $I_2$, $I_3$, ..., $I_n$.
-----Input-----
The first and only line in the input consists of only one integer $n$ ($2\le n \le 5 \cdot 10^5$) Β β the size of the given set $S$.
-----Output-----
Output contains only one line that includes $n - 1$ integers: $I_2$, $I_3$, ..., $I_n$.
-----Examples-----
Input
2
Output
1
Input
3
Output
1 1
-----Note-----
First sample: answer is 1, because $gcd(1, 2) = 1$.
Second sample: there are subsets of $S$ with sizes $2, 3$ with imperfection equal to 1. For example, $\{2,3\}$ and $\{1, 2, 3\}$.
|
def get_primes(n):
res = [2]
arr = [True] * ((n - 1) // 2)
i = 0
for i in range(len(arr)):
if arr[i]:
a = i * 2 + 3
res.append(a)
for ii in range(i + a, len(arr), a):
arr[ii] = False
return res
n = int(input())
primes = get_primes(n)
res = ["1"] * min(n - 1, len(primes))
left = n - 1 - len(res)
ii = 2
while left > 0:
for a in primes:
if ii * a <= n:
res.append(str(ii))
left -= 1
else:
break
if ii % a == 0 or left == 0:
break
ii += 1
print(" ".join(res))
|
FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
|
Kate has a set $S$ of $n$ integers $\{1, \dots, n\} $.
She thinks that imperfection of a subset $M \subseteq S$ is equal to the maximum of $gcd(a, b)$ over all pairs $(a, b)$ such that both $a$ and $b$ are in $M$ and $a \neq b$.
Kate is a very neat girl and for each $k \in \{2, \dots, n\}$ she wants to find a subset that has the smallest imperfection among all subsets in $S$ of size $k$. There can be more than one subset with the smallest imperfection and the same size, but you don't need to worry about it. Kate wants to find all the subsets herself, but she needs your help to find the smallest possible imperfection for each size $k$, will name it $I_k$.
Please, help Kate to find $I_2$, $I_3$, ..., $I_n$.
-----Input-----
The first and only line in the input consists of only one integer $n$ ($2\le n \le 5 \cdot 10^5$) Β β the size of the given set $S$.
-----Output-----
Output contains only one line that includes $n - 1$ integers: $I_2$, $I_3$, ..., $I_n$.
-----Examples-----
Input
2
Output
1
Input
3
Output
1 1
-----Note-----
First sample: answer is 1, because $gcd(1, 2) = 1$.
Second sample: there are subsets of $S$ with sizes $2, 3$ with imperfection equal to 1. For example, $\{2,3\}$ and $\{1, 2, 3\}$.
|
n = int(input())
cur = n
ans = [0] * (n + 1)
used = [0] * (n + 1)
for j in range(n // 2, 1, -1):
for x in range(2 * j, n + 1, j):
if used[x] == 0:
used[x] = 1
ans[cur] = j
cur -= 1
for x in range(1, cur + 1):
ans[x] = 1
print(" ".join([str(x) for x in ans[2:]]))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER
|
Kate has a set $S$ of $n$ integers $\{1, \dots, n\} $.
She thinks that imperfection of a subset $M \subseteq S$ is equal to the maximum of $gcd(a, b)$ over all pairs $(a, b)$ such that both $a$ and $b$ are in $M$ and $a \neq b$.
Kate is a very neat girl and for each $k \in \{2, \dots, n\}$ she wants to find a subset that has the smallest imperfection among all subsets in $S$ of size $k$. There can be more than one subset with the smallest imperfection and the same size, but you don't need to worry about it. Kate wants to find all the subsets herself, but she needs your help to find the smallest possible imperfection for each size $k$, will name it $I_k$.
Please, help Kate to find $I_2$, $I_3$, ..., $I_n$.
-----Input-----
The first and only line in the input consists of only one integer $n$ ($2\le n \le 5 \cdot 10^5$) Β β the size of the given set $S$.
-----Output-----
Output contains only one line that includes $n - 1$ integers: $I_2$, $I_3$, ..., $I_n$.
-----Examples-----
Input
2
Output
1
Input
3
Output
1 1
-----Note-----
First sample: answer is 1, because $gcd(1, 2) = 1$.
Second sample: there are subsets of $S$ with sizes $2, 3$ with imperfection equal to 1. For example, $\{2,3\}$ and $\{1, 2, 3\}$.
|
n = int(input())
arr = [0] * (n + 1)
for i in range(1, n + 1):
for j in range(i + i, n + 1, i):
arr[j] = i
arr.sort()
for i in range(2, n + 1):
print(arr[i], end=" ")
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING
|
Kate has a set $S$ of $n$ integers $\{1, \dots, n\} $.
She thinks that imperfection of a subset $M \subseteq S$ is equal to the maximum of $gcd(a, b)$ over all pairs $(a, b)$ such that both $a$ and $b$ are in $M$ and $a \neq b$.
Kate is a very neat girl and for each $k \in \{2, \dots, n\}$ she wants to find a subset that has the smallest imperfection among all subsets in $S$ of size $k$. There can be more than one subset with the smallest imperfection and the same size, but you don't need to worry about it. Kate wants to find all the subsets herself, but she needs your help to find the smallest possible imperfection for each size $k$, will name it $I_k$.
Please, help Kate to find $I_2$, $I_3$, ..., $I_n$.
-----Input-----
The first and only line in the input consists of only one integer $n$ ($2\le n \le 5 \cdot 10^5$) Β β the size of the given set $S$.
-----Output-----
Output contains only one line that includes $n - 1$ integers: $I_2$, $I_3$, ..., $I_n$.
-----Examples-----
Input
2
Output
1
Input
3
Output
1 1
-----Note-----
First sample: answer is 1, because $gcd(1, 2) = 1$.
Second sample: there are subsets of $S$ with sizes $2, 3$ with imperfection equal to 1. For example, $\{2,3\}$ and $\{1, 2, 3\}$.
|
def solve(n):
nums = [1] * (n + 1)
p = 2
while p * p <= n:
if nums[p] == 1:
for i in range(p * 2, n + 1, p):
nums[i] = max(nums[i], p, i // p)
p += 1
return sorted(sorted(nums[2:]))
n = int(input())
print(" ".join([str(x) for x in solve(n)]))
|
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR
|
Kate has a set $S$ of $n$ integers $\{1, \dots, n\} $.
She thinks that imperfection of a subset $M \subseteq S$ is equal to the maximum of $gcd(a, b)$ over all pairs $(a, b)$ such that both $a$ and $b$ are in $M$ and $a \neq b$.
Kate is a very neat girl and for each $k \in \{2, \dots, n\}$ she wants to find a subset that has the smallest imperfection among all subsets in $S$ of size $k$. There can be more than one subset with the smallest imperfection and the same size, but you don't need to worry about it. Kate wants to find all the subsets herself, but she needs your help to find the smallest possible imperfection for each size $k$, will name it $I_k$.
Please, help Kate to find $I_2$, $I_3$, ..., $I_n$.
-----Input-----
The first and only line in the input consists of only one integer $n$ ($2\le n \le 5 \cdot 10^5$) Β β the size of the given set $S$.
-----Output-----
Output contains only one line that includes $n - 1$ integers: $I_2$, $I_3$, ..., $I_n$.
-----Examples-----
Input
2
Output
1
Input
3
Output
1 1
-----Note-----
First sample: answer is 1, because $gcd(1, 2) = 1$.
Second sample: there are subsets of $S$ with sizes $2, 3$ with imperfection equal to 1. For example, $\{2,3\}$ and $\{1, 2, 3\}$.
|
n = int(input())
A = [(1) for i in range(n + 1)]
for i in range(2, n):
j = 2 * i
while j <= n:
A[j] = i
j += i
ans = sorted(A)
ans = ans[2:]
print(*ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP NUMBER VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Kate has a set $S$ of $n$ integers $\{1, \dots, n\} $.
She thinks that imperfection of a subset $M \subseteq S$ is equal to the maximum of $gcd(a, b)$ over all pairs $(a, b)$ such that both $a$ and $b$ are in $M$ and $a \neq b$.
Kate is a very neat girl and for each $k \in \{2, \dots, n\}$ she wants to find a subset that has the smallest imperfection among all subsets in $S$ of size $k$. There can be more than one subset with the smallest imperfection and the same size, but you don't need to worry about it. Kate wants to find all the subsets herself, but she needs your help to find the smallest possible imperfection for each size $k$, will name it $I_k$.
Please, help Kate to find $I_2$, $I_3$, ..., $I_n$.
-----Input-----
The first and only line in the input consists of only one integer $n$ ($2\le n \le 5 \cdot 10^5$) Β β the size of the given set $S$.
-----Output-----
Output contains only one line that includes $n - 1$ integers: $I_2$, $I_3$, ..., $I_n$.
-----Examples-----
Input
2
Output
1
Input
3
Output
1 1
-----Note-----
First sample: answer is 1, because $gcd(1, 2) = 1$.
Second sample: there are subsets of $S$ with sizes $2, 3$ with imperfection equal to 1. For example, $\{2,3\}$ and $\{1, 2, 3\}$.
|
n = int(input())
res = [1] * (n + 1)
for i in range(2, n + 1):
j = i
while i * j <= n:
res[i * j] = max(res[i * j], j)
j += 1
res.sort()
print(" ".join(map(str, res[2:])))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR NUMBER
|
Nura wants to buy k gadgets. She has only s burles for that. She can buy each gadget for dollars or for pounds. So each gadget is selling only for some type of currency. The type of currency and the cost in that currency are not changing.
Nura can buy gadgets for n days. For each day you know the exchange rates of dollar and pound, so you know the cost of conversion burles to dollars or to pounds.
Each day (from 1 to n) Nura can buy some gadgets by current exchange rate. Each day she can buy any gadgets she wants, but each gadget can be bought no more than once during n days.
Help Nura to find the minimum day index when she will have k gadgets. Nura always pays with burles, which are converted according to the exchange rate of the purchase day. Nura can't buy dollars or pounds, she always stores only burles. Gadgets are numbered with integers from 1 to m in order of their appearing in input.
-----Input-----
First line contains four integers n, m, k, s (1 β€ n β€ 2Β·10^5, 1 β€ k β€ m β€ 2Β·10^5, 1 β€ s β€ 10^9) β number of days, total number and required number of gadgets, number of burles Nura has.
Second line contains n integers a_{i} (1 β€ a_{i} β€ 10^6) β the cost of one dollar in burles on i-th day.
Third line contains n integers b_{i} (1 β€ b_{i} β€ 10^6) β the cost of one pound in burles on i-th day.
Each of the next m lines contains two integers t_{i}, c_{i} (1 β€ t_{i} β€ 2, 1 β€ c_{i} β€ 10^6) β type of the gadget and it's cost. For the gadgets of the first type cost is specified in dollars. For the gadgets of the second type cost is specified in pounds.
-----Output-----
If Nura can't buy k gadgets print the only line with the number -1.
Otherwise the first line should contain integer d β the minimum day index, when Nura will have k gadgets. On each of the next k lines print two integers q_{i}, d_{i} β the number of gadget and the day gadget should be bought. All values q_{i} should be different, but the values d_{i} can coincide (so Nura can buy several gadgets at one day). The days are numbered from 1 to n.
In case there are multiple possible solutions, print any of them.
-----Examples-----
Input
5 4 2 2
1 2 3 2 1
3 2 1 2 3
1 1
2 1
1 2
2 2
Output
3
1 1
2 3
Input
4 3 2 200
69 70 71 72
104 105 106 107
1 1
2 2
1 2
Output
-1
Input
4 3 1 1000000000
900000 910000 940000 990000
990000 999000 999900 999990
1 87654
2 76543
1 65432
Output
-1
|
import sys
from itertools import accumulate
def solve(f, g):
n, m, k, s = [int(x) for x in f.readline().split()]
a_price = [(int(x), i + 1) for i, x in enumerate(f.readline().split())]
b_price = [(int(x), i + 1) for i, x in enumerate(f.readline().split())]
a_gadgets = []
b_gadgets = []
for i, line in enumerate(f):
t, price = [int(x) for x in line.split()]
if t == 1:
a_gadgets.append((price, i + 1))
else:
b_gadgets.append((price, i + 1))
a_gadgets.sort()
b_gadgets.sort()
prefix_a = [0] + list(accumulate(gadget[0] for gadget in a_gadgets))
prefix_b = [0] + list(accumulate(gadget[0] for gadget in b_gadgets))
la = min(k, len(a_gadgets))
lb = min(k, len(b_gadgets))
min_price_for_k = [(prefix_a[i], prefix_b[k - i], i) for i in range(k - lb, la + 1)]
for i in range(1, n):
a_price[i] = min(a_price[i], a_price[i - 1])
b_price[i] = min(b_price[i], b_price[i - 1])
def expence(day):
return lambda x: a_price[day][0] * x[0] + b_price[day][0] * x[1]
x, y = 0, n - 1
while x <= y - 1:
day = (x + y) // 2
min_cost = min(min_price_for_k, key=expence(day))
if expence(day)(min_cost) > s:
x = day + 1
else:
y = day
min_cost = min(min_price_for_k, key=expence(x))
if expence(x)(min_cost) > s:
g.write("-1\n")
else:
g.write(str(x + 1) + "\n")
i1 = min_cost[-1]
A, B = " " + str(a_price[x][1]) + "\n", " " + str(b_price[x][1]) + "\n"
for i in range(i1):
g.write(str(a_gadgets[i][1]) + A)
for i in range(k - i1):
g.write(str(b_gadgets[i][1]) + B)
solve(sys.stdin, sys.stdout)
|
IMPORT FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR VAR NUMBER STRING BIN_OP BIN_OP STRING FUNC_CALL VAR VAR VAR NUMBER STRING FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR
|
Nura wants to buy k gadgets. She has only s burles for that. She can buy each gadget for dollars or for pounds. So each gadget is selling only for some type of currency. The type of currency and the cost in that currency are not changing.
Nura can buy gadgets for n days. For each day you know the exchange rates of dollar and pound, so you know the cost of conversion burles to dollars or to pounds.
Each day (from 1 to n) Nura can buy some gadgets by current exchange rate. Each day she can buy any gadgets she wants, but each gadget can be bought no more than once during n days.
Help Nura to find the minimum day index when she will have k gadgets. Nura always pays with burles, which are converted according to the exchange rate of the purchase day. Nura can't buy dollars or pounds, she always stores only burles. Gadgets are numbered with integers from 1 to m in order of their appearing in input.
-----Input-----
First line contains four integers n, m, k, s (1 β€ n β€ 2Β·10^5, 1 β€ k β€ m β€ 2Β·10^5, 1 β€ s β€ 10^9) β number of days, total number and required number of gadgets, number of burles Nura has.
Second line contains n integers a_{i} (1 β€ a_{i} β€ 10^6) β the cost of one dollar in burles on i-th day.
Third line contains n integers b_{i} (1 β€ b_{i} β€ 10^6) β the cost of one pound in burles on i-th day.
Each of the next m lines contains two integers t_{i}, c_{i} (1 β€ t_{i} β€ 2, 1 β€ c_{i} β€ 10^6) β type of the gadget and it's cost. For the gadgets of the first type cost is specified in dollars. For the gadgets of the second type cost is specified in pounds.
-----Output-----
If Nura can't buy k gadgets print the only line with the number -1.
Otherwise the first line should contain integer d β the minimum day index, when Nura will have k gadgets. On each of the next k lines print two integers q_{i}, d_{i} β the number of gadget and the day gadget should be bought. All values q_{i} should be different, but the values d_{i} can coincide (so Nura can buy several gadgets at one day). The days are numbered from 1 to n.
In case there are multiple possible solutions, print any of them.
-----Examples-----
Input
5 4 2 2
1 2 3 2 1
3 2 1 2 3
1 1
2 1
1 2
2 2
Output
3
1 1
2 3
Input
4 3 2 200
69 70 71 72
104 105 106 107
1 1
2 2
1 2
Output
-1
Input
4 3 1 1000000000
900000 910000 940000 990000
990000 999000 999900 999990
1 87654
2 76543
1 65432
Output
-1
|
import sys
from itertools import accumulate
n, m, k, s = map(int, input().split())
_rate = [list(map(int, input().split())), list(map(int, input().split()))]
rate = [list(accumulate(_rate[0], min)), list(accumulate(_rate[1], min))]
items = [[[0, -1]], [[0, -1]]]
for i in range(m):
t, c = map(int, sys.stdin.readline().split())
items[t - 1].append([c, i + 1])
items[0].sort()
items[1].sort()
for i in range(2):
for j in range(1, len(items[i])):
items[i][j][0] += items[i][j - 1][0]
ok, ng = n, -1
ans_size = [0, 0]
while abs(ok - ng) > 1:
mid = ok + ng >> 1
cnt_1 = min(len(items[0]) - 1, k)
cnt_2 = k - cnt_1
while cnt_1 >= 0 and cnt_2 < len(items[1]):
dollers = items[0][cnt_1][0]
pounds = items[1][cnt_2][0]
if dollers * rate[0][mid] + pounds * rate[1][mid] <= s:
ok = mid
ans_size = [cnt_1, cnt_2]
break
cnt_1 -= 1
cnt_2 += 1
else:
ng = mid
if ok < n:
print(ok + 1)
for i in range(2):
day = 0
for j in range(0, n):
if _rate[i][j] == rate[i][ok]:
day = j + 1
break
for j in range(1, ans_size[i] + 1):
print(items[i][j][1], day)
else:
print(-1)
|
IMPORT ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST LIST LIST NUMBER NUMBER LIST LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER LIST VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER WHILE FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER
|
Nura wants to buy k gadgets. She has only s burles for that. She can buy each gadget for dollars or for pounds. So each gadget is selling only for some type of currency. The type of currency and the cost in that currency are not changing.
Nura can buy gadgets for n days. For each day you know the exchange rates of dollar and pound, so you know the cost of conversion burles to dollars or to pounds.
Each day (from 1 to n) Nura can buy some gadgets by current exchange rate. Each day she can buy any gadgets she wants, but each gadget can be bought no more than once during n days.
Help Nura to find the minimum day index when she will have k gadgets. Nura always pays with burles, which are converted according to the exchange rate of the purchase day. Nura can't buy dollars or pounds, she always stores only burles. Gadgets are numbered with integers from 1 to m in order of their appearing in input.
-----Input-----
First line contains four integers n, m, k, s (1 β€ n β€ 2Β·10^5, 1 β€ k β€ m β€ 2Β·10^5, 1 β€ s β€ 10^9) β number of days, total number and required number of gadgets, number of burles Nura has.
Second line contains n integers a_{i} (1 β€ a_{i} β€ 10^6) β the cost of one dollar in burles on i-th day.
Third line contains n integers b_{i} (1 β€ b_{i} β€ 10^6) β the cost of one pound in burles on i-th day.
Each of the next m lines contains two integers t_{i}, c_{i} (1 β€ t_{i} β€ 2, 1 β€ c_{i} β€ 10^6) β type of the gadget and it's cost. For the gadgets of the first type cost is specified in dollars. For the gadgets of the second type cost is specified in pounds.
-----Output-----
If Nura can't buy k gadgets print the only line with the number -1.
Otherwise the first line should contain integer d β the minimum day index, when Nura will have k gadgets. On each of the next k lines print two integers q_{i}, d_{i} β the number of gadget and the day gadget should be bought. All values q_{i} should be different, but the values d_{i} can coincide (so Nura can buy several gadgets at one day). The days are numbered from 1 to n.
In case there are multiple possible solutions, print any of them.
-----Examples-----
Input
5 4 2 2
1 2 3 2 1
3 2 1 2 3
1 1
2 1
1 2
2 2
Output
3
1 1
2 3
Input
4 3 2 200
69 70 71 72
104 105 106 107
1 1
2 2
1 2
Output
-1
Input
4 3 1 1000000000
900000 910000 940000 990000
990000 999000 999900 999990
1 87654
2 76543
1 65432
Output
-1
|
from sys import stdin, stdout
def ints():
return [int(x) for x in stdin.readline().split()]
n, m, k, s = ints()
a = ints()
b = ints()
d_gad = []
p_gad = []
for i in range(m):
t, c = ints()
if t == 1:
d_gad.append([c, i + 1])
else:
p_gad.append([c, i + 1])
d_gad.sort()
p_gad.sort()
mn_dol = [0] * n
mn_pou = [0] * n
day_mn_dol = [1] * n
day_mn_pou = [1] * n
mn_dol[0] = a[0]
mn_pou[0] = b[0]
for i in range(1, n):
mn_dol[i] = min(mn_dol[i - 1], a[i])
day_mn_dol[i] = i + 1 if mn_dol[i] == a[i] else day_mn_dol[i - 1]
for i in range(1, n):
mn_pou[i] = min(mn_pou[i - 1], b[i])
day_mn_pou[i] = i + 1 if mn_pou[i] == b[i] else day_mn_pou[i - 1]
def Check(x):
i = 0
j = 0
mnd = mn_dol[x]
mnp = mn_pou[x]
SuM = 0
for u in range(k):
if i == len(d_gad):
SuM += p_gad[j][0] * mnp
j += 1
elif j == len(p_gad):
SuM += d_gad[i][0] * mnd
i += 1
else:
p1 = d_gad[i][0] * mnd
p2 = p_gad[j][0] * mnp
if p1 <= p2:
SuM += p1
i += 1
else:
SuM += p2
j += 1
if SuM > s:
return False
return True
def Print_Ans(x):
i = 0
j = 0
mnd = mn_dol[x]
mnp = mn_pou[x]
dayd = day_mn_dol[x]
dayp = day_mn_pou[x]
ans = []
for u in range(k):
if i == len(d_gad):
ans.append(str(p_gad[j][1]) + " " + str(dayp))
j += 1
elif j == len(p_gad):
ans.append(str(d_gad[i][1]) + " " + str(dayd))
i += 1
elif d_gad[i][0] * mnd <= p_gad[j][0] * mnp:
ans.append(str(d_gad[i][1]) + " " + str(dayd))
i += 1
else:
ans.append(str(p_gad[j][1]) + " " + str(dayp))
j += 1
stdout.write("\n".join(ans))
if not Check(n - 1):
print(-1)
else:
p = 0
q = n - 1
while p < q:
mid = (p + q) // 2
if Check(mid):
q = mid
else:
p = mid + 1
stdout.write(f"{p + 1}\n")
Print_Ans(p)
|
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER STRING FUNC_CALL VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER STRING FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER STRING FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER STRING FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR
|
Monocarp is playing chess on one popular website. He has $n$ opponents he can play with. The $i$-th opponent has rating equal to $a_i$. Monocarp's initial rating is $x$. Monocarp wants to raise his rating to the value $y$ ($y > x$).
When Monocarp is playing against one of the opponents, he will win if his current rating is bigger or equal to the opponent's rating. If Monocarp wins, his rating is increased by $1$, otherwise it is decreased by $1$. The rating of his opponent does not change.
Monocarp wants to gain rating $y$ playing as few games as possible. But he can't just grind it, playing against weak opponents. The website has a rule that you should play against all opponents as evenly as possible. Speaking formally, if Monocarp wants to play against an opponent $i$, there should be no other opponent $j$ such that Monocarp has played more games against $i$ than against $j$.
Calculate the minimum possible number of games Monocarp needs to gain rating $y$ or say it's impossible. Note that ratings of Monocarp's opponents don't change, while Monocarp's rating does change.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains three integers $n$, $x$ and $y$ ($1 \le n \le 2 \cdot 10^5$; $1 \le x < y \le 10^{12}$) β the number of Monocarp's opponents, his initial and desired ratings.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^{12}$) β ratings of Monocarp's opponents.
Additional constraint on the input: the total sum of $n$ over all $t$ test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print a single integer β the minimum number of games Monocarp needs to play to gain rating $y$, or $-1$ if it's impossible.
-----Examples-----
Input
3
7 2 10
3 1 9 2 5 20 8
7 1 10
3 1 9 2 5 20 8
5 10 12
100 1 200 11 300
Output
20
-1
2
-----Note-----
In the first test case, Monocarp can use the following strategy:
Monocarp plays against the $2$-nd opponent to increase rating ($2 \to 3$);
Monocarp plays against the $1$-st opponent to increase rating ($3 \to 4$);
Monocarp plays against the $4$-th opponent to increase rating ($4 \to 5$);
Monocarp plays against the $5$-th opponent to increase rating ($5 \to 6$);
Now Monocarp have to play with remaining three opponents. So, he will lose $3$ times and get rating $3$ ($6 \to 5 \to 4 \to 3$);
After that, Monocarp will repeat steps 1-5 again. After $14$ games, he has played twice with each opponent and get final rating $4$.
Monocarp plays against the $1$-st opponent to increase rating ($4 \to 5$);
Monocarp plays against the $2$-nd opponent to increase rating ($5 \to 6$);
Monocarp plays against the $4$-th opponent to increase rating ($6 \to 7$);
Monocarp plays against the $5$-th opponent to increase rating ($7 \to 8$);
Monocarp plays against the $7$-th opponent to increase rating ($8 \to 9$);
Monocarp plays against the $3$-rd opponent to increase rating ($9 \to 10$);
In total, Monocarp, played twice against the $6$-th opponent and three times against other opponents and got rating $10$ in $14 + 6 = 20$ games.
In the second test case, it can be proven that whichever games Monocarp plays, he can't get his rating higher than $4$.
|
def div_(xx, yy):
if xx % yy == 0:
return xx // yy
else:
return xx // yy + 1
def search(m):
mi = 0
ma = n - 1
if b[0] > m:
return 0
if b[n - 1] <= m:
return n
while True:
if b[(mi + ma) // 2] <= m:
mi = (mi + ma) // 2
else:
ma = (mi + ma) // 2
if ma - mi <= 1:
return ma
t = int(input())
for u in range(0, t):
n, x, y = map(int, input().split())
a = list(map(int, input().split()))
w = a.sort()
b = [0] * n
b[0] = a[0]
for i in range(1, n):
b[i] = max(b[i - 1], a[i] - i)
out = 0
while True:
r = search(x)
if x + r >= y:
out = out + y - x
break
get_1 = r - (n - r)
if get_1 <= 0:
out = -1
break
if r == n:
round_ = div_(y - x - r, get_1)
y = y - x - round_ * get_1
out = out + round_ * n + y
break
else:
round_ = div_(b[r] - x, get_1)
if x + get_1 * (round_ - 1) < y - r:
x = x + get_1 * round_
out = out + round_ * n
else:
round_ = div_(y - x - r, get_1)
y = y - x - round_ * get_1
out += round_ * n + y
break
print(out)
|
FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR VAR RETURN BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR RETURN VAR WHILE NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Monocarp is playing chess on one popular website. He has $n$ opponents he can play with. The $i$-th opponent has rating equal to $a_i$. Monocarp's initial rating is $x$. Monocarp wants to raise his rating to the value $y$ ($y > x$).
When Monocarp is playing against one of the opponents, he will win if his current rating is bigger or equal to the opponent's rating. If Monocarp wins, his rating is increased by $1$, otherwise it is decreased by $1$. The rating of his opponent does not change.
Monocarp wants to gain rating $y$ playing as few games as possible. But he can't just grind it, playing against weak opponents. The website has a rule that you should play against all opponents as evenly as possible. Speaking formally, if Monocarp wants to play against an opponent $i$, there should be no other opponent $j$ such that Monocarp has played more games against $i$ than against $j$.
Calculate the minimum possible number of games Monocarp needs to gain rating $y$ or say it's impossible. Note that ratings of Monocarp's opponents don't change, while Monocarp's rating does change.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains three integers $n$, $x$ and $y$ ($1 \le n \le 2 \cdot 10^5$; $1 \le x < y \le 10^{12}$) β the number of Monocarp's opponents, his initial and desired ratings.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^{12}$) β ratings of Monocarp's opponents.
Additional constraint on the input: the total sum of $n$ over all $t$ test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print a single integer β the minimum number of games Monocarp needs to play to gain rating $y$, or $-1$ if it's impossible.
-----Examples-----
Input
3
7 2 10
3 1 9 2 5 20 8
7 1 10
3 1 9 2 5 20 8
5 10 12
100 1 200 11 300
Output
20
-1
2
-----Note-----
In the first test case, Monocarp can use the following strategy:
Monocarp plays against the $2$-nd opponent to increase rating ($2 \to 3$);
Monocarp plays against the $1$-st opponent to increase rating ($3 \to 4$);
Monocarp plays against the $4$-th opponent to increase rating ($4 \to 5$);
Monocarp plays against the $5$-th opponent to increase rating ($5 \to 6$);
Now Monocarp have to play with remaining three opponents. So, he will lose $3$ times and get rating $3$ ($6 \to 5 \to 4 \to 3$);
After that, Monocarp will repeat steps 1-5 again. After $14$ games, he has played twice with each opponent and get final rating $4$.
Monocarp plays against the $1$-st opponent to increase rating ($4 \to 5$);
Monocarp plays against the $2$-nd opponent to increase rating ($5 \to 6$);
Monocarp plays against the $4$-th opponent to increase rating ($6 \to 7$);
Monocarp plays against the $5$-th opponent to increase rating ($7 \to 8$);
Monocarp plays against the $7$-th opponent to increase rating ($8 \to 9$);
Monocarp plays against the $3$-rd opponent to increase rating ($9 \to 10$);
In total, Monocarp, played twice against the $6$-th opponent and three times against other opponents and got rating $10$ in $14 + 6 = 20$ games.
In the second test case, it can be proven that whichever games Monocarp plays, he can't get his rating higher than $4$.
|
t = int(input())
for tes in range(t):
n, x, y = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
if a[0] > x:
print(-1)
continue
ans = 1
x += 1
for i in range(1, n):
if x == y:
break
if i <= n // 2 and a[i] > x:
x = y
ans = -1
break
if x >= a[i]:
ans += 1
x += 1
else:
if a[i] >= y:
add = i - (n - i)
u = y - x
r = u // add
x += add * r
ans += n * r
if x == y:
break
ans += n - i
x -= n - i
break
add = i - (n - i)
u = a[i] - x
r = u // add
x += add * r
ans += n * r
if x == a[i]:
x += 1
ans += 1
else:
ans += n - i
x -= n - i
h = y - x
if h <= i:
break
x += i
ans += i
x += 1
ans += 1
ans += y - x
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
Monocarp is playing chess on one popular website. He has $n$ opponents he can play with. The $i$-th opponent has rating equal to $a_i$. Monocarp's initial rating is $x$. Monocarp wants to raise his rating to the value $y$ ($y > x$).
When Monocarp is playing against one of the opponents, he will win if his current rating is bigger or equal to the opponent's rating. If Monocarp wins, his rating is increased by $1$, otherwise it is decreased by $1$. The rating of his opponent does not change.
Monocarp wants to gain rating $y$ playing as few games as possible. But he can't just grind it, playing against weak opponents. The website has a rule that you should play against all opponents as evenly as possible. Speaking formally, if Monocarp wants to play against an opponent $i$, there should be no other opponent $j$ such that Monocarp has played more games against $i$ than against $j$.
Calculate the minimum possible number of games Monocarp needs to gain rating $y$ or say it's impossible. Note that ratings of Monocarp's opponents don't change, while Monocarp's rating does change.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains three integers $n$, $x$ and $y$ ($1 \le n \le 2 \cdot 10^5$; $1 \le x < y \le 10^{12}$) β the number of Monocarp's opponents, his initial and desired ratings.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^{12}$) β ratings of Monocarp's opponents.
Additional constraint on the input: the total sum of $n$ over all $t$ test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print a single integer β the minimum number of games Monocarp needs to play to gain rating $y$, or $-1$ if it's impossible.
-----Examples-----
Input
3
7 2 10
3 1 9 2 5 20 8
7 1 10
3 1 9 2 5 20 8
5 10 12
100 1 200 11 300
Output
20
-1
2
-----Note-----
In the first test case, Monocarp can use the following strategy:
Monocarp plays against the $2$-nd opponent to increase rating ($2 \to 3$);
Monocarp plays against the $1$-st opponent to increase rating ($3 \to 4$);
Monocarp plays against the $4$-th opponent to increase rating ($4 \to 5$);
Monocarp plays against the $5$-th opponent to increase rating ($5 \to 6$);
Now Monocarp have to play with remaining three opponents. So, he will lose $3$ times and get rating $3$ ($6 \to 5 \to 4 \to 3$);
After that, Monocarp will repeat steps 1-5 again. After $14$ games, he has played twice with each opponent and get final rating $4$.
Monocarp plays against the $1$-st opponent to increase rating ($4 \to 5$);
Monocarp plays against the $2$-nd opponent to increase rating ($5 \to 6$);
Monocarp plays against the $4$-th opponent to increase rating ($6 \to 7$);
Monocarp plays against the $5$-th opponent to increase rating ($7 \to 8$);
Monocarp plays against the $7$-th opponent to increase rating ($8 \to 9$);
Monocarp plays against the $3$-rd opponent to increase rating ($9 \to 10$);
In total, Monocarp, played twice against the $6$-th opponent and three times against other opponents and got rating $10$ in $14 + 6 = 20$ games.
In the second test case, it can be proven that whichever games Monocarp plays, he can't get his rating higher than $4$.
|
def solve():
n, x, y = [int(x) for x in input().split()]
ranks = [int(x) for x in input().split()]
ranks.sort()
score = 0
result = 0
while True:
while score < n and x + score >= ranks[score]:
score += 1
if x + score >= y:
result += y - x
break
d = 2 * score - n
if d <= 0:
result = -1
break
m = y
if score < n and ranks[score] < y:
m = ranks[score]
skip_rounds = (m - score - x) // d
result += skip_rounds * n
x += skip_rounds * d
while x + score < m:
x += d
result += n
print(result)
for _ in range(int(input())):
solve()
|
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR WHILE BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process.
There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems.
You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day.
Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days.
Input
The first line of input contains three integers n, m and k (1 β€ n β€ 105, 0 β€ m β€ 105, 1 β€ k β€ 106).
The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 β€ di β€ 106, 0 β€ fi β€ n, 0 β€ ti β€ n, 1 β€ ci β€ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost.
Output
Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities.
If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes).
Examples
Input
2 6 5
1 1 0 5000
3 2 0 5500
2 2 0 6000
15 0 2 9000
9 0 1 7000
8 0 2 6500
Output
24500
Input
2 4 5
1 2 0 5000
2 1 0 4500
2 1 0 3000
8 0 1 6000
Output
-1
Note
The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more.
In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
|
g = lambda: map(int, input().split())
n, m, k = g()
F, T = [], []
e = int(300000000000.0)
for i in range(m):
d, f, t, c = g()
if f:
F.append((d, f, c))
else:
T.append((-d, t, c))
for p in [F, T]:
C = [e] * (n + 1)
s = n * e
q = []
p.sort()
for d, t, c in p:
if C[t] > c:
s += c - C[t]
C[t] = c
if s < e:
q.append((s, d))
p.clear()
p += q
s, t = e, (0, 0)
for f in F:
while f:
if t[1] + f[1] + k < 0:
s = min(s, f[0] + t[0])
elif T:
t = T.pop()
continue
f = 0
print(s if s < e else -1)
|
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR LIST VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR FOR VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR VAR WHILE VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER
|
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process.
There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems.
You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day.
Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days.
Input
The first line of input contains three integers n, m and k (1 β€ n β€ 105, 0 β€ m β€ 105, 1 β€ k β€ 106).
The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 β€ di β€ 106, 0 β€ fi β€ n, 0 β€ ti β€ n, 1 β€ ci β€ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost.
Output
Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities.
If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes).
Examples
Input
2 6 5
1 1 0 5000
3 2 0 5500
2 2 0 6000
15 0 2 9000
9 0 1 7000
8 0 2 6500
Output
24500
Input
2 4 5
1 2 0 5000
2 1 0 4500
2 1 0 3000
8 0 1 6000
Output
-1
Note
The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more.
In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
|
from sys import stdin, stdout
n, m, k = map(int, stdin.readline().rstrip().split())
arrivalFlightList = []
departureFlightList = []
for _ in range(m):
d, f, t, c = map(int, stdin.readline().rstrip().split())
d -= 1
if f == 0:
t -= 1
departureFlightList.append((d, t, c))
else:
f -= 1
arrivalFlightList.append((d, f, c))
arrivalFlightList.sort(key=lambda x: x[0])
currentCostList = [99999999] * n
bestArrivalCostList = [-1] * 1000000
startFlightListLen = n
noFlights = set(range(n))
lastTime = -1
for d, t, c in arrivalFlightList:
if startFlightListLen > 0:
startFlightListLen = len(noFlights)
noFlights.discard(t)
if startFlightListLen == 1 and len(noFlights) == 0:
currentCostList[t] = c
bestArrivalCostList[d] = sum(currentCostList)
startFlightListLen = 0
elif lastTime != d:
for i in range(lastTime + 1, d + 1):
bestArrivalCostList[i] = bestArrivalCostList[lastTime]
if c < currentCostList[t]:
if startFlightListLen == 0:
bestArrivalCostList[d] -= currentCostList[t] - c
currentCostList[t] = c
lastTime = d
for i in range(lastTime + 1, 1000000):
bestArrivalCostList[i] = bestArrivalCostList[lastTime]
departureFlightList.sort(key=lambda x: x[0], reverse=True)
currentCostList = [99999999] * n
departureCostList = [-1] * 1000000
startFlightListLen = n
noFlights = set(range(n))
lastTime = -1
for d, t, c in departureFlightList:
if startFlightListLen > 0:
startFlightListLen = len(noFlights)
noFlights.discard(t)
if startFlightListLen == 1 and len(noFlights) == 0:
currentCostList[t] = c
departureCostList[d] = sum(currentCostList)
startFlightListLen = 0
elif lastTime != d:
for i in range(lastTime - 1, d - 1, -1):
departureCostList[i] = departureCostList[lastTime]
if c < currentCostList[t]:
if startFlightListLen == 0:
departureCostList[d] -= currentCostList[t] - c
currentCostList[t] = c
lastTime = d
for i in range(lastTime - 1, -1, -1):
departureCostList[i] = departureCostList[lastTime]
bestCost = -1
for i in range(1000000 - (k + 1)):
if bestArrivalCostList[i] > 0 and departureCostList[i + k + 1] > 0:
if bestCost < 0:
bestCost = bestArrivalCostList[i] + departureCostList[i + k + 1]
elif bestCost > bestArrivalCostList[i] + departureCostList[i + k + 1]:
bestCost = bestArrivalCostList[i] + departureCostList[i + k + 1]
print(bestCost)
|
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Country of Metropolia is holding Olympiad of Metrpolises soon. It mean that all jury members of the olympiad should meet together in Metropolis (the capital of the country) for the problem preparation process.
There are n + 1 cities consecutively numbered from 0 to n. City 0 is Metropolis that is the meeting point for all jury members. For each city from 1 to n there is exactly one jury member living there. Olympiad preparation is a long and demanding process that requires k days of work. For all of these k days each of the n jury members should be present in Metropolis to be able to work on problems.
You know the flight schedule in the country (jury members consider themselves important enough to only use flights for transportation). All flights in Metropolia are either going to Metropolis or out of Metropolis. There are no night flights in Metropolia, or in the other words, plane always takes off at the same day it arrives. On his arrival day and departure day jury member is not able to discuss the olympiad. All flights in Megapolia depart and arrive at the same day.
Gather everybody for k days in the capital is a hard objective, doing that while spending the minimum possible money is even harder. Nevertheless, your task is to arrange the cheapest way to bring all of the jury members to Metrpolis, so that they can work together for k days and then send them back to their home cities. Cost of the arrangement is defined as a total cost of tickets for all used flights. It is allowed for jury member to stay in Metropolis for more than k days.
Input
The first line of input contains three integers n, m and k (1 β€ n β€ 105, 0 β€ m β€ 105, 1 β€ k β€ 106).
The i-th of the following m lines contains the description of the i-th flight defined by four integers di, fi, ti and ci (1 β€ di β€ 106, 0 β€ fi β€ n, 0 β€ ti β€ n, 1 β€ ci β€ 106, exactly one of fi and ti equals zero), the day of departure (and arrival), the departure city, the arrival city and the ticket cost.
Output
Output the only integer that is the minimum cost of gathering all jury members in city 0 for k days and then sending them back to their home cities.
If it is impossible to gather everybody in Metropolis for k days and then send them back to their home cities, output "-1" (without the quotes).
Examples
Input
2 6 5
1 1 0 5000
3 2 0 5500
2 2 0 6000
15 0 2 9000
9 0 1 7000
8 0 2 6500
Output
24500
Input
2 4 5
1 2 0 5000
2 1 0 4500
2 1 0 3000
8 0 1 6000
Output
-1
Note
The optimal way to gather everybody in Metropolis in the first sample test is to use flights that take place on days 1, 2, 8 and 9. The only alternative option is to send jury member from second city back home on day 15, that would cost 2500 more.
In the second sample it is impossible to send jury member from city 2 back home from Metropolis.
|
def main():
n, m, k = map(int, input().split())
ff, tt = [], []
for _ in range(m):
d, f, t, c = map(int, input().split())
if f:
ff.append((d, f, c))
else:
tt.append((-d, t, c))
for ft in (ff, tt):
cnt, costs = n, [1000001] * (n + 1)
ft.sort(reverse=True)
while ft:
day, city, cost = ft.pop()
oldcost = costs[city]
if oldcost > cost:
costs[city] = cost
if oldcost == 1000001:
cnt -= 1
if not cnt:
break
else:
print(-1)
return
total = sum(costs) - 1000001
l = [(day, total)]
while ft:
day, city, cost = ft.pop()
oldcost = costs[city]
if oldcost > cost:
total -= oldcost - cost
costs[city] = cost
if l[-1][0] == day:
l[-1] = day, total
else:
l.append((day, total))
if ft is ff:
ff = l
else:
tt = l
l, k = [], -k
d, c = tt.pop()
try:
for day, cost in ff:
while d + day >= k:
d, c = tt.pop()
if d + day < k:
l.append(c + cost)
except IndexError:
pass
print(min(l, default=-1))
main()
|
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR VAR WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR VAR FUNC_CALL VAR FOR VAR VAR VAR WHILE BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR
|
You are given two strings s and t of the same length. You want to change s to t. Changing the i-th character of s to i-th character of t costs |s[i] - t[i]| that is, the absolute difference between the ASCII values of the characters.
You are also given an integer maxCost.
Return the maximum length of a substring of s that can be changed to be the same as the corresponding substring of twith a cost less than or equal to maxCost.
If there is no substring fromΒ s that can be changed to its corresponding substring from t, return 0.
Β
Example 1:
Input: s = "abcd", t = "bcdf", maxCost = 3
Output: 3
Explanation: "abc" of s can change to "bcd". That costs 3, so the maximum length is 3.
Example 2:
Input: s = "abcd", t = "cdef", maxCost = 3
Output: 1
Explanation: Each character in s costs 2 to change to charactor in t, so the maximum length is 1.
Example 3:
Input: s = "abcd", t = "acde", maxCost = 0
Output: 1
Explanation: You can't make any change, so the maximum length is 1.
Β
Constraints:
1 <= s.length, t.length <= 10^5
0 <= maxCost <= 10^6
s andΒ t only contain lower case English letters.
|
class Solution:
def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
currcost = 0
i = 0
j = 0
if len(s) == 0:
return 0
if len(s) == 1:
if abs(ord[s[0]]) - ord(t[0]) >= maxcost:
return 1
else:
return 0
currcost = 0
ans = 0
state = False
while i < len(s) and j < len(s):
if currcost + abs(ord(s[j]) - ord(t[j])) <= maxCost:
currcost += abs(ord(s[j]) - ord(t[j]))
j += 1
state = True
else:
ans = max(ans, j - i)
currcost -= abs(ord(s[i]) - ord(t[i]))
i += 1
state = False
if i >= j:
j = i
currcost = 0
if state:
ans = max(ans, j - i)
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
|
You are given two strings s and t of the same length. You want to change s to t. Changing the i-th character of s to i-th character of t costs |s[i] - t[i]| that is, the absolute difference between the ASCII values of the characters.
You are also given an integer maxCost.
Return the maximum length of a substring of s that can be changed to be the same as the corresponding substring of twith a cost less than or equal to maxCost.
If there is no substring fromΒ s that can be changed to its corresponding substring from t, return 0.
Β
Example 1:
Input: s = "abcd", t = "bcdf", maxCost = 3
Output: 3
Explanation: "abc" of s can change to "bcd". That costs 3, so the maximum length is 3.
Example 2:
Input: s = "abcd", t = "cdef", maxCost = 3
Output: 1
Explanation: Each character in s costs 2 to change to charactor in t, so the maximum length is 1.
Example 3:
Input: s = "abcd", t = "acde", maxCost = 0
Output: 1
Explanation: You can't make any change, so the maximum length is 1.
Β
Constraints:
1 <= s.length, t.length <= 10^5
0 <= maxCost <= 10^6
s andΒ t only contain lower case English letters.
|
class Solution:
def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
i = 0
ans = 0
curr = 0
for j in range(0, len(s)):
curr = curr + abs(ord(s[j]) - ord(t[j]))
if curr <= maxCost:
ans = max(ans, j - i + 1)
else:
curr = curr - abs(ord(s[i]) - ord(t[i]))
i = i + 1
print()
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR VAR
|
You are given two strings s and t of the same length. You want to change s to t. Changing the i-th character of s to i-th character of t costs |s[i] - t[i]| that is, the absolute difference between the ASCII values of the characters.
You are also given an integer maxCost.
Return the maximum length of a substring of s that can be changed to be the same as the corresponding substring of twith a cost less than or equal to maxCost.
If there is no substring fromΒ s that can be changed to its corresponding substring from t, return 0.
Β
Example 1:
Input: s = "abcd", t = "bcdf", maxCost = 3
Output: 3
Explanation: "abc" of s can change to "bcd". That costs 3, so the maximum length is 3.
Example 2:
Input: s = "abcd", t = "cdef", maxCost = 3
Output: 1
Explanation: Each character in s costs 2 to change to charactor in t, so the maximum length is 1.
Example 3:
Input: s = "abcd", t = "acde", maxCost = 0
Output: 1
Explanation: You can't make any change, so the maximum length is 1.
Β
Constraints:
1 <= s.length, t.length <= 10^5
0 <= maxCost <= 10^6
s andΒ t only contain lower case English letters.
|
class Solution:
def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
if not s:
return 0
costs = [abs(ord(si) - ord(ti)) for si, ti in zip(s, t)]
ans = 0
l, r = 0, 0
cur_cost = 0
print(costs)
while l < len(s) and r < len(s):
while r < len(s) and cur_cost <= maxCost:
cur_cost += costs[r]
r += 1
ans = max(ans, r - l - 1)
while l < r and cur_cost > maxCost:
cur_cost -= costs[l]
l += 1
ans = max(ans, r - l)
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
|
You are given two strings s and t of the same length. You want to change s to t. Changing the i-th character of s to i-th character of t costs |s[i] - t[i]| that is, the absolute difference between the ASCII values of the characters.
You are also given an integer maxCost.
Return the maximum length of a substring of s that can be changed to be the same as the corresponding substring of twith a cost less than or equal to maxCost.
If there is no substring fromΒ s that can be changed to its corresponding substring from t, return 0.
Β
Example 1:
Input: s = "abcd", t = "bcdf", maxCost = 3
Output: 3
Explanation: "abc" of s can change to "bcd". That costs 3, so the maximum length is 3.
Example 2:
Input: s = "abcd", t = "cdef", maxCost = 3
Output: 1
Explanation: Each character in s costs 2 to change to charactor in t, so the maximum length is 1.
Example 3:
Input: s = "abcd", t = "acde", maxCost = 0
Output: 1
Explanation: You can't make any change, so the maximum length is 1.
Β
Constraints:
1 <= s.length, t.length <= 10^5
0 <= maxCost <= 10^6
s andΒ t only contain lower case English letters.
|
class Solution:
def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
currCost = 0
maxLength = 0
start = 0
curr = 0
costs = [abs(ord(s[i]) - ord(t[i])) for i in range(len(s))]
print(costs)
while curr < len(costs):
currCost += costs[curr]
while currCost > maxCost:
currCost = currCost - costs[start]
start += 1
if curr - start + 1 > maxLength:
maxLength = curr - start + 1
curr += 1
return maxLength
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
|
You are given two strings s and t of the same length. You want to change s to t. Changing the i-th character of s to i-th character of t costs |s[i] - t[i]| that is, the absolute difference between the ASCII values of the characters.
You are also given an integer maxCost.
Return the maximum length of a substring of s that can be changed to be the same as the corresponding substring of twith a cost less than or equal to maxCost.
If there is no substring fromΒ s that can be changed to its corresponding substring from t, return 0.
Β
Example 1:
Input: s = "abcd", t = "bcdf", maxCost = 3
Output: 3
Explanation: "abc" of s can change to "bcd". That costs 3, so the maximum length is 3.
Example 2:
Input: s = "abcd", t = "cdef", maxCost = 3
Output: 1
Explanation: Each character in s costs 2 to change to charactor in t, so the maximum length is 1.
Example 3:
Input: s = "abcd", t = "acde", maxCost = 0
Output: 1
Explanation: You can't make any change, so the maximum length is 1.
Β
Constraints:
1 <= s.length, t.length <= 10^5
0 <= maxCost <= 10^6
s andΒ t only contain lower case English letters.
|
class Solution:
def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
totalCost = 0
maxLength = 0
costs = []
left = 0
length = 0
for i in range(len(s)):
costs.append(abs(ord(s[i]) - ord(t[i])))
totalCost += costs[-1]
length += 1
if totalCost <= maxCost:
maxLength = length if length > maxLength else maxLength
else:
while totalCost > maxCost:
totalCost -= costs[left]
left += 1
length -= 1
return maxLength
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
|
You are given two strings s and t of the same length. You want to change s to t. Changing the i-th character of s to i-th character of t costs |s[i] - t[i]| that is, the absolute difference between the ASCII values of the characters.
You are also given an integer maxCost.
Return the maximum length of a substring of s that can be changed to be the same as the corresponding substring of twith a cost less than or equal to maxCost.
If there is no substring fromΒ s that can be changed to its corresponding substring from t, return 0.
Β
Example 1:
Input: s = "abcd", t = "bcdf", maxCost = 3
Output: 3
Explanation: "abc" of s can change to "bcd". That costs 3, so the maximum length is 3.
Example 2:
Input: s = "abcd", t = "cdef", maxCost = 3
Output: 1
Explanation: Each character in s costs 2 to change to charactor in t, so the maximum length is 1.
Example 3:
Input: s = "abcd", t = "acde", maxCost = 0
Output: 1
Explanation: You can't make any change, so the maximum length is 1.
Β
Constraints:
1 <= s.length, t.length <= 10^5
0 <= maxCost <= 10^6
s andΒ t only contain lower case English letters.
|
class Solution:
def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
start = 0
cost = 0
max_len = 0
for end in range(len(s)):
cost += abs(ord(s[end]) - ord(t[end]))
while cost > maxCost:
cost -= abs(ord(s[start]) - ord(t[start]))
start += 1
max_len = max(max_len, end - start + 1)
return max_len
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
|
You are given two strings s and t of the same length. You want to change s to t. Changing the i-th character of s to i-th character of t costs |s[i] - t[i]| that is, the absolute difference between the ASCII values of the characters.
You are also given an integer maxCost.
Return the maximum length of a substring of s that can be changed to be the same as the corresponding substring of twith a cost less than or equal to maxCost.
If there is no substring fromΒ s that can be changed to its corresponding substring from t, return 0.
Β
Example 1:
Input: s = "abcd", t = "bcdf", maxCost = 3
Output: 3
Explanation: "abc" of s can change to "bcd". That costs 3, so the maximum length is 3.
Example 2:
Input: s = "abcd", t = "cdef", maxCost = 3
Output: 1
Explanation: Each character in s costs 2 to change to charactor in t, so the maximum length is 1.
Example 3:
Input: s = "abcd", t = "acde", maxCost = 0
Output: 1
Explanation: You can't make any change, so the maximum length is 1.
Β
Constraints:
1 <= s.length, t.length <= 10^5
0 <= maxCost <= 10^6
s andΒ t only contain lower case English letters.
|
class Solution:
def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
arr = [abs(ord(sc) - ord(tc)) for sc, tc in zip(s, t)]
max_len = 0
left, right = 0, 0
curr_sum = 0
max_len = 0
while right < len(arr):
curr_sum += arr[right]
while left < len(arr) and curr_sum > maxCost:
curr_sum -= arr[left]
left += 1
max_len = max(max_len, right - left + 1)
right += 1
return max_len
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
|
You are given two strings s and t of the same length. You want to change s to t. Changing the i-th character of s to i-th character of t costs |s[i] - t[i]| that is, the absolute difference between the ASCII values of the characters.
You are also given an integer maxCost.
Return the maximum length of a substring of s that can be changed to be the same as the corresponding substring of twith a cost less than or equal to maxCost.
If there is no substring fromΒ s that can be changed to its corresponding substring from t, return 0.
Β
Example 1:
Input: s = "abcd", t = "bcdf", maxCost = 3
Output: 3
Explanation: "abc" of s can change to "bcd". That costs 3, so the maximum length is 3.
Example 2:
Input: s = "abcd", t = "cdef", maxCost = 3
Output: 1
Explanation: Each character in s costs 2 to change to charactor in t, so the maximum length is 1.
Example 3:
Input: s = "abcd", t = "acde", maxCost = 0
Output: 1
Explanation: You can't make any change, so the maximum length is 1.
Β
Constraints:
1 <= s.length, t.length <= 10^5
0 <= maxCost <= 10^6
s andΒ t only contain lower case English letters.
|
class Solution:
def equalSubstring(self, string: str, target: str, maxCost: int) -> int:
cost = [0] * len(string)
for i in range(len(string)):
if string[i] != target[i]:
cost[i] += abs(ord(string[i]) - ord(target[i]))
if i > 0:
cost[i] += cost[i - 1]
answer = 0
s = 0
for i in range(len(string)):
if cost[i] <= maxCost:
answer = max(answer, i + 1)
else:
while s <= i and cost[i] - cost[s] > maxCost:
s += 1
answer = max(answer, i - s)
return answer
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
|
You are given two strings s and t of the same length. You want to change s to t. Changing the i-th character of s to i-th character of t costs |s[i] - t[i]| that is, the absolute difference between the ASCII values of the characters.
You are also given an integer maxCost.
Return the maximum length of a substring of s that can be changed to be the same as the corresponding substring of twith a cost less than or equal to maxCost.
If there is no substring fromΒ s that can be changed to its corresponding substring from t, return 0.
Β
Example 1:
Input: s = "abcd", t = "bcdf", maxCost = 3
Output: 3
Explanation: "abc" of s can change to "bcd". That costs 3, so the maximum length is 3.
Example 2:
Input: s = "abcd", t = "cdef", maxCost = 3
Output: 1
Explanation: Each character in s costs 2 to change to charactor in t, so the maximum length is 1.
Example 3:
Input: s = "abcd", t = "acde", maxCost = 0
Output: 1
Explanation: You can't make any change, so the maximum length is 1.
Β
Constraints:
1 <= s.length, t.length <= 10^5
0 <= maxCost <= 10^6
s andΒ t only contain lower case English letters.
|
class Solution:
def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
res = 0
c = [0] * (len(s) + 1)
j = 0
for i in range(1, len(s) + 1):
c[i] += c[i - 1] + abs(ord(s[i - 1]) - ord(t[i - 1]))
while c[i] > maxCost:
c[i] -= abs(ord(s[j]) - ord(t[j]))
j += 1
res = max(res, i - j)
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
|
You are given two strings s and t of the same length. You want to change s to t. Changing the i-th character of s to i-th character of t costs |s[i] - t[i]| that is, the absolute difference between the ASCII values of the characters.
You are also given an integer maxCost.
Return the maximum length of a substring of s that can be changed to be the same as the corresponding substring of twith a cost less than or equal to maxCost.
If there is no substring fromΒ s that can be changed to its corresponding substring from t, return 0.
Β
Example 1:
Input: s = "abcd", t = "bcdf", maxCost = 3
Output: 3
Explanation: "abc" of s can change to "bcd". That costs 3, so the maximum length is 3.
Example 2:
Input: s = "abcd", t = "cdef", maxCost = 3
Output: 1
Explanation: Each character in s costs 2 to change to charactor in t, so the maximum length is 1.
Example 3:
Input: s = "abcd", t = "acde", maxCost = 0
Output: 1
Explanation: You can't make any change, so the maximum length is 1.
Β
Constraints:
1 <= s.length, t.length <= 10^5
0 <= maxCost <= 10^6
s andΒ t only contain lower case English letters.
|
class Solution:
def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
dist = [abs(ord(s[i]) - ord(t[i])) for i in range(len(s))]
i = 0
cost = maxCost
for j in range(len(s)):
cost -= dist[j]
if cost < 0:
cost += dist[i]
i += 1
return j - i + 1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER VAR
|
You are given two strings s and t of the same length. You want to change s to t. Changing the i-th character of s to i-th character of t costs |s[i] - t[i]| that is, the absolute difference between the ASCII values of the characters.
You are also given an integer maxCost.
Return the maximum length of a substring of s that can be changed to be the same as the corresponding substring of twith a cost less than or equal to maxCost.
If there is no substring fromΒ s that can be changed to its corresponding substring from t, return 0.
Β
Example 1:
Input: s = "abcd", t = "bcdf", maxCost = 3
Output: 3
Explanation: "abc" of s can change to "bcd". That costs 3, so the maximum length is 3.
Example 2:
Input: s = "abcd", t = "cdef", maxCost = 3
Output: 1
Explanation: Each character in s costs 2 to change to charactor in t, so the maximum length is 1.
Example 3:
Input: s = "abcd", t = "acde", maxCost = 0
Output: 1
Explanation: You can't make any change, so the maximum length is 1.
Β
Constraints:
1 <= s.length, t.length <= 10^5
0 <= maxCost <= 10^6
s andΒ t only contain lower case English letters.
|
class Solution:
def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
cost_array = [abs(a - b) for a, b in zip(map(ord, s), map(ord, t))]
left = right = t = 0
mx = 0
while right < len(cost_array):
t += cost_array[right]
if t > maxCost:
while left < len(cost_array) and t > maxCost:
t -= cost_array[left]
left += 1
right += 1
mx = max(mx, right - left)
return mx
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
|
You are given two strings s and t of the same length. You want to change s to t. Changing the i-th character of s to i-th character of t costs |s[i] - t[i]| that is, the absolute difference between the ASCII values of the characters.
You are also given an integer maxCost.
Return the maximum length of a substring of s that can be changed to be the same as the corresponding substring of twith a cost less than or equal to maxCost.
If there is no substring fromΒ s that can be changed to its corresponding substring from t, return 0.
Β
Example 1:
Input: s = "abcd", t = "bcdf", maxCost = 3
Output: 3
Explanation: "abc" of s can change to "bcd". That costs 3, so the maximum length is 3.
Example 2:
Input: s = "abcd", t = "cdef", maxCost = 3
Output: 1
Explanation: Each character in s costs 2 to change to charactor in t, so the maximum length is 1.
Example 3:
Input: s = "abcd", t = "acde", maxCost = 0
Output: 1
Explanation: You can't make any change, so the maximum length is 1.
Β
Constraints:
1 <= s.length, t.length <= 10^5
0 <= maxCost <= 10^6
s andΒ t only contain lower case English letters.
|
class Solution:
def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
max_substring = 0
curr_substring = 0
left = right = diff = 0
while right < len(s):
local_diff = abs(ord(s[right]) - ord(t[right]))
if diff + local_diff <= maxCost:
diff += local_diff
right += 1
curr_substring += 1
max_substring = max(curr_substring, max_substring)
else:
if left == right:
right += 1
else:
diff -= abs(ord(s[left]) - ord(t[left]))
curr_substring -= 1
left += 1
return max_substring
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
|
You are given two strings s and t of the same length. You want to change s to t. Changing the i-th character of s to i-th character of t costs |s[i] - t[i]| that is, the absolute difference between the ASCII values of the characters.
You are also given an integer maxCost.
Return the maximum length of a substring of s that can be changed to be the same as the corresponding substring of twith a cost less than or equal to maxCost.
If there is no substring fromΒ s that can be changed to its corresponding substring from t, return 0.
Β
Example 1:
Input: s = "abcd", t = "bcdf", maxCost = 3
Output: 3
Explanation: "abc" of s can change to "bcd". That costs 3, so the maximum length is 3.
Example 2:
Input: s = "abcd", t = "cdef", maxCost = 3
Output: 1
Explanation: Each character in s costs 2 to change to charactor in t, so the maximum length is 1.
Example 3:
Input: s = "abcd", t = "acde", maxCost = 0
Output: 1
Explanation: You can't make any change, so the maximum length is 1.
Β
Constraints:
1 <= s.length, t.length <= 10^5
0 <= maxCost <= 10^6
s andΒ t only contain lower case English letters.
|
class Solution:
def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
max_length = 0
start = 0
end = 0
cost = 0
while end < len(s):
c = abs(ord(s[end]) - ord(t[end]))
if cost + c <= maxCost:
cost += c
end += 1
else:
cost -= abs(ord(s[start]) - ord(t[start]))
start += 1
max_length = max(max_length, end - start)
return max_length
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
|
You are given two strings s and t of the same length. You want to change s to t. Changing the i-th character of s to i-th character of t costs |s[i] - t[i]| that is, the absolute difference between the ASCII values of the characters.
You are also given an integer maxCost.
Return the maximum length of a substring of s that can be changed to be the same as the corresponding substring of twith a cost less than or equal to maxCost.
If there is no substring fromΒ s that can be changed to its corresponding substring from t, return 0.
Β
Example 1:
Input: s = "abcd", t = "bcdf", maxCost = 3
Output: 3
Explanation: "abc" of s can change to "bcd". That costs 3, so the maximum length is 3.
Example 2:
Input: s = "abcd", t = "cdef", maxCost = 3
Output: 1
Explanation: Each character in s costs 2 to change to charactor in t, so the maximum length is 1.
Example 3:
Input: s = "abcd", t = "acde", maxCost = 0
Output: 1
Explanation: You can't make any change, so the maximum length is 1.
Β
Constraints:
1 <= s.length, t.length <= 10^5
0 <= maxCost <= 10^6
s andΒ t only contain lower case English letters.
|
class Solution:
def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
arr = []
lim = len(s)
for i in range(0, lim):
arr.append(abs(ord(s[i]) - ord(t[i])))
sm = [arr[0]]
for j in range(1, lim):
sm.append(sm[-1] + arr[j])
l = 0
h = lim
mx = 0
while l <= h:
m = (l + h) // 2
t = 0
for i in range(0, lim - m):
if sm[i + m] - sm[i] + arr[i] <= maxCost:
t = 1
break
if t == 1:
if m + 1 > mx:
mx = m + 1
l = m + 1
else:
h = m - 1
return mx
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
|
You are given two strings s and t of the same length. You want to change s to t. Changing the i-th character of s to i-th character of t costs |s[i] - t[i]| that is, the absolute difference between the ASCII values of the characters.
You are also given an integer maxCost.
Return the maximum length of a substring of s that can be changed to be the same as the corresponding substring of twith a cost less than or equal to maxCost.
If there is no substring fromΒ s that can be changed to its corresponding substring from t, return 0.
Β
Example 1:
Input: s = "abcd", t = "bcdf", maxCost = 3
Output: 3
Explanation: "abc" of s can change to "bcd". That costs 3, so the maximum length is 3.
Example 2:
Input: s = "abcd", t = "cdef", maxCost = 3
Output: 1
Explanation: Each character in s costs 2 to change to charactor in t, so the maximum length is 1.
Example 3:
Input: s = "abcd", t = "acde", maxCost = 0
Output: 1
Explanation: You can't make any change, so the maximum length is 1.
Β
Constraints:
1 <= s.length, t.length <= 10^5
0 <= maxCost <= 10^6
s andΒ t only contain lower case English letters.
|
class Solution:
def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
cost = [abs(ord(s[i]) - ord(t[i])) for i in range(len(s))]
prefix = [0]
for i in range(len(cost)):
prefix.append(prefix[-1] + cost[i])
maxLen = 0
i, j = 0, 0
while j < len(cost):
while j < len(cost) and prefix[j + 1] - prefix[i] <= maxCost:
j += 1
maxLen = max(maxLen, j - i)
i += 1
if i > j:
j = i
return maxLen
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR
|
You are given two strings s and t of the same length. You want to change s to t. Changing the i-th character of s to i-th character of t costs |s[i] - t[i]| that is, the absolute difference between the ASCII values of the characters.
You are also given an integer maxCost.
Return the maximum length of a substring of s that can be changed to be the same as the corresponding substring of twith a cost less than or equal to maxCost.
If there is no substring fromΒ s that can be changed to its corresponding substring from t, return 0.
Β
Example 1:
Input: s = "abcd", t = "bcdf", maxCost = 3
Output: 3
Explanation: "abc" of s can change to "bcd". That costs 3, so the maximum length is 3.
Example 2:
Input: s = "abcd", t = "cdef", maxCost = 3
Output: 1
Explanation: Each character in s costs 2 to change to charactor in t, so the maximum length is 1.
Example 3:
Input: s = "abcd", t = "acde", maxCost = 0
Output: 1
Explanation: You can't make any change, so the maximum length is 1.
Β
Constraints:
1 <= s.length, t.length <= 10^5
0 <= maxCost <= 10^6
s andΒ t only contain lower case English letters.
|
class Solution:
def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
currCost = 0
maxLength = 0
start = 0
curr = 0
while curr < len(s):
currCost += abs(ord(s[curr]) - ord(t[curr]))
while currCost > maxCost:
currCost = currCost - abs(ord(s[start]) - ord(t[start]))
start += 1
if curr - start + 1 > maxLength:
maxLength = curr - start + 1
curr += 1
return maxLength
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
|
You are given two strings s and t of the same length. You want to change s to t. Changing the i-th character of s to i-th character of t costs |s[i] - t[i]| that is, the absolute difference between the ASCII values of the characters.
You are also given an integer maxCost.
Return the maximum length of a substring of s that can be changed to be the same as the corresponding substring of twith a cost less than or equal to maxCost.
If there is no substring fromΒ s that can be changed to its corresponding substring from t, return 0.
Β
Example 1:
Input: s = "abcd", t = "bcdf", maxCost = 3
Output: 3
Explanation: "abc" of s can change to "bcd". That costs 3, so the maximum length is 3.
Example 2:
Input: s = "abcd", t = "cdef", maxCost = 3
Output: 1
Explanation: Each character in s costs 2 to change to charactor in t, so the maximum length is 1.
Example 3:
Input: s = "abcd", t = "acde", maxCost = 0
Output: 1
Explanation: You can't make any change, so the maximum length is 1.
Β
Constraints:
1 <= s.length, t.length <= 10^5
0 <= maxCost <= 10^6
s andΒ t only contain lower case English letters.
|
class Solution:
def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
maxLength = 0
currLength = 0
currCost = 0
for i in range(len(s)):
currLength += 1
currCost += abs(ord(s[i]) - ord(t[i]))
while currCost > maxCost:
currCost -= abs(
ord(s[i - (currLength - 1)]) - ord(t[i - (currLength - 1)])
)
currLength -= 1
maxLength = max(maxLength, currLength)
return maxLength
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
|
You are given two strings s and t of the same length. You want to change s to t. Changing the i-th character of s to i-th character of t costs |s[i] - t[i]| that is, the absolute difference between the ASCII values of the characters.
You are also given an integer maxCost.
Return the maximum length of a substring of s that can be changed to be the same as the corresponding substring of twith a cost less than or equal to maxCost.
If there is no substring fromΒ s that can be changed to its corresponding substring from t, return 0.
Β
Example 1:
Input: s = "abcd", t = "bcdf", maxCost = 3
Output: 3
Explanation: "abc" of s can change to "bcd". That costs 3, so the maximum length is 3.
Example 2:
Input: s = "abcd", t = "cdef", maxCost = 3
Output: 1
Explanation: Each character in s costs 2 to change to charactor in t, so the maximum length is 1.
Example 3:
Input: s = "abcd", t = "acde", maxCost = 0
Output: 1
Explanation: You can't make any change, so the maximum length is 1.
Β
Constraints:
1 <= s.length, t.length <= 10^5
0 <= maxCost <= 10^6
s andΒ t only contain lower case English letters.
|
class Solution:
def equalSubstring(self, s, t, maxCost):
n = len(s)
right = 0
nowSum = 0
res = 0
for left in range(n):
while (
right <= n - 1
and nowSum + abs(ord(s[right]) - ord(t[right])) <= maxCost
):
nowSum += abs(ord(s[right]) - ord(t[right]))
right += 1
res = max(res, right - left)
nowSum -= abs(ord(s[left]) - ord(t[left]))
return res
|
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR
|
You are given two strings s and t of the same length. You want to change s to t. Changing the i-th character of s to i-th character of t costs |s[i] - t[i]| that is, the absolute difference between the ASCII values of the characters.
You are also given an integer maxCost.
Return the maximum length of a substring of s that can be changed to be the same as the corresponding substring of twith a cost less than or equal to maxCost.
If there is no substring fromΒ s that can be changed to its corresponding substring from t, return 0.
Β
Example 1:
Input: s = "abcd", t = "bcdf", maxCost = 3
Output: 3
Explanation: "abc" of s can change to "bcd". That costs 3, so the maximum length is 3.
Example 2:
Input: s = "abcd", t = "cdef", maxCost = 3
Output: 1
Explanation: Each character in s costs 2 to change to charactor in t, so the maximum length is 1.
Example 3:
Input: s = "abcd", t = "acde", maxCost = 0
Output: 1
Explanation: You can't make any change, so the maximum length is 1.
Β
Constraints:
1 <= s.length, t.length <= 10^5
0 <= maxCost <= 10^6
s andΒ t only contain lower case English letters.
|
class Solution:
def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
end = 0
result = 0
allCost = 0
for start in range(len(s)):
if start >= 1:
allCost -= abs(ord(s[start - 1]) - ord(t[start - 1]))
while (
end + 1 < len(s) + 1
and allCost + abs(ord(s[end]) - ord(t[end])) <= maxCost
):
end += 1
allCost += abs(ord(s[end - 1]) - ord(t[end - 1]))
result = max(result, end - start)
return result
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
|
You are given two strings s and t of the same length. You want to change s to t. Changing the i-th character of s to i-th character of t costs |s[i] - t[i]| that is, the absolute difference between the ASCII values of the characters.
You are also given an integer maxCost.
Return the maximum length of a substring of s that can be changed to be the same as the corresponding substring of twith a cost less than or equal to maxCost.
If there is no substring fromΒ s that can be changed to its corresponding substring from t, return 0.
Β
Example 1:
Input: s = "abcd", t = "bcdf", maxCost = 3
Output: 3
Explanation: "abc" of s can change to "bcd". That costs 3, so the maximum length is 3.
Example 2:
Input: s = "abcd", t = "cdef", maxCost = 3
Output: 1
Explanation: Each character in s costs 2 to change to charactor in t, so the maximum length is 1.
Example 3:
Input: s = "abcd", t = "acde", maxCost = 0
Output: 1
Explanation: You can't make any change, so the maximum length is 1.
Β
Constraints:
1 <= s.length, t.length <= 10^5
0 <= maxCost <= 10^6
s andΒ t only contain lower case English letters.
|
class Solution:
def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
start = 0
end = 0
cost = 0
best = 0
while end < len(s):
while end < len(s) and cost + abs(ord(s[end]) - ord(t[end])) <= maxCost:
cost += abs(ord(s[end]) - ord(t[end]))
end += 1
if start == end:
start += 1
end += 1
continue
best = max(best, end - start)
cost -= abs(ord(s[start]) - ord(t[start]))
start += 1
return best
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
for _ in range(int(input())):
n, m = map(int, input().split())
l = []
c = 1
for v in map(int, input().split()):
l += [(v, c)]
if c == m:
c = 1
else:
c += 1
l = sorted(l, key=lambda x: x[0])
a = [0] * (m + 1)
s = set()
w = []
res = 1000000009
i = 0
while 1:
while len(s) != m and i < n:
s.add(l[i][1])
a[l[i][1]] += 1
w += [l[i]]
i += 1
if len(s) == m:
res = min(res, abs(w[0][0] - w[-1][0]))
if w[0] == w[-1]:
break
a[w[0][1]] -= 1
if a[w[0][1]] == 0:
s.remove(w[0][1])
w.pop(0)
print(res)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR LIST VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR LIST VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
for _ in range(int(input())):
n, m = map(int, input().split())
my_list = list(map(int, input().split()))
my_list_2 = list(set(my_list))
my_dict = {}
for i in my_list_2:
my_dict[i] = []
for i in range(n):
jk = i % m
my_dict[my_list[i]].append(jk)
s = 99**99
s1 = 99**99
my_list_2 = list(my_dict.keys())
my_list_2.sort(reverse=True)
j = -1
n = []
c = [0] * m
my_dif_c = 0
for i in range(len(my_list_2)):
while j + 1 < len(my_list_2) and my_dif_c != m:
j = j + 1
for k in my_dict[my_list_2[j]]:
if c[k] == 0:
my_dif_c += 1
c[k] += 1
if my_dif_c == m:
f = 1
break
if my_dif_c == m:
s1 = abs(my_list_2[i] - my_list_2[j])
s = min(s, s1)
for k in my_dict[my_list_2[i]]:
c[k] -= 1
if c[k] == 0:
my_dif_c -= 1
print(s)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
def inp():
t = int(input())
while t > 0:
n, m = map(int, input().split())
a = list(map(int, input().split()))
count = 0
pairs = []
for num in a:
pairs.append((num, count % m))
count += 1
pairs.sort()
newList = a[:m]
maxi = max(newList)
mini = min(newList)
diff = maxi - mini
for i in range(0, n):
miniflag = False
maxiFlag = False
if newList[pairs[i][1]] == mini:
miniflag = True
if newList[pairs[i][1]] == maxi:
maxiFlag = True
newList[pairs[i][1]] = pairs[i][0]
if miniflag == True or maxiFlag == True:
mini = min(newList)
maxi = max(newList)
else:
maxi = max(maxi, pairs[i][0])
diff = min(diff, maxi - mini)
print(diff)
t = t - 1
inp()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN 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 LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
def try3(n, m, pts_s):
i = 0
mi = 10**10
res = [-1] * m
tmp = m
mi_i = -1
while i < n - m - 1:
if tmp == 0:
mi = min(mi, res[pts_s[i - 1][1]] - min(res))
if mi == 0:
break
if res[pts_s[i][1]] == -1:
tmp -= 1
res[pts_s[i][1]] = pts_s[i][0]
i += 1
print(mi)
def try2():
for _ in range(int(input())):
n, m = map(int, input().split())
k = 0
pts = []
for i in input().split():
pts.append([int(i), k % m])
k += 1
pts_s = sorted(pts)
if m == n:
print(pts_s[-1][0] - pts_s[0][0])
continue
if n > 1000:
try3(n, m, pts_s)
continue
i = 0
mi = 10**10
while i < n - m:
ct = [0] * m
tmp = m
for j in range(i, n):
if ct[pts_s[j][1]] == 0:
tmp -= 1
ct[pts_s[j][1]] = 1
if tmp == 0:
mi = min(mi, pts_s[j][0] - pts_s[i][0])
break
i += 1
if tmp == 0:
mi = min(mi, pts_s[-1][0] - pts_s[i - 1][0])
if mi == 0:
break
print(mi)
def try1():
for _ in range(int(input())):
n, m = map(int, input().split())
a = [int(i) for i in input().split()]
ar1 = []
if m == n:
print(max(a) - min(a))
continue
mi_v = 10**8
mi_i = 0
kk = 0
for i in range(0, n, m):
ar1.append(a[i : i + m])
tp = max(ar1[kk]) - min(ar1[kk])
if mi_v > tp and len(ar1[kk]) == m:
mi_v = tp
mi_i = kk
kk += 1
res = ar1[mi_i]
mi = min(res)
ma = max(res)
for i in range(len(ar1)):
if i == kk:
continue
for j in range(min(len(res), len(ar1[i]))):
if ar1[i][j] >= mi and ar1[i][j] <= ma:
res[j] = ar1[i][j]
mi = min(res)
ma = max(res)
print(max(res) - min(res))
try2()
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST IF VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
for case in range(int(input())):
n, m = [int(x) for x in input().split()]
all_boxes = [int(x) for x in input().split()]
color_wise_boxes = []
for j in range(0, n, m):
for i in range(m):
try:
color_wise_boxes.append([all_boxes[i + j], i])
except IndexError:
break
color_wise_boxes.sort()
initials = {}
diff = 10000000000.0
_min = 10000000000.0
_max = 0
flag = False
for box in color_wise_boxes:
min_flag = False
try:
if initials[box[1]] == _min:
min_flag = True
except KeyError:
pass
initials[box[1]] = box[0]
if not flag:
if len(initials) >= m:
flag = True
if (initials[box[1]] < _min or initials[box[1]] > _max) and flag:
if min_flag or _min == 10000000000.0:
_min = min(initials.values())
_max = initials[box[1]]
diff = min(diff, abs(_max - _min))
print(diff)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER IF VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
INF = 10**9 + 5
for _ in range(int(input())):
N, M = map(int, input().split())
arr = list(map(int, input().split()))
pl = {}
for i in range(N):
act = arr[i]
if act not in pl:
pl[act] = set([i % M])
else:
pl[act].add(i % M)
arr2 = list(pl.keys())
arr2.sort()
i1, i2 = 0, 0
acts = [0] * M
miss = M
bst = INF
len2 = len(arr2)
while i2 < len2:
while i2 < len2 and miss > 0:
for idx in pl[arr2[i2]]:
if acts[idx] == 0:
miss -= 1
acts[idx] += 1
if miss > 0:
i2 += 1
if i2 < len2:
while miss == 0:
bst = min(bst, arr2[i2] - arr2[i1])
for idx in pl[arr2[i1]]:
acts[idx] -= 1
if acts[idx] == 0:
miss += 1
i1 += 1
i2 += 1
print(bst)
|
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR LIST BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR WHILE VAR VAR VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
for tc in range(int(input())):
n, m = map(int, input().split())
a = list(map(int, input().split()))
b = []
temp = []
for i in range(m):
for j in range(i, n, m):
temp.append((a[j], i))
temp = sorted(temp, key=lambda x: x[0])
start = 0
finish = 1
res = 10**10
dic = {}
for i in range(m):
dic[i] = 0
li = [temp[start][1]]
dic[temp[start][1]] = 1
flag = 0
for i in range(finish, n):
dic[temp[i][1]] += 1
if flag == 0 and li.count(temp[i][1]) == 0:
li.append(temp[i][1])
finish = i
if flag == 0 and len(li) == m:
flag = 1
x = abs(temp[start][0] - temp[finish][0])
if x < res:
res = x
start += 1
dic[temp[start - 1][1]] -= 1
if flag == 1:
finish = i
while 1:
if dic[temp[start - 1][1]] == 0:
break
else:
x = abs(temp[start][0] - temp[finish][0])
if x < res:
res = x
start += 1
dic[temp[start - 1][1]] -= 1
print(res)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER NUMBER IF VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR WHILE NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
for _ in range(int(input())):
n, m = map(int, input().split())
ll = list(map(int, input().split()))
a = []
for i in range(n):
a.append([])
a[i].append(ll[i])
a[i].append(i % m)
l = sorted(a, key=lambda lll: lll[0])
a = [0] * m
j = 0
ans = 0
for i in range(n):
if a[l[i][1]] == 0:
ans += 1
a[l[i][1]] += 1
if ans == m:
j = i
ans = l[i][0] - l[0][0]
break
for i in range(1, n - m + 1):
if ans == 0:
break
index = l[i - 1][1]
a[index] -= 1
if a[index] != 0:
ans = min(ans, l[j][0] - l[i][0])
else:
for k in range(j + 1, n):
a[l[k][1]] += 1
if l[k][1] == index:
j = k
ans = min(ans, l[j][0] - l[i][0])
break
else:
break
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
for x in range(int(input())):
m, n = map(int, input().split())
l = list(map(int, input().split()))
l1 = []
l2 = []
l3 = [0] * n
t = 0
flagi = 0
for x in range(len(l)):
l1.append([l[x], x % n])
l1.sort()
count = 0
for y in range(m):
if count != n:
if l3[l1[y][1]] == 0:
k = 0
if flagi != 1:
k = y
flagi = 1
l3[l1[y][1]] += 1
count += 1
if count == n:
t = y + 1
l2.append(abs(l1[y][0] - l1[k][0]))
break
else:
l3[l1[y][1]] += 1
for x in range(m - n):
l3[l1[x][1]] -= 1
flag = 0
if l3[l1[x][1]] == 0:
for y in range(t, m):
if l1[y][1] == l1[x][1]:
l3[l1[y][1]] += 1
t = y + 1
l2.append(abs(l1[y][0] - l1[x + 1][0]))
flag = 1
break
else:
l3[l1[y][1]] += 1
if flag == 0:
break
else:
l2.append(abs(l1[t - 1][0] - l1[x + 1][0]))
print(min(l2))
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
try:
for _ in range(int(input())):
n, m = map(int, input().split())
vis = [0] * m
l = list(map(int, input().split()))
l = list([l[i], i % m] for i in range(n))
l.sort()
res = l[n - 1][0] - l[0][0]
s = 0
for i in l:
vis[i[1]] += 1
if vis.count(0) < 1:
while vis[l[s][1]] > 1:
vis[l[s][1]] -= 1
s += 1
if res > i[0] - l[s][0]:
res = i[0] - l[s][0]
print(res)
except EOFError:
print("0")
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR NUMBER NUMBER WHILE VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR STRING
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
for _ in range(int(input().strip())):
n, m = [int(i) for i in input().strip().split()]
old_boxes = [(int(inp), i % m) for i, inp in enumerate(input().strip().split())]
old_boxes.sort()
old_boxes.append((0, m))
first = 0
boxes = []
for i in range(1, n + 1):
if old_boxes[first][1] != old_boxes[i][1]:
boxes.append(
(old_boxes[first][0], old_boxes[i - 1][0], old_boxes[first][1])
)
first = i
cnt_colors = [(0) for _ in range(m)]
unselected_colors = {i for i in range(m)}
first = 0
mini = 10**10
cnt_colors[boxes[first][2]] += 1
unselected_colors -= {boxes[first][2]}
for i in range(1, len(boxes)):
cnt_colors[boxes[i][2]] += 1
unselected_colors -= {boxes[i][2]}
while not unselected_colors:
ball_cnt = boxes[i][0] - boxes[first][1]
if ball_cnt < mini:
mini = ball_cnt
cnt_colors[boxes[first][2]] -= 1
if not cnt_colors[boxes[first][2]]:
unselected_colors.add(boxes[first][2])
first += 1
print(mini)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER WHILE VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
t = int(input())
def sortSecond(val):
return val[1]
def minDiff(arr, N, K, w, chk, res):
wa = res
for i in range(N - K + 1):
curSeqDiff = arr[i + K - 1] - arr[i]
if curSeqDiff < res:
yes = set(chk).issubset(set(w[i : i + K]))
if curSeqDiff < res and yes:
res = curSeqDiff
elif curSeqDiff < res and ~yes:
wa = curSeqDiff
return res, wa
def sol(a, n, m, w, chk, ans):
ca, wa = minDiff(a, n, m, w, chk, ans)
while wa < ca:
c1, wa = minDiff(a, n, m + 1, w, chk, ca)
if c1 < ca:
ca = c1
if m >= n:
break
m += 1
print(ca)
def minDiff1(a, n, m, w):
co = [0] * m
done = [False] * m
doall = [True] * m
ca = a[n - 1] - a[0]
i = 0
j = 0
while i < n:
done[w[i]] = True
co[w[i]] += 1
if w[i] == w[j] and i != j:
co[w[j]] -= 1
j += 1
if done == doall:
while co[w[j]] > 1:
co[w[j]] -= 1
j += 1
ca = min(a[i] - a[j], ca)
co[w[j]] -= 1
if co[w[j]] == 0:
done[w[j]] = False
j += 1
i += 1
print(ca)
for i in range(t):
n, m = input().split()
n = int(n)
m = int(m)
a = [int(i) for i in input().split()]
sin = [j[0] for j in sorted(enumerate(a), key=lambda x: x[1])]
w = [0] * n
for j in range(n):
w[j] = sin[j] % m
a.sort()
ans = 9147483647
chk = [int(i) for i in range(m)]
if m == n:
print(a[n - 1] - a[0])
minDiff1(a, n, m, w)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN VAR NUMBER FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
t = int(input())
for q in range(t):
n, m = map(int, input().split())
a = list(map(int, input().split()))
l = []
for i in range(n):
l.append((a[i], i % m))
l.sort()
visit = [0] * m
m_l = [0] * m
c = 0
for i in range(n):
max_ = l[i][0]
if visit[l[i][1]] == 0:
c += 1
visit[l[i][1]] = 1
m_l[l[i][1]] = l[i][0]
if c == m:
min_ = min(m_l)
res = max_ - min_
break
i += 1
while i < n:
y = m_l[l[i][1]]
max_ = l[i][0]
m_l[l[i][1]] = l[i][0]
if y == min_:
min_ = min(m_l)
res = min(res, max_ - min_)
i += 1
print(res)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
for _ in range(int(input())):
n, m = map(int, input().split())
a = [int(x) for x in input().split()]
colors = list(range(m)) * (n // m) + list(range(n % m))
a = sorted([[x, y] for x, y in zip(a, colors)])
color_slots = [None] * m
min_arr = []
min_ind = 0
c_cnt = 0
res = 10**9 + 10
for val, color in a:
min_arr.append(val)
if color_slots[color] is None:
c_cnt += 1
else:
min_arr[color_slots[color]] = None
color_slots[color] = len(min_arr) - 1
if c_cnt == m:
mx = min_arr[-1]
while min_arr[min_ind] is None:
min_ind += 1
mn = min_arr[min_ind]
res = min(res, mx - mn)
print(res)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NONE VAR NUMBER ASSIGN VAR VAR VAR NONE ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR NONE VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
import sys
from itertools import combinations
def a_le():
return list(map(int, sys.stdin.readline().strip().split()))
def int_le():
return map(int, sys.stdin.readline().strip().split())
def input():
return sys.stdin.readline().strip()
for _ in range(int(input())):
N, M = map(int, input().split())
A = a_le()
B = []
if N == M:
print(max(A) - min(A))
else:
for i in range(len(A)):
j = i % M + 1
B.append([A[i], j])
B.sort()
C = [0] * M
count = 0
Res = []
for i, j in B:
if count < M:
if C[j - 1] == 0:
count += 1
C[j - 1] = i
if 0 not in C:
count -= 1
Res.append(max(C) - min(C))
C[C.index(min(C))] = 0
print(min(Res))
|
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST IF VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR IF VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
for t in range(int(input())):
n, m = list(map(int, input().split()))
a = list(map(int, input().split()))
d = {}
for i in range(n):
if a[i] in d.keys():
if (i + 1) % m != 0:
d[a[i]].append((i + 1) % m)
else:
d[a[i]].append(m)
elif (i + 1) % m != 0:
d[a[i]] = [(i + 1) % m]
else:
d[a[i]] = [m]
a.sort()
indices = []
for i in range(n):
indices.append(d[a[i]][0])
d[a[i]] = d[a[i]][1:]
d = [[] for i in range(m)]
for i in range(n):
d[indices[i] - 1].append(i)
tracker = [(0) for i in range(m)]
indices_covered = []
corr_value = []
length = 0
min_diff = 0
diff = 0
check = "false"
index = 0
starter = 0
lowest = 0
lind = 0
lcorr = 0
for i in range(n):
check = "false"
if tracker[indices[i] - 1] == 0:
tracker[indices[i] - 1] = 1
indices_covered.append(indices[i])
lind += 1
corr_value.append(a[i])
lcorr += 1
length += 1
if length == m:
for j in range(lowest, lcorr):
if corr_value[j] != "-":
min_diff = corr_value[-1] - corr_value[j]
lowest = j
break
else:
start = indices_covered[lowest]
index = -(d[indices[i] - 1][1] - d[indices[i] - 1][0])
corr_value[index] = "-"
indices_covered[index] = "-"
for s in range(lowest, lcorr):
if corr_value[s] != "-":
lowest = s
break
corr_value.append(a[i])
lcorr += 1
indices_covered.append(indices[i])
lind += 1
del d[indices[i] - 1][0]
if length != m:
continue
if start == indices[i]:
check = "true"
if check == "true":
diff = a[i] - corr_value[lowest]
min_diff = min(min_diff, diff)
print(min_diff)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR LIST BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR STRING IF VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR STRING FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR IF VAR VAR VAR ASSIGN VAR STRING IF VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
t = int(input())
def minDiff1(a, n, m, w):
co = [0] * m
done = [False] * m
doall = [True] * m
ca = a[n - 1] - a[0]
i = 0
j = 0
while i < n:
done[w[i]] = True
co[w[i]] += 1
if w[i] == w[j] and i != j:
co[w[j]] -= 1
j += 1
if done == doall:
while co[w[j]] > 1:
co[w[j]] -= 1
j += 1
ca = min(a[i] - a[j], ca)
co[w[j]] -= 1
if co[w[j]] == 0:
done[w[j]] = False
j += 1
i += 1
print(ca)
for i in range(t):
n, m = input().split()
n = int(n)
m = int(m)
a = [int(i) for i in input().split()]
sin = [j[0] for j in sorted(enumerate(a), key=lambda x: x[1])]
w = [0] * n
for j in range(n):
w[j] = sin[j] % m
a.sort()
ans = 9147483647
chk = [int(i) for i in range(m)]
if m == n:
print(a[n - 1] - a[0])
minDiff1(a, n, m, w)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR 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 VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
def findsub(string, pat):
len1 = len(string)
len2 = len(pat)
hash_pat = [0] * (10**5 + 1)
hash_str = [0] * (10**5 + 1)
for i in range(0, len2):
hash_pat[pat[i]] += 1
mindif = 10**10
start, start_index, min_len = 0, -1, float("inf")
count = 0
for j in range(0, len1):
hash_str[string[j][1]] += 1
if (
hash_pat[string[j][1]] != 0
and hash_str[string[j][1]] <= hash_pat[string[j][1]]
):
count += 1
if count == len2:
while (
hash_str[string[start][1]] > hash_pat[string[start][1]]
or hash_pat[string[start][1]] == 0
):
if hash_str[string[start][1]] > hash_pat[string[start][1]]:
hash_str[string[start][1]] -= 1
start += 1
len_window = j - start + 1
tmp = string[j][0] - string[start][0]
mindif = min(mindif, tmp)
return mindif
for ii in range(int(input())):
n, m = map(int, input().split())
p = list(map(int, input().split()))
k = [i for i in range(0, m)]
cnt = 1
s = []
for i in p:
s.append([i, cnt])
cnt = (cnt + 1) % m
s.sort()
a = findsub(s, k)
print(a)
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
class Sequence:
def __init__(self, m):
self.m = m
self.stack = []
self.indexes = []
self.present = {}
for i in range(self.m):
self.present[i] = False
def isComplete(self):
if len(self.stack) == self.m:
return True
return False
def isEmpty(self):
if len(self.stack) == 0:
return True
return False
def push(self, obj):
self.stack.append(obj)
self.indexes.append(obj.color)
def pop(self, index):
self.stack.pop(index)
self.indexes.pop(index)
class Box:
def __init__(self, value, color):
self.value = value
self.color = color
tests = int(input())
while tests:
tests -= 1
n, m = map(int, input().split())
if m > n:
m = n
l = list(map(int, input().split()))
Boxes = []
color = 0
for i in range(n):
if color == m:
color = 0
Boxes.append(Box(l[i], color))
color += 1
Boxes.sort(key=lambda x: x.value)
prev = Boxes[0]
_min = 10**10
sequence = Sequence(m)
for i in range(n):
if sequence.isEmpty():
sequence.push(Boxes[i])
sequence.present[Boxes[i].color] = True
continue
if sequence.present[Boxes[i].color]:
if not sequence.isComplete():
index = sequence.indexes.index(Boxes[i].color)
sequence.pop(index)
sequence.push(Boxes[i])
else:
sequence.push(Boxes[i])
sequence.present[Boxes[i].color] = True
if sequence.isComplete():
minBox = sequence.stack[0]
maxBox = sequence.stack[m - 1]
sequence.pop(0)
sequence.present[minBox.color] = False
_diff = maxBox.value - minBox.value
if _min > _diff:
_min = _diff
print(_min)
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR IF FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
def solve(l):
mi = [float("inf"), -1]
ma = 0
for i in range(len(l)):
if l[i] < mi[0]:
mi[0] = l[i]
mi[1] = i
if l[i] > ma:
ma = l[i]
return ma - mi[0], mi[1]
for t in range(int(input())):
n, m = [int(i) for i in input().split()]
a = [(int(j), i % m) for i, j in enumerate(input().split())]
a.sort()
l = [None for i in range(m)]
last = 0
index = []
diff = float("inf")
sel = 0
all_sel = 2**m - 1
for i in range(n):
l[a[i][1]] = a[i][0]
last = a[i][0]
sel = sel | 1 << a[i][1]
index.append(a[i][1])
if sel == all_sel:
d, val = solve(l)
sel = sel & ~(1 << val)
diff = min(diff, d)
print(diff)
|
FUNC_DEF ASSIGN VAR LIST FUNC_CALL VAR STRING NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN BIN_OP VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NONE VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
t = int(input())
for itr in range(t):
nm = input().split(" ")
n, m = int(nm[0]), int(nm[1])
a = list(map(int, input().split(" ")))
collis = {}
aa = [[a[i], i % m] for i in range(n)]
aa.sort(reverse=True)
mini = 1000000000
i = 0
while len(collis.keys()) < m:
if collis.get(aa[i][1]) is None:
collis[aa[i][1]] = 1
else:
collis[aa[i][1]] += 1
i += 1
j = 0
i -= 1
while j < n:
if aa[j][0] - aa[i][0] < mini:
mini = aa[j][0] - aa[i][0]
fl = False
temp = aa[j][0]
while j < n and aa[j][0] == temp:
if collis.get(aa[j][1]) is not None:
collis[aa[j][1]] -= 1
if collis[aa[j][1]] == 0:
del collis[aa[j][1]]
j += 1
if j >= n:
break
i = max(i + 1, j)
flag = False
while len(collis.keys()) < m:
if i == n:
flag = True
break
if collis.get(aa[i][1]) is None:
collis[aa[i][1]] = 1
else:
collis[aa[i][1]] += 1
i += 1
if flag:
break
i -= 1
print(mini)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT ASSIGN VAR LIST VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER NONE ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR NUMBER NONE VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER NONE ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
t = int(input())
for _ in range(t):
length, m = map(int, input().split())
a = list(map(int, input().split()))
ans = [0] * m
answer = 10000000000
res = [[0, 0] for i in range(length)]
lookup = [0] * m
for i in range(length):
res[i][0] = a[i]
res[i][1] = i % m
res = sorted(res)
for i in range(length):
maxi = res[i][0]
index = res[i][1]
removed = ans[index]
ans[index] = res[i][0]
lookup.remove(removed)
lookup.append(res[i][0])
mini = lookup[0]
if mini != 0:
answer = min(answer, maxi - mini)
print(answer)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
t = int(input())
for _ in range(t):
n, m = map(int, input().split())
arr = [int(x) for x in input().split()]
mat = []
for j in range(m):
temp = [arr[i] for i in range(j, n, m)]
temp.sort()
mat.append(temp)
mini = 10**10
flag = 0
temp = [mat[i][0] for i in range(m)]
count = 0
while flag != 1 and mini != 0 and count < 2000:
count += 1
if max(temp) - min(temp) < mini:
mini = max(temp) - min(temp)
my_index = arr.index(min(temp)) % m
arr[arr.index(min(temp))] = -1
mat[my_index].pop(0)
if len(mat[my_index]) == 0:
flag = 1
break
temp.remove(min(temp))
if len(mat[my_index]) != 0:
temp.append(mat[my_index][0])
print(mini)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
t = int(input())
for _ in range(t):
n, m = map(int, input().split())
func = lambda x: (int(x[1]), x[0] % m)
arr = list(map(func, enumerate(input().split())))
arr.sort()
cnt = [0] * m
cnt0 = m
r = 0
ans = float("inf")
for l in range(n):
while r < n and cnt0 > 0:
cnt0 -= cnt[arr[r][1]] == 0
cnt[arr[r][1]] += 1
r += 1
if cnt0 == 0:
ans = min(ans, arr[r - 1][0] - arr[l][0])
cnt[arr[l][1]] -= 1
cnt0 += cnt[arr[l][1]] == 0
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
t = int(input())
for _ in range(t):
n, m = map(int, input().split())
a = list(map(int, input().split()))
final = []
final = [(j % m) for j in sorted([i for i in range(n)], key=lambda x: a[x])]
a.sort()
found = {}
i = 0
j = 0
ans = a[-1] - a[0]
while i < n - m + 1 and j < n:
while j < n and len(found.keys()) < m:
if final[j] in found:
found[final[j]] += 1
else:
found[final[j]] = 1
j += 1
while i < n - m + 1 and len(found.keys()) == m:
ans = min(ans, a[j - 1] - a[i])
found[final[i]] -= 1
if found[final[i]] == 0:
del found[final[i]]
i += 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR WHILE VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER WHILE VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
t = int(input())
while t > 0:
t -= 1
n, m = input().split()
n, m = int(n), int(m)
k = list(map(int, input().split()))
s = [0] * n
j = 0
v = [0] * m
while j != n:
for i in range(m):
s[j] = i
j += 1
if j >= n:
break
z = [x for _, x in sorted(zip(k, s))]
h = sorted(k)
c = 0
a = 0
minn = 100000000000000
for i in range(n):
if v[z[i]] == 0:
c += 1
v[z[i]] += 1
if c == m:
while c >= m:
temp = abs(h[a] - h[i])
if minn > temp:
minn = temp
v[z[a]] -= 1
if v[z[a]] != 0:
a += 1
else:
c -= 1
a += 1
print(minn)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
import sys
def smallestDifference(colors, arr):
color_count = [0] * colors
arr_with_color = list(zip(arr, [i for i in range(len(arr))]))
arr_with_color.sort(key=lambda x: x[0])
i, j = 0, 0
mini_diff = sys.maxsize
count = 0
while i <= len(arr):
if count < colors and i < len(arr):
color_count[arr_with_color[i][1] % colors] += 1
if color_count[arr_with_color[i][1] % colors] == 1:
count += 1
i += 1
elif count >= colors:
mini_diff = min(mini_diff, arr_with_color[i - 1][0] - arr_with_color[j][0])
color_count[arr_with_color[j][1] % colors] -= 1
if color_count[arr_with_color[j][1] % colors] == 0:
count -= 1
j += 1
else:
break
return mini_diff
try:
testcases = int(input())
for i in range(testcases):
arr_len, colors = list(map(int, input().strip().split()))
arr = list(map(int, input().strip().split()))
print(smallestDifference(colors, arr))
except:
pass
|
IMPORT FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
t = int(input())
for i in range(t):
n, m = list(map(int, input().split()))
arr = list(map(int, input().split()))
currArr = [arr[x] for x in range(m)]
arr = [[arr[x], x % m] for x in range(n)]
arr = sorted(arr)
maxVal = max(currArr)
minVal = min(currArr)
answer = maxVal - minVal
for x in range(0, len(arr)):
a, b = arr[x]
if currArr[b] == maxVal:
currArr[b] = a
maxVal = max(currArr)
minVal = min(currArr)
if currArr[b] == minVal:
currArr[b] = a
minVal = min(currArr)
maxVal = max(currArr)
currArr[b] = a
if maxVal - minVal < answer:
answer = maxVal - minVal
print(answer)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
def sortSecond(val):
return val[0]
for T in range(int(input())):
n, m = map(int, input().split())
l = list(map(int, input().split()))
temp = []
for i in range(n):
temp.append([l[i], i % m])
temp.sort(key=sortSecond)
temp1 = [0] * m
st = 0
for i in range(n):
temp1[temp[i][1]] += 1
if not 0 in temp1:
end = i
break
dif = temp[end][0] - temp[st][0]
while end < n:
if not 0 in temp1:
if temp[end][0] - temp[st][0] < dif:
dif = temp[end][0] - temp[st][0]
temp1[temp[st][1]] -= 1
st += 1
else:
end += 1
if end == n:
break
temp1[temp[end][1]] += 1
print(dif)
|
FUNC_DEF RETURN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER IF NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER WHILE VAR VAR IF NUMBER VAR IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
def customSort(a):
return a[0]
T = int(input())
for t in range(T):
N, M = list(map(int, input().strip().split(" ")))
A_temp = list(map(int, input().strip().split(" ")))
A = []
for n in range(N):
A.append([A_temp[n], n % M])
A = sorted(A, key=customSort)
colors = set()
boxes = dict()
diffs = []
min_element_index = 0
insertedBoxes = []
indexOfColors = dict()
valuesOfIndex = dict()
for i in range(N):
max_element = A[i][0]
if A[i][1] in indexOfColors:
del valuesOfIndex[indexOfColors[A[i][1]]]
boxes[A[i][1]] = A[i][0]
indexOfColors[A[i][1]] = i
insertedBoxes.append(A[i][0])
valuesOfIndex[i] = A[i][0]
colors.add(A[i][1])
if len(colors) == M:
min_element = list(valuesOfIndex.values())[0]
diffs.append(max_element - min_element)
ans = min(diffs)
print(ans)
|
FUNC_DEF RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
for q in range(int(input())):
n, m = map(int, input().split())
l1, l, c, f, r, result = (
[int(x) for x in input().split()],
[],
[0] * m,
0,
0,
10**10,
)
for i in range(0, n):
l2 = []
l2.append(l1[i]), l2.append(i % m)
l.append(l2)
l.sort()
for i in range(n):
while r < n and f < m:
if c[l[r][1]] == 0:
f += 1
c[l[r][1]] += 1
r += 1
if f == m:
result = min(result, l[r - 1][0] - l[i][0])
c[l[i][1]] -= 1
if c[l[i][1]] == 0:
f -= 1
print(result)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR LIST BIN_OP LIST NUMBER VAR NUMBER NUMBER BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
t = int(input())
for i in range(t):
n, m = map(int, input().split())
a = [int(v) for v in input().split()]
s = [[(0) for j in range(2)] for i in range(n)]
k = 0
for j in range(n):
s[j][0] = a[j]
s[j][1] = k
k = k + 1
if k == m:
k = 0
s.sort()
v = {}
start = 0
k = m
res = s[n - 1][0] - s[0][0]
for j in range(n):
if s[j][1] in v:
v[s[j][1]] = v[s[j][1]] + 1
else:
v[s[j][1]] = 1
while len(v) == k:
an = s[j][0] - s[start][0]
if an < res:
res = an
if s[start][1] in v:
if v[s[start][1]] == 1:
del v[s[start][1]]
start = start + 1
else:
v[s[start][1]] = v[s[start][1]] - 1
start = start + 1
print(res)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER VAR IF VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
def find(N, M, arr):
pos = []
for i in range(len(arr)):
pos.append([i % M, i])
pos.sort(key=lambda x: arr[x[1]])
arr.sort()
cnt = [0] * M
mask = 2**M - 1
i = 0
j = 0
while pos[i][0] == pos[j][0]:
cnt[pos[j][0]] += 1
j += 1
cnt[pos[j][0]] += 1
chk = 1 << pos[i][0]
chk = chk | 1 << pos[j][0]
res = float("inf")
while j < N:
while chk != mask:
j += 1
if j == N:
break
chk = chk | 1 << pos[j][0]
cnt[pos[j][0]] += 1
if j == N:
continue
flag = False
while True:
if cnt[pos[i][0]] == 1 and flag:
break
if cnt[pos[i][0]] == 1:
res = min(res, arr[j] - arr[i])
cnt[pos[i][0]] -= 1
if cnt[pos[i][0]] < 1:
chk = chk ^ 1 << pos[i][0]
flag = True
i += 1
return res
def main():
for i in range(int(input())):
N, M = list(map(int, input().split()))
arr = list(map(int, input().split()))
print(find(N, M, arr))
main()
|
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR VAR WHILE VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR NUMBER NUMBER VAR IF VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
for _ in range(int(input())):
n, m = [int(x) for x in input().split()]
l = [int(x) for x in input().split()]
a = [(i % m) for i in range(n)]
l1 = []
values = []
a = [a for _, a in sorted(zip(l, a))]
l.sort()
max_val = 99999999999
arr = [(-1) for x in range(m)]
for i in range(n):
arr[a[i]] = i
if -1 not in arr:
m1 = max(arr)
m2 = min(arr)
if max_val > l[m1] - l[m2]:
max_val = l[m1] - l[m2]
arr[arr.index(m2)] = -1
print(max_val)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
class MinHeapNode:
def __init__(self):
self.element = 0
self.i = 0
self.j = 0
class MinHeap:
def __init__(self, a, size):
self.heap_size = size
self.harr = a
i = (self.heap_size - 1) // 2
while i >= 0:
self.MinHeapify(i)
i -= 1
def left(self, i):
return 2 * i + 1
def right(self, i):
return 2 * i + 2
def getMin(self):
return self.harr[0]
def replaceMin(self, x):
self.harr[0] = x
self.MinHeapify(0)
def MinHeapify(self, i):
l = self.left(i)
r = self.right(i)
smallest = i
if l < self.heap_size and self.harr[l].element < self.harr[i].element:
smallest = l
if r < self.heap_size and self.harr[r].element < self.harr[smallest].element:
smallest = r
if smallest != i:
temp = self.harr[i]
self.harr[i] = self.harr[smallest]
self.harr[smallest] = temp
self.MinHeapify(smallest)
def findSmallestRange(arr, k, N):
rangee = float("inf")
minn = float("inf")
maxx = -float("inf")
harr = [(0) for i in range(k)]
for i in range(k):
harr[i] = MinHeapNode()
harr[i].element = arr[i][0]
harr[i].i = i
harr[i].j = 1
if harr[i].element > maxx:
maxx = harr[i].element
hp = MinHeap(harr, k)
while 1:
root = hp.getMin()
minn = hp.getMin().element
if rangee > maxx - minn + 1:
rangee = maxx - minn + 1
start = minn
end = maxx
if root.j < N[root.i]:
root.element = arr[root.i][root.j]
root.j += 1
if root.element > maxx:
maxx = root.element
else:
break
hp.replaceMin(root)
print(end - start)
t = int(input())
for i in range(t):
n, m = [int(i) for i in input().split()]
temp = [int(i) for i in input().split()]
arr = [[] for i in range(m)]
N = [(0) for i in range(m)]
for i in range(n):
arr[i % m].append(temp[i])
N[i % m] += 1
for i in range(m):
arr[i].sort()
findSmallestRange(arr, m, N)
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF RETURN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
def next(mapping, bucket_count, bucket_count_set, colors, start):
end = start
while end < len(mapping):
ball, bucket = mapping[end]
bucket_count_set.add(bucket)
bucket_count[bucket] += 1
if len(bucket_count_set) == colors:
return end, bucket_count, bucket_count_set
end += 1
def next_2(mapping, bucket_count, bucket_count_set, colors, end):
if len(bucket_count_set) == colors:
return end, bucket_count, bucket_count_set
end += 1
while end < len(mapping):
_, bucket = mapping[end]
bucket_count_set.add(bucket)
bucket_count[bucket] += 1
if len(bucket_count_set) == colors:
return end, bucket_count, bucket_count_set
end += 1
def ans(mapping, colors):
bucket_count = [0] * colors
bucket_count_set = set()
start = 0
end, bucket_count, bucket_count_set = next(
mapping, bucket_count, bucket_count_set, colors, start
)
start_ball, start_bucket = mapping[start]
end_ball, end_bucket = mapping[end]
minimax = end_ball - start_ball
bucket_count[start_bucket] -= 1
start += 1
if bucket_count[start_bucket] == 0:
bucket_count_set.remove(start_bucket)
while start < len(mapping):
ne = next_2(mapping, bucket_count, bucket_count_set, colors, end)
if ne:
end, bucket_count, bucket_count_set = ne
start_ball, start_bucket = mapping[start]
end_ball, _ = mapping[end]
minimax = min(minimax, end_ball - start_ball)
bucket_count[start_bucket] -= 1
start += 1
if bucket_count[start_bucket] == 0:
bucket_count_set.remove(start_bucket)
else:
return minimax
test = int(input())
while test > 0:
test -= 1
buckets, colors = map(int, input().split())
balls = list(map(int, input().split()))
mapping = []
for i in range(len(balls)):
ball = balls[i]
bucket = i % colors
mapping.append((ball, bucket))
mapping.sort()
print(ans(mapping, colors))
|
FUNC_DEF ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN 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 LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Read problem statements in [Mandarin Chinese] and [Vietnamese] as well.
Chef placed $N$ boxes (numbered $1$ through $N$) in a straight line. For each valid $i$, the $i$-th box contains $A_{i}$ balls.
Then, Chef painted the boxes using $M$ distinct colours in such a way that for each $M$ consecutive boxes, no two of these boxes have the same colour.
Help Chef choose $M$ distinct boxes (not necessarily consecutive) such that no two of the chosen boxes have the same colour and the difference between the number of balls in a box that contains the maximum number of balls and a box that contains the minimum number of balls among the chosen boxes is the smallest possible.
------ Input ------
The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
The first line of each test case contains two space-separated integers $N$ and $M$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \ldots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the minimum possible difference between the maximum and minimum number of balls in the chosen boxes.
------ Constraints ------
$1 β€ T β€ 5$
$2 β€ M β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{9}$, for each valid $i$
------ Subtasks ------
Subtask #1 (30 points): $N β€ 10^{3}$
Subtask #2 (70 points): original constraints
----- Sample Input 1 ------
1
4 2
10 20 15 28
----- Sample Output 1 ------
5
----- explanation 1 ------
Example case 1: Let's denote the colours by 'R' (red) and 'G' (green). There are two possible ways of painting the boxes: "RGRG" and "GRGR".
There are four ways to choose two boxes (one red and one green box). The numbers of balls in them and the differences are:
- $(10, 28)$ with difference $28-10=18$
- $(10, 20)$ with difference $20-10=10$
- $(15, 28)$ with difference $28-15=13$
- $(15, 20)$ with difference $20-15=5$, which is the smallest possible
|
t = int(input())
for _ in range(t):
n, m = list(map(int, input().split()))
ar = list(map(int, input().split()))
col = 0
arr = []
for i in range(n):
arr.append([ar[i], col % m])
col += 1
arr.sort(key=lambda x: x[0])
dic = {}
i = j = 0
ans = float("inf")
while j < n:
dic[arr[j][1]] = dic.get(arr[j][1], 0) + 1
while dic and dic[arr[i][1]] > 1:
dic[arr[i][1]] -= 1
i += 1
if len(dic) == m:
ans = min(ans, arr[j][0] - arr[i][0])
j += 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell $(0, 0)$. Robot can perform the following four kinds of operations: U β move from $(x, y)$ to $(x, y + 1)$; D β move from $(x, y)$ to $(x, y - 1)$; L β move from $(x, y)$ to $(x - 1, y)$; R β move from $(x, y)$ to $(x + 1, y)$.
Vasya also has got a sequence of $n$ operations. Vasya wants to modify this sequence so after performing it the robot will end up in $(x, y)$.
Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: $maxID - minID + 1$, where $maxID$ is the maximum index of a changed operation, and $minID$ is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices $2$, $5$ and $7$ are changed, so the length of changed subsegment is $7 - 2 + 1 = 6$. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is $1$.
If there are no changes, then the length of changed subsegment is $0$. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.
Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from $(0, 0)$ to $(x, y)$, or tell him that it's impossible.
-----Input-----
The first line contains one integer number $n~(1 \le n \le 2 \cdot 10^5)$ β the number of operations.
The second line contains the sequence of operations β a string of $n$ characters. Each character is either U, D, L or R.
The third line contains two integers $x, y~(-10^9 \le x, y \le 10^9)$ β the coordinates of the cell where the robot should end its path.
-----Output-----
Print one integer β the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from $(0, 0)$ to $(x, y)$. If this change is impossible, print $-1$.
-----Examples-----
Input
5
RURUU
-2 3
Output
3
Input
4
RULR
1 1
Output
0
Input
3
UUU
100 100
Output
-1
-----Note-----
In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is $3 - 1 + 1 = 3$.
In the second example the given sequence already leads the robot to $(x, y)$, so the length of the changed subsegment is $0$.
In the third example the robot can't end his path in the cell $(x, y)$.
|
n = int(input())
s = list(input())
x, y = map(int, input().split())
it = [0, 0, 0, 0]
for i in range(len(s)):
if s[i] == "U":
s[i] = 0
elif s[i] == "R":
s[i] = 1
elif s[i] == "D":
s[i] = 2
else:
s[i] = 3
it[s[i]] += 1
def distance(x, y, xx, yy):
return abs(x - xx) + abs(y - yy)
def yes(steps, ost, x, y):
xx = steps[1] - steps[3]
yy = steps[0] - steps[2]
return distance(x, y, xx, yy) <= ost
ans = 0
if distance(x, y, 0, 0) > n or (x + y + n) % 2 == 1:
print(-1)
elif yes(it, 0, x, y):
print(0)
else:
i = -1
cur_ans = 0
max_ans = 0
steps = [0, 0, 0, 0]
while yes(steps, n - cur_ans, x, y):
i += 1
steps[s[i]] += 1
cur_ans += 1
steps[s[i]] -= 1
i -= 1
cur_ans -= 1
max_ans = cur_ans
j = n
ok = True
while j > 0 and ok:
j = j - 1
steps[s[j]] += 1
cur_ans += 1
while i >= 0 and not yes(steps, n - cur_ans, x, y):
steps[s[i]] -= 1
i -= 1
cur_ans -= 1
if yes(steps, n - cur_ans, x, y) and cur_ans > max_ans:
max_ans = cur_ans
ok = i >= 0 or yes(steps, n - cur_ans, x, y)
print(n - max_ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER FUNC_DEF RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER WHILE FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell $(0, 0)$. Robot can perform the following four kinds of operations: U β move from $(x, y)$ to $(x, y + 1)$; D β move from $(x, y)$ to $(x, y - 1)$; L β move from $(x, y)$ to $(x - 1, y)$; R β move from $(x, y)$ to $(x + 1, y)$.
Vasya also has got a sequence of $n$ operations. Vasya wants to modify this sequence so after performing it the robot will end up in $(x, y)$.
Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: $maxID - minID + 1$, where $maxID$ is the maximum index of a changed operation, and $minID$ is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices $2$, $5$ and $7$ are changed, so the length of changed subsegment is $7 - 2 + 1 = 6$. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is $1$.
If there are no changes, then the length of changed subsegment is $0$. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.
Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from $(0, 0)$ to $(x, y)$, or tell him that it's impossible.
-----Input-----
The first line contains one integer number $n~(1 \le n \le 2 \cdot 10^5)$ β the number of operations.
The second line contains the sequence of operations β a string of $n$ characters. Each character is either U, D, L or R.
The third line contains two integers $x, y~(-10^9 \le x, y \le 10^9)$ β the coordinates of the cell where the robot should end its path.
-----Output-----
Print one integer β the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from $(0, 0)$ to $(x, y)$. If this change is impossible, print $-1$.
-----Examples-----
Input
5
RURUU
-2 3
Output
3
Input
4
RULR
1 1
Output
0
Input
3
UUU
100 100
Output
-1
-----Note-----
In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is $3 - 1 + 1 = 3$.
In the second example the given sequence already leads the robot to $(x, y)$, so the length of the changed subsegment is $0$.
In the third example the robot can't end his path in the cell $(x, y)$.
|
from sys import stdin
n = int(stdin.readline())
s = stdin.readline().strip()
x, y = [int(z) for z in stdin.readline().split()]
x2 = s.count("R") - s.count("L")
y2 = s.count("U") - s.count("D")
if x == x2 and y == y2:
print(0)
elif (x - x2 + y - y2) % 2 == 1:
print(-1)
elif x + y > n:
print(-1)
else:
low = n * 2
xD = x2 - x
yD = y2 - y
flex = 0
left = 0
right = 0
while right < n:
while abs(xD) + abs(yD) > flex and right < n:
char = s[right]
if char == "R":
xD -= 1
elif char == "L":
xD += 1
elif char == "U":
yD -= 1
else:
yD += 1
flex += 1
right += 1
while abs(xD) + abs(yD) <= flex:
low = min(low, right - left)
char = s[left]
if char == "R":
xD += 1
elif char == "L":
xD -= 1
elif char == "U":
yD += 1
else:
yD -= 1
flex -= 1
left += 1
if low == n * 2:
print(-1)
else:
print(low)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR STRING FUNC_CALL VAR STRING IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER WHILE BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell $(0, 0)$. Robot can perform the following four kinds of operations: U β move from $(x, y)$ to $(x, y + 1)$; D β move from $(x, y)$ to $(x, y - 1)$; L β move from $(x, y)$ to $(x - 1, y)$; R β move from $(x, y)$ to $(x + 1, y)$.
Vasya also has got a sequence of $n$ operations. Vasya wants to modify this sequence so after performing it the robot will end up in $(x, y)$.
Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: $maxID - minID + 1$, where $maxID$ is the maximum index of a changed operation, and $minID$ is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices $2$, $5$ and $7$ are changed, so the length of changed subsegment is $7 - 2 + 1 = 6$. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is $1$.
If there are no changes, then the length of changed subsegment is $0$. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.
Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from $(0, 0)$ to $(x, y)$, or tell him that it's impossible.
-----Input-----
The first line contains one integer number $n~(1 \le n \le 2 \cdot 10^5)$ β the number of operations.
The second line contains the sequence of operations β a string of $n$ characters. Each character is either U, D, L or R.
The third line contains two integers $x, y~(-10^9 \le x, y \le 10^9)$ β the coordinates of the cell where the robot should end its path.
-----Output-----
Print one integer β the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from $(0, 0)$ to $(x, y)$. If this change is impossible, print $-1$.
-----Examples-----
Input
5
RURUU
-2 3
Output
3
Input
4
RULR
1 1
Output
0
Input
3
UUU
100 100
Output
-1
-----Note-----
In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is $3 - 1 + 1 = 3$.
In the second example the given sequence already leads the robot to $(x, y)$, so the length of the changed subsegment is $0$.
In the third example the robot can't end his path in the cell $(x, y)$.
|
def check(num):
r = R[n - 1] - R[num - 1]
l = L[n - 1] - L[num - 1]
u = U[n - 1] - U[num - 1]
d = D[n - 1] - D[num - 1]
if abs(x - (r - l)) + abs(y - (u - d)) <= num:
return True
for i in range(num, n):
r = R[n - 1] - (R[i] - R[i - num])
l = L[n - 1] - (L[i] - L[i - num])
u = U[n - 1] - (U[i] - U[i - num])
d = D[n - 1] - (D[i] - D[i - num])
if abs(x - (r - l)) + abs(y - (u - d)) <= num:
return True
return False
n = int(input())
s = input()
x, y = map(int, input().split())
R, L, U, D = {}, {}, {}, {}
for i in range(n):
R[i] = R[i - 1] if i > 0 else 0
L[i] = L[i - 1] if i > 0 else 0
U[i] = U[i - 1] if i > 0 else 0
D[i] = D[i - 1] if i > 0 else 0
if s[i] == "R":
R[i] += 1
elif s[i] == "L":
L[i] += 1
elif s[i] == "U":
U[i] += 1
else:
D[i] += 1
if abs(x - R[n - 1] + L[n - 1]) + abs(y - (U[n - 1] - D[n - 1])) == 0:
print(0)
exit(0)
if (abs(x) + abs(y)) % 2 != n % 2:
print(-1)
exit(0)
low = 1
high = n
while low < high:
mid = (low + high) // 2
if check(mid):
high = mid - 1
else:
low = mid + 1
if check(low):
print(low)
else:
if low == n:
print(-1)
exit(0)
print(low + 1)
|
FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR IF BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR DICT DICT DICT DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR NUMBER IF VAR VAR STRING VAR VAR NUMBER IF VAR VAR STRING VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell $(0, 0)$. Robot can perform the following four kinds of operations: U β move from $(x, y)$ to $(x, y + 1)$; D β move from $(x, y)$ to $(x, y - 1)$; L β move from $(x, y)$ to $(x - 1, y)$; R β move from $(x, y)$ to $(x + 1, y)$.
Vasya also has got a sequence of $n$ operations. Vasya wants to modify this sequence so after performing it the robot will end up in $(x, y)$.
Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: $maxID - minID + 1$, where $maxID$ is the maximum index of a changed operation, and $minID$ is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices $2$, $5$ and $7$ are changed, so the length of changed subsegment is $7 - 2 + 1 = 6$. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is $1$.
If there are no changes, then the length of changed subsegment is $0$. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.
Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from $(0, 0)$ to $(x, y)$, or tell him that it's impossible.
-----Input-----
The first line contains one integer number $n~(1 \le n \le 2 \cdot 10^5)$ β the number of operations.
The second line contains the sequence of operations β a string of $n$ characters. Each character is either U, D, L or R.
The third line contains two integers $x, y~(-10^9 \le x, y \le 10^9)$ β the coordinates of the cell where the robot should end its path.
-----Output-----
Print one integer β the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from $(0, 0)$ to $(x, y)$. If this change is impossible, print $-1$.
-----Examples-----
Input
5
RURUU
-2 3
Output
3
Input
4
RULR
1 1
Output
0
Input
3
UUU
100 100
Output
-1
-----Note-----
In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is $3 - 1 + 1 = 3$.
In the second example the given sequence already leads the robot to $(x, y)$, so the length of the changed subsegment is $0$.
In the third example the robot can't end his path in the cell $(x, y)$.
|
import sys
def input():
return sys.stdin.readline().rstrip()
def slv():
n = int(input())
s = list(input())
x, y = map(int, input().split())
if not (abs(x) + abs(y) <= n and (abs(x) + abs(y) - n) % 2 == 0):
print(-1)
return
tot_x, tot_y = 0, 0
for c in s:
if c == "D":
tot_y -= 1
elif c == "U":
tot_y += 1
elif c == "R":
tot_x += 1
else:
tot_x -= 1
def possible(R):
if R < 0:
return False
idxl, idxr = 0, 0
rx, ry = 0, 0
addition = 0
while idxr < n:
c = s[idxr]
if c == "D":
ry -= 1
elif c == "U":
ry += 1
elif c == "R":
rx += 1
else:
rx -= 1
addition += 1
idxr += 1
if addition > R:
c = s[idxl]
if c == "D":
ry += 1
elif c == "U":
ry -= 1
elif c == "R":
rx -= 1
else:
rx += 1
idxl += 1
addition -= 1
if addition == R:
move_x, move_y = x - (tot_x - rx), y - (tot_y - ry)
if (
abs(move_x) + abs(move_y) <= R
and R % 2 == (abs(move_x) + abs(move_y)) % 2
):
return True
return False
ans = n + 100
if possible(2 * (n // 2)):
l = -1
r = n // 2
while r - l > 1:
med = (l + r) // 2
if possible(2 * med):
r = med
else:
l = med
ans = min(ans, 2 * r)
if possible(2 * ((n - 1) // 2) + 1):
l = -1
r = (n - 1) // 2
while r - l > 1:
med = (l + r) // 2
if possible(2 * med + 1):
r = med
else:
l = med
ans = min(ans, 2 * r + 1)
print(ans)
return
def main():
t = 1
for i in range(t):
slv()
return
main()
|
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR IF FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN EXPR FUNC_CALL VAR
|
Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell $(0, 0)$. Robot can perform the following four kinds of operations: U β move from $(x, y)$ to $(x, y + 1)$; D β move from $(x, y)$ to $(x, y - 1)$; L β move from $(x, y)$ to $(x - 1, y)$; R β move from $(x, y)$ to $(x + 1, y)$.
Vasya also has got a sequence of $n$ operations. Vasya wants to modify this sequence so after performing it the robot will end up in $(x, y)$.
Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: $maxID - minID + 1$, where $maxID$ is the maximum index of a changed operation, and $minID$ is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices $2$, $5$ and $7$ are changed, so the length of changed subsegment is $7 - 2 + 1 = 6$. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is $1$.
If there are no changes, then the length of changed subsegment is $0$. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.
Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from $(0, 0)$ to $(x, y)$, or tell him that it's impossible.
-----Input-----
The first line contains one integer number $n~(1 \le n \le 2 \cdot 10^5)$ β the number of operations.
The second line contains the sequence of operations β a string of $n$ characters. Each character is either U, D, L or R.
The third line contains two integers $x, y~(-10^9 \le x, y \le 10^9)$ β the coordinates of the cell where the robot should end its path.
-----Output-----
Print one integer β the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from $(0, 0)$ to $(x, y)$. If this change is impossible, print $-1$.
-----Examples-----
Input
5
RURUU
-2 3
Output
3
Input
4
RULR
1 1
Output
0
Input
3
UUU
100 100
Output
-1
-----Note-----
In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is $3 - 1 + 1 = 3$.
In the second example the given sequence already leads the robot to $(x, y)$, so the length of the changed subsegment is $0$.
In the third example the robot can't end his path in the cell $(x, y)$.
|
def valid(step, tx, ty, nx, ny, s, d):
fx = 0
fy = 0
for i in range(len(s)):
c = s[i]
fx += d[c][0]
fy += d[c][1]
if i >= step:
c = s[i - step]
fx -= d[c][0]
fy -= d[c][1]
if i >= step - 1:
diff = abs(nx - fx - tx) + abs(ny - fy - ty)
if diff <= step and (step - diff) % 2 == 0:
return True
return False
def main():
d = {"U": (0, 1), "D": (0, -1), "L": (-1, 0), "R": (1, 0)}
nx = 0
ny = 0
n = int(input())
s = input()
tx, ty = map(lambda x: int(x), input().split(" "))
diff = abs(tx) + abs(ty)
if diff > len(s) or (diff - len(s)) % 2 == 1:
print(-1)
return
for c in s:
nx += d[c][0]
ny += d[c][1]
if (nx, ny) == (tx, ty):
print(0)
return
l = 0
r = len(s)
ans = r
while l < r:
m = (l + r) // 2
if valid(m, tx, ty, nx, ny, s, d):
ans = m
r = m
else:
l = m + 1
print(ans)
main()
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR DICT STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER RETURN FOR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell $(0, 0)$. Robot can perform the following four kinds of operations: U β move from $(x, y)$ to $(x, y + 1)$; D β move from $(x, y)$ to $(x, y - 1)$; L β move from $(x, y)$ to $(x - 1, y)$; R β move from $(x, y)$ to $(x + 1, y)$.
Vasya also has got a sequence of $n$ operations. Vasya wants to modify this sequence so after performing it the robot will end up in $(x, y)$.
Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: $maxID - minID + 1$, where $maxID$ is the maximum index of a changed operation, and $minID$ is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices $2$, $5$ and $7$ are changed, so the length of changed subsegment is $7 - 2 + 1 = 6$. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is $1$.
If there are no changes, then the length of changed subsegment is $0$. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.
Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from $(0, 0)$ to $(x, y)$, or tell him that it's impossible.
-----Input-----
The first line contains one integer number $n~(1 \le n \le 2 \cdot 10^5)$ β the number of operations.
The second line contains the sequence of operations β a string of $n$ characters. Each character is either U, D, L or R.
The third line contains two integers $x, y~(-10^9 \le x, y \le 10^9)$ β the coordinates of the cell where the robot should end its path.
-----Output-----
Print one integer β the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from $(0, 0)$ to $(x, y)$. If this change is impossible, print $-1$.
-----Examples-----
Input
5
RURUU
-2 3
Output
3
Input
4
RULR
1 1
Output
0
Input
3
UUU
100 100
Output
-1
-----Note-----
In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is $3 - 1 + 1 = 3$.
In the second example the given sequence already leads the robot to $(x, y)$, so the length of the changed subsegment is $0$.
In the third example the robot can't end his path in the cell $(x, y)$.
|
n = int(input())
a = input()
x, y = map(int, input().split())
locs = [(0, 0)]
for i in range(n):
if a[i] == "U":
locs.append((locs[-1][0], locs[-1][1] + 1))
elif a[i] == "D":
locs.append((locs[-1][0], locs[-1][1] - 1))
elif a[i] == "R":
locs.append((locs[-1][0] + 1, locs[-1][1]))
else:
locs.append((locs[-1][0] - 1, locs[-1][1]))
if abs(x) + abs(y) > n or (x + y - n) % 2 == 1:
print(-1)
elif locs[-1] == (x, y):
print(0)
else:
a = 0
b = 0
best = n
end = locs[-1]
while True:
c, d = locs[a][0] + end[0] - locs[b][0], locs[a][1] + end[1] - locs[b][1]
if abs(c - x) + abs(d - y) <= b - a:
best = min(best, b - a)
a += 1
else:
b += 1
if b > n:
print(best)
break
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER WHILE NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR
|
Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell $(0, 0)$. Robot can perform the following four kinds of operations: U β move from $(x, y)$ to $(x, y + 1)$; D β move from $(x, y)$ to $(x, y - 1)$; L β move from $(x, y)$ to $(x - 1, y)$; R β move from $(x, y)$ to $(x + 1, y)$.
Vasya also has got a sequence of $n$ operations. Vasya wants to modify this sequence so after performing it the robot will end up in $(x, y)$.
Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: $maxID - minID + 1$, where $maxID$ is the maximum index of a changed operation, and $minID$ is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices $2$, $5$ and $7$ are changed, so the length of changed subsegment is $7 - 2 + 1 = 6$. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is $1$.
If there are no changes, then the length of changed subsegment is $0$. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.
Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from $(0, 0)$ to $(x, y)$, or tell him that it's impossible.
-----Input-----
The first line contains one integer number $n~(1 \le n \le 2 \cdot 10^5)$ β the number of operations.
The second line contains the sequence of operations β a string of $n$ characters. Each character is either U, D, L or R.
The third line contains two integers $x, y~(-10^9 \le x, y \le 10^9)$ β the coordinates of the cell where the robot should end its path.
-----Output-----
Print one integer β the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from $(0, 0)$ to $(x, y)$. If this change is impossible, print $-1$.
-----Examples-----
Input
5
RURUU
-2 3
Output
3
Input
4
RULR
1 1
Output
0
Input
3
UUU
100 100
Output
-1
-----Note-----
In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is $3 - 1 + 1 = 3$.
In the second example the given sequence already leads the robot to $(x, y)$, so the length of the changed subsegment is $0$.
In the third example the robot can't end his path in the cell $(x, y)$.
|
def doable(n, x, y, m, prefixLR, prefixUD):
for i in range(n - m + 1):
j = i + m - 1
dx = prefixLR[i] + prefixLR[-1] - prefixLR[j + 1]
dy = prefixUD[i] + prefixUD[-1] - prefixUD[j + 1]
if abs(x - dx) + abs(y - dy) <= m:
return True
return False
def main():
n = int(input())
s = list(input().strip())
x, y = map(int, input().strip().split())
k = abs(x) + abs(y)
if k > n or k % 2 != n % 2:
print(-1)
return
prefixLR = [0] * (n + 1)
prefixUD = [0] * (n + 1)
for i in range(n):
prefixLR[i + 1] = prefixLR[i]
prefixUD[i + 1] = prefixUD[i]
if s[i] == "L":
prefixLR[i + 1] -= 1
elif s[i] == "R":
prefixLR[i + 1] += 1
elif s[i] == "D":
prefixUD[i + 1] -= 1
else:
prefixUD[i + 1] += 1
left = 0
right = n
while left < right:
mid = left + (right - left) // 2
if doable(n, x, y, mid, prefixLR, prefixUD):
right = mid
else:
left = mid + 1
print(left)
main()
|
FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell $(0, 0)$. Robot can perform the following four kinds of operations: U β move from $(x, y)$ to $(x, y + 1)$; D β move from $(x, y)$ to $(x, y - 1)$; L β move from $(x, y)$ to $(x - 1, y)$; R β move from $(x, y)$ to $(x + 1, y)$.
Vasya also has got a sequence of $n$ operations. Vasya wants to modify this sequence so after performing it the robot will end up in $(x, y)$.
Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: $maxID - minID + 1$, where $maxID$ is the maximum index of a changed operation, and $minID$ is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices $2$, $5$ and $7$ are changed, so the length of changed subsegment is $7 - 2 + 1 = 6$. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is $1$.
If there are no changes, then the length of changed subsegment is $0$. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.
Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from $(0, 0)$ to $(x, y)$, or tell him that it's impossible.
-----Input-----
The first line contains one integer number $n~(1 \le n \le 2 \cdot 10^5)$ β the number of operations.
The second line contains the sequence of operations β a string of $n$ characters. Each character is either U, D, L or R.
The third line contains two integers $x, y~(-10^9 \le x, y \le 10^9)$ β the coordinates of the cell where the robot should end its path.
-----Output-----
Print one integer β the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from $(0, 0)$ to $(x, y)$. If this change is impossible, print $-1$.
-----Examples-----
Input
5
RURUU
-2 3
Output
3
Input
4
RULR
1 1
Output
0
Input
3
UUU
100 100
Output
-1
-----Note-----
In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is $3 - 1 + 1 = 3$.
In the second example the given sequence already leads the robot to $(x, y)$, so the length of the changed subsegment is $0$.
In the third example the robot can't end his path in the cell $(x, y)$.
|
n = int(input())
dirs = input()
goal = list(map(int, input().split(" ")))
def can(start, end, steps):
dist = abs(start[0] - end[0]) + abs(start[1] - end[1])
return dist <= steps and (steps - dist) % 2 == 0
if not can((0, 0), goal, n):
print(-1)
return
diffs = {"U": (0, 1), "D": (0, -1), "L": (-1, 0), "R": (1, 0)}
pos = [(0, 0)] + [None] * n
for i, dir in enumerate(dirs):
old_pos = pos[i]
diff = diffs[dir]
pos[i + 1] = old_pos[0] + diff[0], old_pos[1] + diff[1]
final_pos = pos[n]
best = (abs(final_pos[0] - goal[0]) + abs(final_pos[1] - goal[1])) // 2
if best == 0:
print(0)
return
start = 0
end = best
current_best = float("inf")
while end <= n:
cur_pos = (
pos[start][0] + final_pos[0] - pos[end][0],
pos[start][1] + final_pos[1] - pos[end][1],
)
if can(cur_pos, goal, end - start):
current_best = min(current_best, end - start)
if current_best == best:
break
start += 1
else:
end += 1
print(current_best)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR DICT STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER BIN_OP LIST NONE VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell $(0, 0)$. Robot can perform the following four kinds of operations: U β move from $(x, y)$ to $(x, y + 1)$; D β move from $(x, y)$ to $(x, y - 1)$; L β move from $(x, y)$ to $(x - 1, y)$; R β move from $(x, y)$ to $(x + 1, y)$.
Vasya also has got a sequence of $n$ operations. Vasya wants to modify this sequence so after performing it the robot will end up in $(x, y)$.
Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: $maxID - minID + 1$, where $maxID$ is the maximum index of a changed operation, and $minID$ is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices $2$, $5$ and $7$ are changed, so the length of changed subsegment is $7 - 2 + 1 = 6$. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is $1$.
If there are no changes, then the length of changed subsegment is $0$. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.
Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from $(0, 0)$ to $(x, y)$, or tell him that it's impossible.
-----Input-----
The first line contains one integer number $n~(1 \le n \le 2 \cdot 10^5)$ β the number of operations.
The second line contains the sequence of operations β a string of $n$ characters. Each character is either U, D, L or R.
The third line contains two integers $x, y~(-10^9 \le x, y \le 10^9)$ β the coordinates of the cell where the robot should end its path.
-----Output-----
Print one integer β the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from $(0, 0)$ to $(x, y)$. If this change is impossible, print $-1$.
-----Examples-----
Input
5
RURUU
-2 3
Output
3
Input
4
RULR
1 1
Output
0
Input
3
UUU
100 100
Output
-1
-----Note-----
In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is $3 - 1 + 1 = 3$.
In the second example the given sequence already leads the robot to $(x, y)$, so the length of the changed subsegment is $0$.
In the third example the robot can't end his path in the cell $(x, y)$.
|
n = int(input())
a = input()
x, y = map(int, input().split())
if abs(x) + abs(y) > n or (x + y + n) % 2 != 0:
print(-1)
else:
dx = [0] * n
dy = [0] * n
for i in range(n):
if a[i] == "R":
dx[i] = 1
elif a[i] == "L":
dx[i] = -1
elif a[i] == "U":
dy[i] = 1
else:
dy[i] = -1
xx = sum(dx)
yy = sum(dy)
if xx == x and yy == y:
print(0)
else:
l = 0
res = n
for r in range(n):
xx -= dx[r]
yy -= dy[r]
while r - l + 1 >= abs(x - xx) + abs(y - yy):
xx += dx[l]
yy += dy[l]
l += 1
if l > 0:
res = min(res, r - l + 2)
print(res)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR WHILE BIN_OP BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell $(0, 0)$. Robot can perform the following four kinds of operations: U β move from $(x, y)$ to $(x, y + 1)$; D β move from $(x, y)$ to $(x, y - 1)$; L β move from $(x, y)$ to $(x - 1, y)$; R β move from $(x, y)$ to $(x + 1, y)$.
Vasya also has got a sequence of $n$ operations. Vasya wants to modify this sequence so after performing it the robot will end up in $(x, y)$.
Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: $maxID - minID + 1$, where $maxID$ is the maximum index of a changed operation, and $minID$ is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices $2$, $5$ and $7$ are changed, so the length of changed subsegment is $7 - 2 + 1 = 6$. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is $1$.
If there are no changes, then the length of changed subsegment is $0$. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.
Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from $(0, 0)$ to $(x, y)$, or tell him that it's impossible.
-----Input-----
The first line contains one integer number $n~(1 \le n \le 2 \cdot 10^5)$ β the number of operations.
The second line contains the sequence of operations β a string of $n$ characters. Each character is either U, D, L or R.
The third line contains two integers $x, y~(-10^9 \le x, y \le 10^9)$ β the coordinates of the cell where the robot should end its path.
-----Output-----
Print one integer β the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from $(0, 0)$ to $(x, y)$. If this change is impossible, print $-1$.
-----Examples-----
Input
5
RURUU
-2 3
Output
3
Input
4
RULR
1 1
Output
0
Input
3
UUU
100 100
Output
-1
-----Note-----
In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is $3 - 1 + 1 = 3$.
In the second example the given sequence already leads the robot to $(x, y)$, so the length of the changed subsegment is $0$.
In the third example the robot can't end his path in the cell $(x, y)$.
|
n = int(input().strip())
s = str(input().strip())
x, y = list(map(int, input().strip().split()))
sumx = []
sumy = []
sx = 0
sy = 0
sumx.append(sx)
sumy.append(sy)
for i in s:
if i == "U":
sy += 1
elif i == "D":
sy -= 1
elif i == "R":
sx += 1
elif i == "L":
sx -= 1
sumx.append(sx)
sumy.append(sy)
def check(mid):
i = 0
while i + mid <= n:
dx = sumx[i + mid] - sumx[i]
dy = sumy[i + mid] - sumy[i]
cx = sx - dx
cy = sy - dy
gdx = x - cx
gdy = y - cy
t = abs(gdx) + abs(gdy)
if t % 2 == mid % 2 and t <= mid:
return True
i += 1
return False
hi = n
lo = 0
mid = (hi + lo) // 2
while hi - lo > 1:
mid = (hi + lo) // 2
if check(mid):
hi = mid
else:
lo = mid
if check(lo):
print(lo)
elif check(mid):
print(mid)
elif check(hi):
print(hi)
else:
print(-1)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell $(0, 0)$. Robot can perform the following four kinds of operations: U β move from $(x, y)$ to $(x, y + 1)$; D β move from $(x, y)$ to $(x, y - 1)$; L β move from $(x, y)$ to $(x - 1, y)$; R β move from $(x, y)$ to $(x + 1, y)$.
Vasya also has got a sequence of $n$ operations. Vasya wants to modify this sequence so after performing it the robot will end up in $(x, y)$.
Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: $maxID - minID + 1$, where $maxID$ is the maximum index of a changed operation, and $minID$ is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices $2$, $5$ and $7$ are changed, so the length of changed subsegment is $7 - 2 + 1 = 6$. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is $1$.
If there are no changes, then the length of changed subsegment is $0$. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.
Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from $(0, 0)$ to $(x, y)$, or tell him that it's impossible.
-----Input-----
The first line contains one integer number $n~(1 \le n \le 2 \cdot 10^5)$ β the number of operations.
The second line contains the sequence of operations β a string of $n$ characters. Each character is either U, D, L or R.
The third line contains two integers $x, y~(-10^9 \le x, y \le 10^9)$ β the coordinates of the cell where the robot should end its path.
-----Output-----
Print one integer β the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from $(0, 0)$ to $(x, y)$. If this change is impossible, print $-1$.
-----Examples-----
Input
5
RURUU
-2 3
Output
3
Input
4
RULR
1 1
Output
0
Input
3
UUU
100 100
Output
-1
-----Note-----
In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is $3 - 1 + 1 = 3$.
In the second example the given sequence already leads the robot to $(x, y)$, so the length of the changed subsegment is $0$.
In the third example the robot can't end his path in the cell $(x, y)$.
|
class Vec(object):
def __init__(self, x, y):
self.x, self.y = x, y
def __add__(self, other):
return Vec(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return Vec(self.x - other.x, self.y - other.y)
def len(self):
return abs(self.x) + abs(self.y)
def __repr__(self):
return f"{self.x}, {self.y}"
md = {"U": Vec(0, 1), "D": Vec(0, -1), "L": Vec(-1, 0), "R": Vec(1, 0)}
n = int(input())
s = input()
u, v = map(int, input().split())
if Vec(u, v).len() > n or Vec(u, v).len() % 2 != n % 2:
print("-1")
exit()
pre = [Vec(0, 0) for _ in range(n + 2)]
suf = [Vec(0, 0) for _ in range(n + 2)]
for i in range(1, n + 1):
pre[i] = pre[i - 1] + md[s[i - 1]]
for i in range(n, 0, -1):
suf[i] = suf[i + 1] + md[s[i - 1]]
def check(p, q):
p += 1
q += 1
return (pre[p - 1] + suf[q] - Vec(u, v)).len() <= q - p
ans = n
l, r = 0, 0
while l < n:
while r < n and not check(l, r):
r += 1
if check(l, r):
ans = min(ans, r - l)
l += 1
print(ans)
|
CLASS_DEF VAR FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN VAR STRING VAR ASSIGN VAR DICT STRING STRING STRING STRING FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_DEF VAR NUMBER VAR NUMBER RETURN FUNC_CALL BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell $(0, 0)$. Robot can perform the following four kinds of operations: U β move from $(x, y)$ to $(x, y + 1)$; D β move from $(x, y)$ to $(x, y - 1)$; L β move from $(x, y)$ to $(x - 1, y)$; R β move from $(x, y)$ to $(x + 1, y)$.
Vasya also has got a sequence of $n$ operations. Vasya wants to modify this sequence so after performing it the robot will end up in $(x, y)$.
Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: $maxID - minID + 1$, where $maxID$ is the maximum index of a changed operation, and $minID$ is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices $2$, $5$ and $7$ are changed, so the length of changed subsegment is $7 - 2 + 1 = 6$. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is $1$.
If there are no changes, then the length of changed subsegment is $0$. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.
Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from $(0, 0)$ to $(x, y)$, or tell him that it's impossible.
-----Input-----
The first line contains one integer number $n~(1 \le n \le 2 \cdot 10^5)$ β the number of operations.
The second line contains the sequence of operations β a string of $n$ characters. Each character is either U, D, L or R.
The third line contains two integers $x, y~(-10^9 \le x, y \le 10^9)$ β the coordinates of the cell where the robot should end its path.
-----Output-----
Print one integer β the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from $(0, 0)$ to $(x, y)$. If this change is impossible, print $-1$.
-----Examples-----
Input
5
RURUU
-2 3
Output
3
Input
4
RULR
1 1
Output
0
Input
3
UUU
100 100
Output
-1
-----Note-----
In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is $3 - 1 + 1 = 3$.
In the second example the given sequence already leads the robot to $(x, y)$, so the length of the changed subsegment is $0$.
In the third example the robot can't end his path in the cell $(x, y)$.
|
d = {"U": (0, 1), "D": (0, -1), "L": (-1, 0), "R": (1, 0)}
def compute_delta(s, head_idx, tail_idx):
x = y = 0
for i in range(head_idx, tail_idx):
x, y = x + d[s[i]][0], y + d[s[i]][1]
return [x, y]
n = int(input())
s = input()
dsc = list(map(int, input().split()))
total = compute_delta(s, 0, n)
l, r = 0, n
current_sol = -1
while l <= r:
local_len = (r + l) // 2
initial_diff = compute_delta(s, 0, local_len)
local = [total[0] - initial_diff[0], total[1] - initial_diff[1]]
is_possible = False
diff = abs(dsc[0] - local[0]) + abs(dsc[1] - local[1])
if diff <= local_len and (diff + local_len) % 2 == 0:
is_possible = True
for i in range(local_len, n):
if is_possible:
break
d_old, d_new = d[s[i]], d[s[i - local_len]]
local = [local[0] - d_old[0] + d_new[0], local[1] - d_old[1] + d_new[1]]
diff = abs(dsc[0] - local[0]) + abs(dsc[1] - local[1])
if diff <= local_len and (diff + local_len) % 2 == 0:
is_possible = True
if is_possible:
current_sol = local_len
r = local_len - 1
else:
l = local_len + 1
print(current_sol)
|
ASSIGN VAR DICT STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER RETURN LIST VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR LIST BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR LIST BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell $(0, 0)$. Robot can perform the following four kinds of operations: U β move from $(x, y)$ to $(x, y + 1)$; D β move from $(x, y)$ to $(x, y - 1)$; L β move from $(x, y)$ to $(x - 1, y)$; R β move from $(x, y)$ to $(x + 1, y)$.
Vasya also has got a sequence of $n$ operations. Vasya wants to modify this sequence so after performing it the robot will end up in $(x, y)$.
Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: $maxID - minID + 1$, where $maxID$ is the maximum index of a changed operation, and $minID$ is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices $2$, $5$ and $7$ are changed, so the length of changed subsegment is $7 - 2 + 1 = 6$. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is $1$.
If there are no changes, then the length of changed subsegment is $0$. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.
Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from $(0, 0)$ to $(x, y)$, or tell him that it's impossible.
-----Input-----
The first line contains one integer number $n~(1 \le n \le 2 \cdot 10^5)$ β the number of operations.
The second line contains the sequence of operations β a string of $n$ characters. Each character is either U, D, L or R.
The third line contains two integers $x, y~(-10^9 \le x, y \le 10^9)$ β the coordinates of the cell where the robot should end its path.
-----Output-----
Print one integer β the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from $(0, 0)$ to $(x, y)$. If this change is impossible, print $-1$.
-----Examples-----
Input
5
RURUU
-2 3
Output
3
Input
4
RULR
1 1
Output
0
Input
3
UUU
100 100
Output
-1
-----Note-----
In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is $3 - 1 + 1 = 3$.
In the second example the given sequence already leads the robot to $(x, y)$, so the length of the changed subsegment is $0$.
In the third example the robot can't end his path in the cell $(x, y)$.
|
import sys
n = int(input())
S = input()
x, y = map(int, input().split())
if abs(x) + abs(y) > n or (abs(x) + abs(y)) % 2 != n % 2:
print(-1)
sys.exit()
now = [0, 0]
LISTL = [(0, 0)]
for s in S:
if s == "R":
now[0] += 1
elif s == "L":
now[0] -= 1
elif s == "U":
now[1] += 1
else:
now[1] -= 1
LISTL.append((now[0], now[1]))
LISTR = [(0, 0)]
now = [0, 0]
for s in S[::-1]:
if s == "R":
now[0] += 1
elif s == "L":
now[0] -= 1
elif s == "U":
now[1] += 1
else:
now[1] -= 1
LISTR.append((now[0], now[1]))
def su(a, b, x, y):
return abs(x - (a[0] + b[0])) + abs(y - (a[1] + b[1]))
ANS = 0
for i in range(n + 1):
for j in range(max(0, ANS - i), n + 1):
if su(LISTR[i], LISTL[j], x, y) <= n - i - j:
if ANS < i + j:
ANS = i + j
else:
break
print(n - ANS)
|
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER NUMBER IF VAR STRING VAR NUMBER NUMBER IF VAR STRING VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR VAR NUMBER IF VAR STRING VAR NUMBER NUMBER IF VAR STRING VAR NUMBER NUMBER IF VAR STRING VAR NUMBER NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER FUNC_DEF RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
|
Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell $(0, 0)$. Robot can perform the following four kinds of operations: U β move from $(x, y)$ to $(x, y + 1)$; D β move from $(x, y)$ to $(x, y - 1)$; L β move from $(x, y)$ to $(x - 1, y)$; R β move from $(x, y)$ to $(x + 1, y)$.
Vasya also has got a sequence of $n$ operations. Vasya wants to modify this sequence so after performing it the robot will end up in $(x, y)$.
Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: $maxID - minID + 1$, where $maxID$ is the maximum index of a changed operation, and $minID$ is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices $2$, $5$ and $7$ are changed, so the length of changed subsegment is $7 - 2 + 1 = 6$. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is $1$.
If there are no changes, then the length of changed subsegment is $0$. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.
Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from $(0, 0)$ to $(x, y)$, or tell him that it's impossible.
-----Input-----
The first line contains one integer number $n~(1 \le n \le 2 \cdot 10^5)$ β the number of operations.
The second line contains the sequence of operations β a string of $n$ characters. Each character is either U, D, L or R.
The third line contains two integers $x, y~(-10^9 \le x, y \le 10^9)$ β the coordinates of the cell where the robot should end its path.
-----Output-----
Print one integer β the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from $(0, 0)$ to $(x, y)$. If this change is impossible, print $-1$.
-----Examples-----
Input
5
RURUU
-2 3
Output
3
Input
4
RULR
1 1
Output
0
Input
3
UUU
100 100
Output
-1
-----Note-----
In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is $3 - 1 + 1 = 3$.
In the second example the given sequence already leads the robot to $(x, y)$, so the length of the changed subsegment is $0$.
In the third example the robot can't end his path in the cell $(x, y)$.
|
def check(length: int):
global n, x, y, cur, hor, ver
hor = 0
ver = 0
for i in range(length, n):
upd(i)
for i in range(n - length + 1):
if abs(x - hor) + abs(y - ver) <= length:
return True
if i + length < n:
upd(i)
minus_upd(i + length)
return False
def upd(pos: int):
global s, ver, hor
if s[pos] == "U":
ver += 1
elif s[pos] == "D":
ver -= 1
elif s[pos] == "R":
hor += 1
else:
hor -= 1
def minus_upd(pos: int):
global s, ver, hor
if s[pos] == "U":
ver -= 1
elif s[pos] == "D":
ver += 1
elif s[pos] == "R":
hor -= 1
else:
hor += 1
n = int(input())
s = list(input())
x, y = map(int, input().split())
if (x + y) % 2 != n % 2 or abs(x) + abs(y) > n:
print(-1)
exit()
ver = 0
hor = 0
cur = 0
for i in range(n):
upd(i)
left = -1
r = n
while r - left > 1:
length = (r + left) // 2
if check(length):
r = length
else:
left = length
print(r)
|
FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN NUMBER FUNC_DEF VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER FUNC_DEF VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell $(0, 0)$. Robot can perform the following four kinds of operations: U β move from $(x, y)$ to $(x, y + 1)$; D β move from $(x, y)$ to $(x, y - 1)$; L β move from $(x, y)$ to $(x - 1, y)$; R β move from $(x, y)$ to $(x + 1, y)$.
Vasya also has got a sequence of $n$ operations. Vasya wants to modify this sequence so after performing it the robot will end up in $(x, y)$.
Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: $maxID - minID + 1$, where $maxID$ is the maximum index of a changed operation, and $minID$ is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices $2$, $5$ and $7$ are changed, so the length of changed subsegment is $7 - 2 + 1 = 6$. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is $1$.
If there are no changes, then the length of changed subsegment is $0$. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.
Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from $(0, 0)$ to $(x, y)$, or tell him that it's impossible.
-----Input-----
The first line contains one integer number $n~(1 \le n \le 2 \cdot 10^5)$ β the number of operations.
The second line contains the sequence of operations β a string of $n$ characters. Each character is either U, D, L or R.
The third line contains two integers $x, y~(-10^9 \le x, y \le 10^9)$ β the coordinates of the cell where the robot should end its path.
-----Output-----
Print one integer β the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from $(0, 0)$ to $(x, y)$. If this change is impossible, print $-1$.
-----Examples-----
Input
5
RURUU
-2 3
Output
3
Input
4
RULR
1 1
Output
0
Input
3
UUU
100 100
Output
-1
-----Note-----
In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is $3 - 1 + 1 = 3$.
In the second example the given sequence already leads the robot to $(x, y)$, so the length of the changed subsegment is $0$.
In the third example the robot can't end his path in the cell $(x, y)$.
|
n = int(input())
dx = [(0) for i in range(n + 1)]
dy = [(0) for i in range(n + 1)]
for i, ch in enumerate(input()):
dx[i + 1] = dx[i]
dy[i + 1] = dy[i]
if ch == "U":
dy[i + 1] += 1
elif ch == "D":
dy[i + 1] -= 1
elif ch == "R":
dx[i + 1] += 1
else:
assert ch == "L"
dx[i + 1] -= 1
x, y = map(int, input().split())
def canChange(left, right):
dx1 = dx[left]
dy1 = dy[left]
dx3 = dx[-1] - dx[right]
dy3 = dy[-1] - dy[right]
dx2 = x - dx1 - dx3
dy2 = y - dy1 - dy3
length = right - left
free = length - (abs(dx2) + abs(dy2))
return free >= 0 and free % 2 == 0
result = n + 1
ptr = n + 1
for i in reversed(range(n)):
while ptr - 1 >= i and canChange(i, ptr - 1):
ptr -= 1
result = min(result, ptr - i)
if ptr == n + 1:
print(-1)
else:
print(result)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR IF VAR STRING VAR BIN_OP VAR NUMBER NUMBER IF VAR STRING VAR BIN_OP VAR NUMBER NUMBER IF VAR STRING VAR BIN_OP VAR NUMBER NUMBER VAR STRING VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell $(0, 0)$. Robot can perform the following four kinds of operations: U β move from $(x, y)$ to $(x, y + 1)$; D β move from $(x, y)$ to $(x, y - 1)$; L β move from $(x, y)$ to $(x - 1, y)$; R β move from $(x, y)$ to $(x + 1, y)$.
Vasya also has got a sequence of $n$ operations. Vasya wants to modify this sequence so after performing it the robot will end up in $(x, y)$.
Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: $maxID - minID + 1$, where $maxID$ is the maximum index of a changed operation, and $minID$ is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices $2$, $5$ and $7$ are changed, so the length of changed subsegment is $7 - 2 + 1 = 6$. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is $1$.
If there are no changes, then the length of changed subsegment is $0$. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.
Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from $(0, 0)$ to $(x, y)$, or tell him that it's impossible.
-----Input-----
The first line contains one integer number $n~(1 \le n \le 2 \cdot 10^5)$ β the number of operations.
The second line contains the sequence of operations β a string of $n$ characters. Each character is either U, D, L or R.
The third line contains two integers $x, y~(-10^9 \le x, y \le 10^9)$ β the coordinates of the cell where the robot should end its path.
-----Output-----
Print one integer β the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from $(0, 0)$ to $(x, y)$. If this change is impossible, print $-1$.
-----Examples-----
Input
5
RURUU
-2 3
Output
3
Input
4
RULR
1 1
Output
0
Input
3
UUU
100 100
Output
-1
-----Note-----
In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is $3 - 1 + 1 = 3$.
In the second example the given sequence already leads the robot to $(x, y)$, so the length of the changed subsegment is $0$.
In the third example the robot can't end his path in the cell $(x, y)$.
|
n = int(input())
s = input()
p, q = input().split()
if p[0] == "-":
x = -1 * int(p[1:])
else:
x = int(p)
if q[0] == "-":
y = -1 * int(q[1:])
else:
y = int(q)
cur = [0, 0]
if abs(x) + abs(y) > n:
print(-1)
elif (x + y) % 2 != n % 2:
print(-1)
else:
end = n
for i in range(n):
if s[i] == "R":
cur[0] += 1
if s[i] == "L":
cur[0] -= 1
if s[i] == "U":
cur[1] += 1
if s[i] == "D":
cur[1] -= 1
if abs(x - cur[0]) + abs(y - cur[1]) >= n - i:
end = i
break
if end == n:
print(0)
else:
m = [0] * (end + 1)
start = n
for i in range(end, -1, -1):
if s[i] == "R":
cur[0] -= 1
if s[i] == "L":
cur[0] += 1
if s[i] == "U":
cur[1] -= 1
if s[i] == "D":
cur[1] += 1
while abs(x - cur[0]) + abs(y - cur[1]) <= start - i:
start -= 1
if s[start] == "R":
x -= 1
if s[start] == "L":
x += 1
if s[start] == "U":
y -= 1
if s[start] == "D":
y += 1
m[i] = start - i + 1
minn = n
for i in m:
minn = min(minn, i)
print(minn)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER NUMBER IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER NUMBER WHILE BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell $(0, 0)$. Robot can perform the following four kinds of operations: U β move from $(x, y)$ to $(x, y + 1)$; D β move from $(x, y)$ to $(x, y - 1)$; L β move from $(x, y)$ to $(x - 1, y)$; R β move from $(x, y)$ to $(x + 1, y)$.
Vasya also has got a sequence of $n$ operations. Vasya wants to modify this sequence so after performing it the robot will end up in $(x, y)$.
Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: $maxID - minID + 1$, where $maxID$ is the maximum index of a changed operation, and $minID$ is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices $2$, $5$ and $7$ are changed, so the length of changed subsegment is $7 - 2 + 1 = 6$. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is $1$.
If there are no changes, then the length of changed subsegment is $0$. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.
Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from $(0, 0)$ to $(x, y)$, or tell him that it's impossible.
-----Input-----
The first line contains one integer number $n~(1 \le n \le 2 \cdot 10^5)$ β the number of operations.
The second line contains the sequence of operations β a string of $n$ characters. Each character is either U, D, L or R.
The third line contains two integers $x, y~(-10^9 \le x, y \le 10^9)$ β the coordinates of the cell where the robot should end its path.
-----Output-----
Print one integer β the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from $(0, 0)$ to $(x, y)$. If this change is impossible, print $-1$.
-----Examples-----
Input
5
RURUU
-2 3
Output
3
Input
4
RULR
1 1
Output
0
Input
3
UUU
100 100
Output
-1
-----Note-----
In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is $3 - 1 + 1 = 3$.
In the second example the given sequence already leads the robot to $(x, y)$, so the length of the changed subsegment is $0$.
In the third example the robot can't end his path in the cell $(x, y)$.
|
def is_check(x):
i = 1
j = x
h, v = 0, 0
while j <= n:
h = sum_x[n] - sum_x[j] + (sum_x[i - 1] - sum_x[0])
v = sum_y[n] - sum_y[j] + (sum_y[i - 1] - sum_y[0])
i += 1
j += 1
if abs(x_f - h) + abs(y_f - v) <= x:
return 1
return 0
n = int(input())
st = input()
x_f, y_f = map(int, input().split())
sum_x = [0]
sum_y = [0]
add_x, add_y = 0, 0
for i in range(n):
if st[i] == "R":
add_x += 1
elif st[i] == "L":
add_x -= 1
elif st[i] == "U":
add_y += 1
else:
add_y -= 1
sum_x.append(add_x)
sum_y.append(add_y)
i = 0
j = n
while i < j:
m = (i + j) // 2
if is_check(m):
j = m - 1
else:
i = m + 1
if abs(x_f) + abs(y_f) > n or (abs(x_f) + abs(y_f) + n) % 2 == 1:
print(-1)
elif is_check(i):
print(i)
else:
print(i + 1)
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell $(0, 0)$. Robot can perform the following four kinds of operations: U β move from $(x, y)$ to $(x, y + 1)$; D β move from $(x, y)$ to $(x, y - 1)$; L β move from $(x, y)$ to $(x - 1, y)$; R β move from $(x, y)$ to $(x + 1, y)$.
Vasya also has got a sequence of $n$ operations. Vasya wants to modify this sequence so after performing it the robot will end up in $(x, y)$.
Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: $maxID - minID + 1$, where $maxID$ is the maximum index of a changed operation, and $minID$ is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices $2$, $5$ and $7$ are changed, so the length of changed subsegment is $7 - 2 + 1 = 6$. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is $1$.
If there are no changes, then the length of changed subsegment is $0$. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.
Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from $(0, 0)$ to $(x, y)$, or tell him that it's impossible.
-----Input-----
The first line contains one integer number $n~(1 \le n \le 2 \cdot 10^5)$ β the number of operations.
The second line contains the sequence of operations β a string of $n$ characters. Each character is either U, D, L or R.
The third line contains two integers $x, y~(-10^9 \le x, y \le 10^9)$ β the coordinates of the cell where the robot should end its path.
-----Output-----
Print one integer β the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from $(0, 0)$ to $(x, y)$. If this change is impossible, print $-1$.
-----Examples-----
Input
5
RURUU
-2 3
Output
3
Input
4
RULR
1 1
Output
0
Input
3
UUU
100 100
Output
-1
-----Note-----
In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is $3 - 1 + 1 = 3$.
In the second example the given sequence already leads the robot to $(x, y)$, so the length of the changed subsegment is $0$.
In the third example the robot can't end his path in the cell $(x, y)$.
|
n = int(input())
s = list(input())
a, b = list(map(int, input().split()))
L = s.count("L")
U = s.count("U")
R = s.count("R")
D = s.count("D")
x = 0
y = 0
xmin = 0
ymin = 0
minn = 2 * n
while x + y < 2 * n:
if abs(a - (R - L)) + abs(b - (U - D)) > y - x and y != n:
i = s[y]
if i == "L":
L -= 1
elif i == "R":
R -= 1
elif i == "D":
D -= 1
elif i == "U":
U -= 1
y += 1
elif abs(a - (R - L)) + abs(b - (U - D)) <= y - x or y == n:
if y - x < minn and abs(a - (R - L)) + abs(b - (U - D)) <= y - x:
minn = y - x
i = s[x]
if i == "L":
L += 1
elif i == "R":
R += 1
elif i == "D":
D += 1
elif i == "U":
U += 1
x += 1
if abs(a) + abs(b) > len(s):
print(-1)
elif (len(s) - (abs(a) + abs(b))) % 2 != 0:
print(-1)
else:
print(minn)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR WHILE BIN_OP VAR VAR BIN_OP NUMBER VAR IF BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell $(0, 0)$. Robot can perform the following four kinds of operations: U β move from $(x, y)$ to $(x, y + 1)$; D β move from $(x, y)$ to $(x, y - 1)$; L β move from $(x, y)$ to $(x - 1, y)$; R β move from $(x, y)$ to $(x + 1, y)$.
Vasya also has got a sequence of $n$ operations. Vasya wants to modify this sequence so after performing it the robot will end up in $(x, y)$.
Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: $maxID - minID + 1$, where $maxID$ is the maximum index of a changed operation, and $minID$ is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices $2$, $5$ and $7$ are changed, so the length of changed subsegment is $7 - 2 + 1 = 6$. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is $1$.
If there are no changes, then the length of changed subsegment is $0$. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.
Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from $(0, 0)$ to $(x, y)$, or tell him that it's impossible.
-----Input-----
The first line contains one integer number $n~(1 \le n \le 2 \cdot 10^5)$ β the number of operations.
The second line contains the sequence of operations β a string of $n$ characters. Each character is either U, D, L or R.
The third line contains two integers $x, y~(-10^9 \le x, y \le 10^9)$ β the coordinates of the cell where the robot should end its path.
-----Output-----
Print one integer β the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from $(0, 0)$ to $(x, y)$. If this change is impossible, print $-1$.
-----Examples-----
Input
5
RURUU
-2 3
Output
3
Input
4
RULR
1 1
Output
0
Input
3
UUU
100 100
Output
-1
-----Note-----
In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is $3 - 1 + 1 = 3$.
In the second example the given sequence already leads the robot to $(x, y)$, so the length of the changed subsegment is $0$.
In the third example the robot can't end his path in the cell $(x, y)$.
|
def go(pos, k):
if "U" == k:
pos[1] += 1
elif "D" == k:
pos[1] -= 1
elif "L" == k:
pos[0] -= 1
else:
pos[0] += 1
return pos
def relapos(a, b):
c = [b[0] - a[0], b[1] - a[1]]
return c
def stdis(a, b):
return abs(b[1] - a[1]) + abs(b[0] - a[0])
n = int(input())
s = input()
x, y = [int(i) for i in input().split()]
poss = [[0, 0]]
pos = [0, 0]
for i in range(0, n):
poss.append(tuple(go(pos, s[i])))
if abs(x) + abs(y) > n or abs(n - x - y) % 2 != 0:
print(-1)
else:
lpos = poss[-1]
dd = stdis(lpos, [x, y])
if dd == 0:
print(0)
else:
q1 = (dd - 1) // 2
q2 = n
j = True
lpos1 = [0, 0]
while q2 - q1 != 1:
q = (q2 + q1) // 2
for k1 in range(n - q + 1):
r = relapos(poss[k1], poss[k1 + q])
lpos1[0] = lpos[0] - r[0]
lpos1[1] = lpos[1] - r[1]
if stdis(lpos1, [x, y]) <= q:
q2 = q
break
else:
q1 = q
print(q2)
|
FUNC_DEF IF STRING VAR VAR NUMBER NUMBER IF STRING VAR VAR NUMBER NUMBER IF STRING VAR VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR LIST VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR LIST VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell $(0, 0)$. Robot can perform the following four kinds of operations: U β move from $(x, y)$ to $(x, y + 1)$; D β move from $(x, y)$ to $(x, y - 1)$; L β move from $(x, y)$ to $(x - 1, y)$; R β move from $(x, y)$ to $(x + 1, y)$.
Vasya also has got a sequence of $n$ operations. Vasya wants to modify this sequence so after performing it the robot will end up in $(x, y)$.
Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: $maxID - minID + 1$, where $maxID$ is the maximum index of a changed operation, and $minID$ is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices $2$, $5$ and $7$ are changed, so the length of changed subsegment is $7 - 2 + 1 = 6$. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is $1$.
If there are no changes, then the length of changed subsegment is $0$. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.
Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from $(0, 0)$ to $(x, y)$, or tell him that it's impossible.
-----Input-----
The first line contains one integer number $n~(1 \le n \le 2 \cdot 10^5)$ β the number of operations.
The second line contains the sequence of operations β a string of $n$ characters. Each character is either U, D, L or R.
The third line contains two integers $x, y~(-10^9 \le x, y \le 10^9)$ β the coordinates of the cell where the robot should end its path.
-----Output-----
Print one integer β the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from $(0, 0)$ to $(x, y)$. If this change is impossible, print $-1$.
-----Examples-----
Input
5
RURUU
-2 3
Output
3
Input
4
RULR
1 1
Output
0
Input
3
UUU
100 100
Output
-1
-----Note-----
In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is $3 - 1 + 1 = 3$.
In the second example the given sequence already leads the robot to $(x, y)$, so the length of the changed subsegment is $0$.
In the third example the robot can't end his path in the cell $(x, y)$.
|
n = int(input())
root = input()
x, y = list(map(int, input().split()))
curx, cury = 0, 0
dx, dy = [0], [0]
for i in range(n):
if root[i] == "L":
curx -= 1
if root[i] == "R":
curx += 1
if root[i] == "U":
cury += 1
if root[i] == "D":
cury -= 1
dx.append(curx)
dy.append(cury)
if curx == x and cury == y:
print(0)
else:
ans = n + 1
for i in range(1, n + 1):
l, r = i, n
while l <= r:
mid = (l + r) // 2
dx1 = dx[i - 1]
dy1 = dy[i - 1]
dx2 = dx[n] - dx[mid]
dy2 = dy[n] - dy[mid]
ddx = x - (dx1 + dx2)
ddy = y - (dy1 + dy2)
z = mid - i + 1 - abs(ddx) - abs(ddy)
if z >= 0 and z % 2 == 0:
ans = min(ans, mid - i + 1)
r = mid - 1
else:
l = mid + 1
if ans == n + 1:
print(-1)
else:
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR LIST NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell $(0, 0)$. Robot can perform the following four kinds of operations: U β move from $(x, y)$ to $(x, y + 1)$; D β move from $(x, y)$ to $(x, y - 1)$; L β move from $(x, y)$ to $(x - 1, y)$; R β move from $(x, y)$ to $(x + 1, y)$.
Vasya also has got a sequence of $n$ operations. Vasya wants to modify this sequence so after performing it the robot will end up in $(x, y)$.
Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: $maxID - minID + 1$, where $maxID$ is the maximum index of a changed operation, and $minID$ is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices $2$, $5$ and $7$ are changed, so the length of changed subsegment is $7 - 2 + 1 = 6$. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is $1$.
If there are no changes, then the length of changed subsegment is $0$. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.
Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from $(0, 0)$ to $(x, y)$, or tell him that it's impossible.
-----Input-----
The first line contains one integer number $n~(1 \le n \le 2 \cdot 10^5)$ β the number of operations.
The second line contains the sequence of operations β a string of $n$ characters. Each character is either U, D, L or R.
The third line contains two integers $x, y~(-10^9 \le x, y \le 10^9)$ β the coordinates of the cell where the robot should end its path.
-----Output-----
Print one integer β the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from $(0, 0)$ to $(x, y)$. If this change is impossible, print $-1$.
-----Examples-----
Input
5
RURUU
-2 3
Output
3
Input
4
RULR
1 1
Output
0
Input
3
UUU
100 100
Output
-1
-----Note-----
In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is $3 - 1 + 1 = 3$.
In the second example the given sequence already leads the robot to $(x, y)$, so the length of the changed subsegment is $0$.
In the third example the robot can't end his path in the cell $(x, y)$.
|
N = int(input())
ops = [x for x in input()]
X, Y = map(int, input().split())
dd = abs(X) + abs(Y)
lops = len(ops)
if dd > lops or (lops - dd) % 2 != 0:
print(-1)
exit(0)
[ll, lr, lu, ld, rl, rr, ru, rd] = [[(0) for _ in range(lops + 2)] for _ in range(8)]
l, r, u, d = 0, 0, 0, 0
for i in range(lops):
op = ops[i]
if op == "L":
l += 1
elif op == "R":
r += 1
elif op == "U":
u += 1
else:
d += 1
ll[i + 1] = l
lr[i + 1] = r
ld[i + 1] = d
lu[i + 1] = u
l, r, u, d = 0, 0, 0, 0
for i in range(lops - 1, -1, -1):
op = ops[i]
if op == "L":
l += 1
elif op == "R":
r += 1
elif op == "U":
u += 1
else:
d += 1
rl[i] = l
rr[i] = r
rd[i] = d
ru[i] = u
def check(lsub):
for i in range(lops - lsub + 1):
j = i + lsub - 1
l0, r0, u0, d0 = ll[i], lr[i], lu[i], ld[i]
l1, r1, u1, d1 = rl[j + 1], rr[j + 1], ru[j + 1], rd[j + 1]
x = r0 + r1 - (l0 + l1)
y = u0 + u1 - (d0 + d1)
dx = abs(X - x)
dy = abs(Y - y)
if dx + dy <= lsub and (lsub - dx - dy) % 2 == 0:
return True
return False
sl, sr = 0, lops + 1
while sl < sr:
m = (sl + sr) // 2
if check(m):
sr = m
else:
sl = m + 1
sl = -1 if sl > lops else sl
print(sl)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN LIST VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER IF VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
|
Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell $(0, 0)$. Robot can perform the following four kinds of operations: U β move from $(x, y)$ to $(x, y + 1)$; D β move from $(x, y)$ to $(x, y - 1)$; L β move from $(x, y)$ to $(x - 1, y)$; R β move from $(x, y)$ to $(x + 1, y)$.
Vasya also has got a sequence of $n$ operations. Vasya wants to modify this sequence so after performing it the robot will end up in $(x, y)$.
Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: $maxID - minID + 1$, where $maxID$ is the maximum index of a changed operation, and $minID$ is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices $2$, $5$ and $7$ are changed, so the length of changed subsegment is $7 - 2 + 1 = 6$. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is $1$.
If there are no changes, then the length of changed subsegment is $0$. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.
Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from $(0, 0)$ to $(x, y)$, or tell him that it's impossible.
-----Input-----
The first line contains one integer number $n~(1 \le n \le 2 \cdot 10^5)$ β the number of operations.
The second line contains the sequence of operations β a string of $n$ characters. Each character is either U, D, L or R.
The third line contains two integers $x, y~(-10^9 \le x, y \le 10^9)$ β the coordinates of the cell where the robot should end its path.
-----Output-----
Print one integer β the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from $(0, 0)$ to $(x, y)$. If this change is impossible, print $-1$.
-----Examples-----
Input
5
RURUU
-2 3
Output
3
Input
4
RULR
1 1
Output
0
Input
3
UUU
100 100
Output
-1
-----Note-----
In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is $3 - 1 + 1 = 3$.
In the second example the given sequence already leads the robot to $(x, y)$, so the length of the changed subsegment is $0$.
In the third example the robot can't end his path in the cell $(x, y)$.
|
n = int(input())
subtract = lambda t1, t2: (t1[0] - t2[0], t1[1] - t2[1])
add = lambda t1, t2: (t1[0] + t2[0], t1[1] + t2[1])
def conv(ch):
if ch == "L":
return -1, 0
elif ch == "R":
return 1, 0
elif ch == "U":
return 0, 1
elif ch == "D":
return 0, -1
ops = [conv(ch) for ch in input()]
x, y = list(map(int, input().split()))
lsum = [ops[0]] * n
for i in range(1, n):
lsum[i] = add(lsum[i - 1], ops[i])
rsum = [ops[n - 1]] * n
i = n - 2
while i >= 0:
rsum[i] = add(rsum[i + 1], ops[i])
i = i - 1
def check(L):
for i in range(0, n - L + 1):
moves = x, y
if i > 0:
moves = subtract(moves, lsum[i - 1])
if i + L < n:
moves = subtract(moves, rsum[i + L])
turns = abs(moves[0]) + abs(moves[1])
if turns <= L and (L - turns) % 2 == 0:
return True
return False
if abs(x) + abs(y) > n or (abs(x) + abs(y) - n) % 2 != 0:
print(-1)
else:
st, en = 0, n
while st < en:
md = (st + en) // 2
if check(md):
en = md
else:
st = md + 1
print(st)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF IF VAR STRING RETURN NUMBER NUMBER IF VAR STRING RETURN NUMBER NUMBER IF VAR STRING RETURN NUMBER NUMBER IF VAR STRING RETURN NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell $(0, 0)$. Robot can perform the following four kinds of operations: U β move from $(x, y)$ to $(x, y + 1)$; D β move from $(x, y)$ to $(x, y - 1)$; L β move from $(x, y)$ to $(x - 1, y)$; R β move from $(x, y)$ to $(x + 1, y)$.
Vasya also has got a sequence of $n$ operations. Vasya wants to modify this sequence so after performing it the robot will end up in $(x, y)$.
Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: $maxID - minID + 1$, where $maxID$ is the maximum index of a changed operation, and $minID$ is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices $2$, $5$ and $7$ are changed, so the length of changed subsegment is $7 - 2 + 1 = 6$. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is $1$.
If there are no changes, then the length of changed subsegment is $0$. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.
Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from $(0, 0)$ to $(x, y)$, or tell him that it's impossible.
-----Input-----
The first line contains one integer number $n~(1 \le n \le 2 \cdot 10^5)$ β the number of operations.
The second line contains the sequence of operations β a string of $n$ characters. Each character is either U, D, L or R.
The third line contains two integers $x, y~(-10^9 \le x, y \le 10^9)$ β the coordinates of the cell where the robot should end its path.
-----Output-----
Print one integer β the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from $(0, 0)$ to $(x, y)$. If this change is impossible, print $-1$.
-----Examples-----
Input
5
RURUU
-2 3
Output
3
Input
4
RULR
1 1
Output
0
Input
3
UUU
100 100
Output
-1
-----Note-----
In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is $3 - 1 + 1 = 3$.
In the second example the given sequence already leads the robot to $(x, y)$, so the length of the changed subsegment is $0$.
In the third example the robot can't end his path in the cell $(x, y)$.
|
import sys
input = sys.stdin.readline
n = int(input())
s = [i for i in input() if i != "\n"]
x, y = map(int, input().split())
if abs(x) + abs(y) > n or (abs(x) + abs(y)) % 2 != n % 2:
print(-1)
else:
dx, dy = [], []
for i in range(n):
if s[i] == "R":
dx.append(1)
dy.append(0)
elif s[i] == "L":
dx.append(-1)
dy.append(0)
elif s[i] == "U":
dy.append(1)
dx.append(0)
else:
dy.append(-1)
dx.append(0)
xx, yy = sum(dx), sum(dy)
if xx == x and yy == y:
print(0)
else:
l, res = n - 1, n
for r in range(n - 1, -1, -1):
xx -= dx[r]
yy -= dy[r]
while l - r + 1 >= abs(x - xx) + abs(y - yy):
xx += dx[l]
yy += dy[l]
l -= 1
if l < n - 1:
res = min(res, l - r + 2)
print(res)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR VAR VAR WHILE BIN_OP BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell $(0, 0)$. Robot can perform the following four kinds of operations: U β move from $(x, y)$ to $(x, y + 1)$; D β move from $(x, y)$ to $(x, y - 1)$; L β move from $(x, y)$ to $(x - 1, y)$; R β move from $(x, y)$ to $(x + 1, y)$.
Vasya also has got a sequence of $n$ operations. Vasya wants to modify this sequence so after performing it the robot will end up in $(x, y)$.
Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: $maxID - minID + 1$, where $maxID$ is the maximum index of a changed operation, and $minID$ is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices $2$, $5$ and $7$ are changed, so the length of changed subsegment is $7 - 2 + 1 = 6$. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is $1$.
If there are no changes, then the length of changed subsegment is $0$. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them.
Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from $(0, 0)$ to $(x, y)$, or tell him that it's impossible.
-----Input-----
The first line contains one integer number $n~(1 \le n \le 2 \cdot 10^5)$ β the number of operations.
The second line contains the sequence of operations β a string of $n$ characters. Each character is either U, D, L or R.
The third line contains two integers $x, y~(-10^9 \le x, y \le 10^9)$ β the coordinates of the cell where the robot should end its path.
-----Output-----
Print one integer β the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from $(0, 0)$ to $(x, y)$. If this change is impossible, print $-1$.
-----Examples-----
Input
5
RURUU
-2 3
Output
3
Input
4
RULR
1 1
Output
0
Input
3
UUU
100 100
Output
-1
-----Note-----
In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is $3 - 1 + 1 = 3$.
In the second example the given sequence already leads the robot to $(x, y)$, so the length of the changed subsegment is $0$.
In the third example the robot can't end his path in the cell $(x, y)$.
|
def check(x, y, fx, fy, num_moves):
if (
abs(fx - x) + abs(fy - y) - num_moves <= 0
and (abs(fx - x) + abs(fy - y) - num_moves) % 2 == 0
):
return True
return False
N = int(input())
mm = {"U": 0, "D": 1, "L": 2, "R": 3}
dpmat = [[0] for i in range(4)]
ops = str(input())
for op in ops:
dpmat[0].append(dpmat[0][-1])
dpmat[1].append(dpmat[1][-1])
dpmat[2].append(dpmat[2][-1])
dpmat[3].append(dpmat[3][-1])
dpmat[mm[op]][-1] = dpmat[mm[op]][-1] + 1
fpos = list(map(int, input().split()))
if N < fpos[0] + fpos[1]:
print("-1")
else:
x, y = 0, 0
ans = 100000000000.0
while y <= N and x <= y:
if y == 0 and x == 0:
num_moves = 0
xr = dpmat[3][N]
xl = dpmat[2][N]
yu = dpmat[0][N]
yd = dpmat[1][N]
else:
num_moves = y - x + 1
xr = dpmat[3][x - 1] + dpmat[3][N] - dpmat[3][y]
xl = dpmat[2][x - 1] + dpmat[2][N] - dpmat[2][y]
yu = dpmat[0][x - 1] + dpmat[0][N] - dpmat[0][y]
yd = dpmat[1][x - 1] + dpmat[1][N] - dpmat[1][y]
if check(xr - xl, yu - yd, fpos[0], fpos[1], num_moves) == True:
x += 1
ans = min(ans, num_moves)
else:
if x == 0:
x += 1
y += 1
if ans == 100000000000.0:
print("-1")
else:
print(max(0, ans))
|
FUNC_DEF IF BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER BIN_OP BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING STRING STRING NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.