description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
for _ in range(int(input())):
n, x = map(int, input().split())
l1 = list(map(int, input().split()))
l2 = l1[:]
l2.sort()
if l1 == l2:
print(0)
else:
co = 0
for i in range(n):
f1 = 0
if l1[i] <= x:
pass
else:
l1[i], x = x, l1[i]
co += 1
for j in range(i + 1, n - 1, 1):
if l1[j] > l1[j + 1]:
f1 = 1
if f1 == 0:
break
l3 = l1[:]
l3.sort()
if l3 == l1:
print(co)
else:
print(-1)
|
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 EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
t = int(input())
def solve():
n, x = map(int, input().split())
a = list(map(int, input().split()))
xwas = x
sor = list(sorted(a))
g = 1
for i in range(n):
g = g and sor[i] == a[i]
if g:
return 0
sor = list(sorted(a + [x]))
k = 0
b = 1
for i in range(n):
if sor[i] != a[i]:
k += 1
if a[i] <= x:
return -1
else:
a[i], x = x, a[i]
sor2 = list(sorted(a[i:]))
if sor2 == a[i:]:
return k
return k
for _ in range(t):
print(solve())
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR RETURN VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
def check_sort(list1):
ans = 1
for i in range(n - 1):
if list1[i + 1] < list1[i]:
ans = 0
break
return ans
t = int(input())
for _ in range(t):
n, x = list(map(int, input().strip().split()))
list1 = list(map(int, input().strip().split()))
count = 0
possible = 1
for i in range(n):
if check_sort(list1) == 1:
break
if list1[i] <= x:
if i != 0 and list1[i] < list1[i - 1]:
possible = 0
break
else:
temp = list1[i]
list1[i] = x
x = temp
count += 1
if possible == 0:
print(-1)
else:
print(count)
|
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN 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 ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
t = int(input())
def do_step(arr, x, i):
while i < len(arr):
if x < arr[i]:
arr[i], x = x, arr[i]
return x, i
i += 1
return x, i
for _ in range(t):
n, x = map(int, input().split())
arr = [int(v) for v in input().split()]
steps = 0
i = 0
while i != len(arr):
if sorted(arr) == arr:
break
x, i = do_step(arr, x, i)
if i != -1:
steps += 1
if sorted(arr) != arr:
steps = -1
print(steps)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR NUMBER RETURN VAR 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 ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
def issorted(l):
flag = 0
if all(l[i] <= l[i + 1] for i in range(len(l) - 1)):
return True
return False
t = int(input())
for _ in range(t):
n, x = map(int, input().split())
l = list(map(int, input().split()))
if issorted(l):
print(0)
else:
c = 0
for i in range(n):
while not issorted(l):
if x < l[i]:
x, l[i] = l[i], x
c += 1
break
else:
break
if issorted(l):
print(c)
else:
print(-1)
|
FUNC_DEF ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER 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 IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
def swap(arr, x):
count = 0
for i in range(1, len(arr)):
if arr[i] < x and arr[i] < arr[i - 1]:
return -1
if arr[i] < arr[i - 1]:
for j in range(0, i):
if arr[j] > x:
scratch = x
x = arr[j]
arr[j] = scratch
count += 1
if arr[i] < x and arr[i] < arr[i - 1]:
return -1
if arr[i] < arr[i - 1]:
scratch = x
x = arr[i]
arr[i] = scratch
count += 1
return count
t = int(input())
for i in range(0, t):
nx = list(map(int, input().strip().split()))
arr = list(map(int, input().strip().split()))
print(swap(arr, nx[1]))
|
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
t = int(input())
for i in range(t):
n, x = map(int, input().split())
arr = list(map(int, input().split()))
ans = 0
while not all(arr[j] <= arr[j + 1] for j in range(len(arr) - 1)):
ans += 1
for j in range(len(arr)):
if arr[j] > x:
arr[j], x = x, arr[j]
break
else:
ans = -1
break
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 NUMBER WHILE FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
def sorted_list(mylist):
n = len(mylist)
if n == 1:
return 1
for i in range(1, n):
if mylist[i - 1] > mylist[i]:
return -1
elif i == n - 1:
return 1
cases = int(input())
for z in range(cases):
n, x = input().split()
n = int(n)
x = int(x)
mylist = [int(i) for i in input().split()]
flag = 0
count = 0
for i in range(0, n):
if sorted_list(mylist) == 1:
flag = 1
break
if mylist[i] > x:
temp = x
count += 1
x = mylist[i]
mylist[i] = temp
if flag == 1:
print(count)
else:
print(-1)
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL 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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
t = int(input())
def is_sorted(a, i, j):
if i == j or i + 1 == j:
return True
if a[i] > a[i + 1]:
return False
for ii in range(i + 1, j):
if a[ii - 1] > a[ii]:
return False
return True
while t:
n, x = list(map(int, input().split()))
arr = list(map(int, input().split()))
if is_sorted(arr, 0, len(arr)):
print(0)
else:
ans = 0
for i in range(len(arr)):
if arr[i] > x:
tmp = arr[i]
arr[i] = x
x = tmp
ans += 1
if is_sorted(arr, i, len(arr)):
break
if is_sorted(arr, 0, len(arr)):
print(ans)
else:
print(-1)
t -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER WHILE 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 IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
for _ in range(int(input())):
n, x = map(int, input().split())
A = list(map(int, input().split()))
if sorted(A) == A:
print(0)
else:
i = 1
ans = 0
flag = 0
while i < n:
if A[i] < A[i - 1]:
if x >= A[i - 1] and A[i] > x:
A[i], x = x, A[i]
ans += 1
else:
j = 0
for j in range(i):
if A[j] > x:
break
else:
flag = 1
break
for k in range(j, i):
if x >= A[i - 1]:
break
if A[k] > x:
A[k], x = x, A[k]
ans += 1
elif A[k] == x:
continue
if A[i] >= A[i - 1]:
continue
if A[i] < A[i - 1] and A[i] > x:
A[i], x = x, A[i]
ans += 1
else:
flag = 1
break
i += 1
if flag:
print(-1)
else:
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 IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
def check(l, n):
for i in range(n - 1):
if l[i] > l[i + 1]:
return False
return True
def main(n, x, l):
c = 0
for i in range(n):
if check(l, n) is True:
return c
if l[i] > x:
x, l[i] = l[i], x
c += 1
if check(l, n) is True:
return c
return -1
k = int(input())
for _ in range(k):
n, x = map(int, input().strip().split())
l = list(map(int, input().strip().split()))
n = len(l)
print(main(n, x, l))
|
FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER RETURN VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER RETURN VAR RETURN NUMBER 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 FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
for _ in range(int(input())):
n, x = map(int, input().split())
a = list(map(int, input().split()))
if a == sorted(a):
print(0)
continue
ans = 0
j = 0
b = [0]
while j < n - 1:
if a[j] > a[j + 1]:
b[j] += 1
b += (b[j],)
j += 1
j = 0
while j < n:
if a[j] > x:
a[j], x = x, a[j]
ans += 1
if b[j] == b[-1]:
break
j += 1
if a == sorted(a):
print(ans)
else:
print(-1)
|
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 IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
t_count = int(input())
for _ in range(t_count):
num_count, x = map(int, input().split())
nums = list(map(int, input().split()))
operation_count = 0
sorted_until = 0
place_of_x = 0
last_was_swap = False
while sorted_until != num_count - 1:
if nums[sorted_until + 1] >= nums[sorted_until]:
if nums[sorted_until] <= x:
place_of_x += 1
sorted_until += 1
last_was_swap = False
else:
operation_count += 1
nums[place_of_x], x = x, nums[place_of_x]
if last_was_swap:
operation_count = -1
break
last_was_swap = True
sorted_until = place_of_x
print(operation_count)
|
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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
def sort1(l):
n = len(l)
for i in range(1, n):
if l[i - 1] > l[i]:
return False
return True
for _ in range(int(input())):
n, x = list(map(int, input().split()))
l = list(map(int, input().split()))
ans = 0
f = 0
while not sort1(l):
ans += 1
i = 0
while i < n and l[i] <= x:
i += 1
if i == n:
f = 1
break
x, l[i] = l[i], x
if f == 1:
print(-1)
else:
print(ans)
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER 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 NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
def how_many_operations_need_to_make_sorted(a_seq, x):
a_seq = list(a_seq)
operations_n = 0
while not __is_sorted(a_seq):
for i in range(len(a_seq)):
if a_seq[i] > x:
a_seq[i], x = x, a_seq[i]
operations_n += 1
break
else:
raise ValueError
return operations_n
def __is_sorted(seq):
for i in range(len(seq) - 1):
if not seq[i] <= seq[i + 1]:
return False
return True
def __main():
(t,) = __read_ints()
for i_t in range(t):
n, x = __read_ints()
a_seq = tuple(__read_ints())
try:
result = how_many_operations_need_to_make_sorted(a_seq, x)
except ValueError:
result = -1
print(result)
def __read_ints():
return map(int, input().split())
__main()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
def answer():
global x
opp = 0
if a == sorted(a):
return opp
i = 0
while i < n:
while i < n and a[i] <= x:
i += 1
if i < n:
x, a[i] = a[i], x
opp += 1
if a == sorted(a):
return opp
i += 1
return -1
for T in range(int(input())):
n, x = map(int, input().split())
a = list(map(int, input().split()))
print(answer())
|
FUNC_DEF ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN VAR VAR NUMBER RETURN 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 EXPR FUNC_CALL VAR FUNC_CALL VAR
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
def sol():
n, x = list(map(int, input().split()))
arr = list(map(int, input().split()))
a = 0
solution = 0
if arr == sorted(arr):
return 0
for i in range(n):
if arr[i] > x:
solution += 1
arr[i], x = x, arr[i]
if arr == sorted(arr):
return solution
if i == len(arr) - 1:
return -1
return solution
for _ in range(int(input())):
print(sol())
|
FUNC_DEF 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 NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
tc = int(input())
while tc:
n, x = map(int, input().split())
li = list(map(int, input().split()))
pre = [li[0]]
m = 0
def not_sorted(li):
for i in range(1, n):
if li[i] < li[i - 1]:
return 1
return 0
m = 0
flag = True
while not_sorted(li):
m += 1
i = 0
while i < n and li[i] <= x:
i += 1
if i == n:
flag = False
break
li[i], x = x, li[i]
if flag:
print(m)
else:
print(-1)
tc -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE 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 VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR NUMBER
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
for _ in range(int(input())):
n, x = map(int, input().split())
l = list(map(int, input().split()))
f = 1
ans = 0
for i in range(1, n):
if l[i] < l[i - 1]:
idx = i - 1
val = i - 1
for j in range(i - 2, -1, -1):
val = j
if x >= l[j]:
break
idx = val
if idx == 0 and l[0] > x:
idx -= 1
for k in range(idx + 1, i - 1):
if x == l[k]:
continue
temp = l[k]
l[k] = x
x = temp
ans += 1
cv = l[i - 1]
if l[i - 1] > x and l[i] >= x:
temp = cv
l[i - 1] = x
x = temp
ans += 1
else:
f = 0
if f:
print(ans)
else:
print(-1)
|
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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
import sys
input = sys.stdin.readline
for _ in range(int(input())):
n, x = list(map(int, input().split()))
arr = list(map(int, input().split()))
ans = 0
for i in range(1, len(arr)):
if arr[i] < arr[i - 1]:
index = i - 1
while index >= 0 and arr[index] > x:
index -= 1
ans += 1
index += 1
for j in range(index, i):
temp = arr[j]
if arr[j] == x:
ans -= 1
arr[j] = x
x = temp
for i in range(1, len(arr)):
if arr[i] < arr[i - 1]:
ans = -1
print(ans)
|
IMPORT ASSIGN VAR VAR 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 NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
def probeg(n, a):
index = 0
LN = 0
V = 1
while index < n and V:
if a[index] < LN:
V = 0
LN = a[index]
index += 1
if V:
return 0
else:
return 1
t = int(input())
for _ in range(t):
n, x = map(int, input().split())
a = list(map(int, input().split()))
V = probeg(n, a)
answer = 0
S = 1
while V:
answer += 1
index = 0
valid = 1
while index < n and valid:
if a[index] > x:
t = a[index]
a[index] = x
x = t
valid = 0
if index == n - 1:
if a[index] <= x:
answer = -1
S = 0
index += 1
if S:
V = probeg(n, a)
else:
V = 0
print(answer)
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR RETURN NUMBER RETURN NUMBER 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 FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
for w in range(int(input())):
n, x = tuple(map(int, input().split()))
a = list(map(int, input().split()))
c = 0
ans = 0
t = -1
i = 0
if a == sorted(a):
print(0)
elif x > max(a):
print(-1)
else:
for i in range(n):
if a[i] > x:
a[i], x = x, a[i]
ans += 1
if a == sorted(a):
break
if a == sorted(a):
print(ans)
else:
print(-1)
|
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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
import sys
def solve():
n, x = list(map(int, input().split()))
a = list(map(int, input().split()))
o = 0
if a == sorted(a):
print(o)
return
for i in range(len(a)):
if a == sorted(a):
print(o)
return
if a[i] > x:
temp = a[i]
a[i] = x
x = temp
o += 1
if a != sorted(a):
print(-1)
else:
print(o)
num = int(input())
for n in range(num):
solve()
|
IMPORT FUNC_DEF 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 IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
import sys
def get_ints():
return map(int, sys.stdin.readline().strip().split())
def get_int():
return list(map(int, sys.stdin.readline().strip().split()))[0]
def get_list():
return list(map(int, sys.stdin.readline().strip().split()))
def get_string():
return sys.stdin.readline().strip()
N = 0
a = []
def solve():
global N
N, X = get_ints()
a = get_list()
lastSortedPosition = N - 1
notSorted = False
for i in reversed(range(1, N)):
if a[i] < a[i - 1]:
lastSortedPosition = i
notSorted = True
break
if not notSorted:
print(0)
return
bad = False
answer = 0
for i in range(lastSortedPosition):
if a[i] > X:
a[i], X = X, a[i]
answer = answer + 1
if i != N - 1 and a[i] > a[i + 1]:
bad = True
break
elif i == lastSortedPosition - 1 and a[i] > a[i + 1]:
bad = True
break
elif i != 0 and a[i] < a[i - 1]:
bad = True
break
if bad:
print(-1)
else:
print(answer)
test_cases = get_int()
for _ in range(test_cases):
solve()
|
IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
t = int(input())
for i in range(t):
n, x = map(int, input().split())
lst = list(map(int, input().split()))
c = 0
lst1 = lst.copy()
lst1.sort()
ind = 0
while True:
if lst1 == lst or ind > n - 1:
break
elif lst[ind] > x:
lst[ind], x = x, lst[ind]
c += 1
lst1 = lst.copy()
lst1.sort()
ind += 1
else:
ind += 1
lst1 = lst.copy()
lst1.sort()
if lst1 == lst:
print(c)
else:
print(-1)
|
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 NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
def read():
return [int(x) for x in input().split()]
(ca,) = read()
for _ in range(ca):
n, x = read()
ds = read()
i = 0
r = 0
for j in range(1, len(ds)):
if ds[j] < ds[j - 1]:
while i < j:
if x < ds[i]:
r += 1
ds[i] ^= x
x ^= ds[i]
ds[i] ^= x
i += 1
if ds[j] < ds[j - 1]:
r = -1
break
print(r)
|
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
for _ in range(int(input())):
n, x = map(int, input().split())
a = list(map(int, input().split()))
res = 0
i = 0
backward = False
while i < n - 1:
if not backward:
if a[i] > a[i + 1]:
if a[i] <= x or x > a[i + 1]:
res = -1
break
elif i > 0:
if a[i - 1] <= x:
buffer = a[i]
a[i] = x
x = buffer
res += 1
i += 1
else:
backward = True
i -= 1
else:
buffer = a[i]
a[i] = x
x = buffer
res += 1
i += 1
else:
i += 1
elif i > 0:
if a[i - 1] > x:
i -= 1
else:
backward = False
buffer = a[i]
a[i] = x
x = buffer
res += 1
i += 1
else:
backward = False
buffer = a[i]
a[i] = x
x = buffer
res += 1
i += 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
t = int(input())
def getfirst(a: list, x: int):
for i in range(len(a)):
if a[i] > x:
return i
return -1
for _ in range(t):
n, x = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
while a != sorted(a):
e = getfirst(a, x)
if e < 0:
ans = -1
break
else:
a[e], x = x, a[e]
ans += 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN VAR RETURN NUMBER 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 NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
for i in range(int(input())):
n, x = map(int, input().split())
j = list(map(int, input().split()))
k = 0
m = 0
if j == sorted(j):
m = 1
print(k)
elif j != sorted(j):
for z in range(len(j)):
if j[z] > x:
t = j[z]
j[z] = x
x = t
k += 1
if j == sorted(j):
m = 1
print(k)
break
if m == 0:
print(-1)
|
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 NUMBER ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
t = int(input())
for o in range(t):
n, x = map(int, input().split())
l = list(map(int, input().split()))
c, flag = 0, False
for i in range(n):
m = l[:]
m.sort()
if m == l:
flag = True
break
elif l[i] > x:
x, l[i] = l[i], x
c += 1
if flag:
print(c)
else:
print(-1)
|
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 VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
T = int(input())
r = 1
while r <= T:
n, x = map(int, input().split())
arr = list(map(int, input().split()))
ans = 0
stack = [0]
temp = [x]
for i in range(len(arr)):
a = arr[i]
if stack[-1] <= a:
stack.append(a)
continue
if stack[-1] <= temp[-1]:
ans = -1
break
while stack[-1] > temp[0]:
temp.append(stack.pop())
temp = [temp[0]] + temp[1:][::-1]
for ele in temp[:-1]:
stack.append(ele)
if ele != arr[len(stack) - 2]:
ans += 1
temp = [temp[-1]]
if stack[-1] <= a:
stack.append(a)
else:
ans = -1
break
print(ans)
r += 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE 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 NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST VAR NUMBER VAR NUMBER NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
def swaps(A, x):
n = len(A)
count = 0
if A == sorted(A):
return 0
for i in range(n):
if A == sorted(A):
return count
if A[i] > x:
A[i], x = x, A[i]
count += 1
if A == sorted(A):
return count
return -1
t = int(input())
for i in range(t):
n, x = input().split()
n = int(n)
x = int(x)
a = [int(j) for j in input().split()][:n]
print(swaps(a, x))
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL 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 VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
def s(a, n):
for i in range(1, n):
if arr[i] < arr[i - 1]:
return False
return True
for _ in range(int(input())):
n, k = map(int, input().split(" "))
arr = [int(num) for num in input().split(" ")]
a = arr.copy()
a.sort()
ans = 0
while s(arr, n) == False:
f = 0
for i in range(n):
if arr[i] > k:
temp = arr[i]
arr[i] = k
k = temp
ans = ans + 1
f = 1
break
if f == 0:
break
if s(arr, n) == True:
print(ans)
else:
print(-1)
|
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
for i in range(int(input())):
n, x = map(int, input().split(" "))
l = list(map(int, input().split(" ")))
if n == 1:
print(0)
else:
c = 1
count = 0
for i in range(1, n):
if l[i] >= l[i - 1]:
c += 1
if c == n:
print(0)
else:
end = False
for i in range(0, n):
if end:
i = n
continue
if l[i] > x:
x, l[i] = l[i], x
count += 1
c = 1
for i in range(1, n):
if l[i] >= l[i - 1]:
c += 1
if not c == n:
continue
else:
print(count)
end = True
if not end:
c = 1
for i in range(1, n):
if l[i] >= l[i - 1]:
c += 1
if c == n:
print(count)
else:
print(-1)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
def find(a, si):
x = a[si]
for i in range(si + 1, len(a)):
if x > a[i]:
return False
x = a[i]
return True
def solve(a, x):
p = 0
c = 0
while p < len(a):
if find(a, 0):
return c
if x < a[p]:
x, a[p] = a[p], x
c += 1
p += 1
return -1
t = int(input())
for _ in range(t):
n, x = [int(i) for i in input().split()]
l = [int(i) for i in input().split()]
print(solve(l, x))
|
FUNC_DEF ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
for test_var in range(int(input())):
n, x = map(int, input().split())
arr = list(map(int, input().split()))
def get_index():
for i, m in enumerate(arr):
if m > x:
return i
return -1
count = 0
while arr != sorted(arr):
index = get_index()
if index < 0:
break
arr[index], x = x, arr[index]
count += 1
if arr == sorted(arr):
print(count)
else:
print(-1)
|
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 FUNC_DEF FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
import sys
readline = sys.stdin.readline
rdw = lambda: readline().rstrip()
rdws = lambda: readline().split()
rdwl = lambda: list(readline().split())
rdi = lambda: int(readline())
rdis = lambda: map(int, readline().split())
rdil = lambda: list(map(int, readline().split()))
rdilrows = lambda cnt: [rdil() for _ in range(cnt)]
def solve():
n, x = rdis()
a = rdil()
res = 0
for i in range(n - 1):
if a[i] > a[i + 1]:
if a[i] > x and x <= a[i + 1]:
j = i - 1
while j >= 0 and a[j] > x:
if a[j] > a[i + 1]:
print(-1)
return
if a[j] != a[j + 1]:
res += 1
j -= 1
x = a[i]
res += 1
else:
res = -1
break
print(res)
tests = 1
tests = rdi()
for testnum in range(tests):
solve()
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
t = int(input())
for i in range(t):
n, x = [int(c) for c in input().split()]
a = [int(c) for c in input().split()]
a = [-1] + a + [1000]
check1 = check2 = swaps = indx = 0
for j in range(n, 1, -1):
if a[j - 1] > a[j]:
indx = j - 1
check1 = 1
break
for j in range(1, indx + 2):
if x < a[j]:
swaps += 1
a[j], x = x, a[j]
if a[j - 1] > a[j]:
check2 = 1
break
if not check1:
print(0)
elif not check2:
print(swaps)
else:
print(-1)
|
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 BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
for T in range(int(input())):
n, x = [int(x) for x in input().split()]
arr = [int(x) for x in input().split()]
ans = 0
if arr == sorted(arr):
print(ans)
continue
done = 0
for i in range(n):
if arr == sorted(arr):
print(ans)
done = 1
break
if arr[i] > x:
arr[i], x = x, arr[i]
ans += 1
if done:
continue
elif arr == sorted(arr):
print(ans)
else:
print(-1)
|
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 NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
def check(arr):
newarr = []
for i in range(1, len(arr)):
if arr[i - 1] > arr[i]:
return False
return True
t = int(input())
for i in range(t):
n, x = input().split()
n = int(n)
x = int(x)
a = [int(i) for i in input().split()]
j = 0
c = 0
while j < n:
if check(a):
break
if a[j] > x:
a[j], x = x, a[j]
c += 1
j += 1
z = []
for i in a:
z.append(i)
z.sort()
c1 = 0
for i in range(len(a)):
if a[i] == z[i]:
c1 += 1
if c1 == len(a):
pass
else:
c = -1
print(c)
|
FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL 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 NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
for i in range(int(input())):
n, k = map(int, input().split())
a = list(map(int, input().split()))
l = 0
sumo = 0
for j in range(1, n):
if a[j] < a[j - 1]:
l = l + 1
if l == 0:
print("0")
else:
l = 0
m = 0
for j in range(n - 1, 1, -1):
m = m + 1
if a[j] < a[j - 1]:
break
for j in range(n):
if a[j] > k:
if j >= n - m:
break
t = a[j]
a[j] = k
k = t
sumo = sumo + 1
for j in range(1, n):
if a[j] < a[j - 1]:
l = l + 1
break
if l == 0:
print(sumo)
else:
print("-1")
|
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 NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
import sys
input = sys.stdin.readline
for _ in range(int(input())):
n, x = map(int, input().split())
l = [*map(int, input().split())]
if l == sorted(l):
print(0)
continue
cunt = 0
for i in range(n):
if l == sorted(l):
break
if l[i] > x:
x, l[i] = l[i], x
cunt += 1
if l == sorted(l):
print(cunt)
continue
print(-1)
|
IMPORT ASSIGN VAR 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 LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
for _ in range(int(input())):
n, x = [int(x) for x in input().split()]
l = list(map(int, input().split()))
count = 0
for i in range(len(l)):
flag = True
for j in range(len(l) - 1):
if l[j] > l[j + 1]:
flag = False
if flag == True:
break
elif l[i] > x:
temp = x
x = l[i]
l[i] = temp
count = count + 1
for j in range(len(l) - 1):
if l[j] > l[j + 1]:
flag = False
if flag == False:
print(-1)
else:
print(count)
|
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
t = int(input())
for i in range(t):
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = sorted(b)
flag = False
if b == c:
print(0)
else:
count = 0
for j in range(a[0]):
if sorted(b) != b:
if b[j] > a[1]:
count += 1
b[j], a[1] = a[1], b[j]
if sorted(b) == b:
print(count)
flag = True
break
if flag == False:
print(-1)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
def sS():
n, x = map(int, input().split())
arr = list(map(int, input().split()))
if check(arr) == True:
return 0
c = 0
for i in range(n):
if arr[i] > x:
arr[i], x = x, arr[i]
c += 1
if check(arr) == True:
return c
if check(arr) == False:
return -1
def check(arr):
m = 0
for i in range(len(arr) - 1):
if arr[i] > arr[i + 1]:
m += 1
if m > 0:
return False
return True
t = int(input())
while t > 0:
print(sS())
t -= 1
|
FUNC_DEF 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 IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
import sys
def minp():
return sys.stdin.readline().strip()
def mint():
return int(minp())
def mints():
return map(int, minp().split())
def solve():
n, x = mints()
a = list(mints())
b = a.copy()
b.append(x)
b.sort()
res = int(1000000000.0)
for i in range(n + 1):
ok = True
if b[i] >= x:
k = 0
xx = x
cnt = 0
for j in range(n):
if k == i:
k += 1
if b[k] != a[j]:
if xx != b[k] or a[j] < xx:
ok = False
break
xx = a[j]
cnt += 1
k += 1
if ok:
res = min(res, cnt)
if res >= int(1000000000.0):
print(-1)
else:
print(res)
for t in range(mint()):
solve()
|
IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
inn = lambda: int(input())
inm = lambda: map(int, input().split())
ins = lambda: str(input())
ina = lambda: list(map(int, input().split()))
def solve():
n, x = inm()
a = ina()
if sorted(a) == a:
print(0)
return
moves = 0
pref = [1] * n
for i in range(n - 2, -1, -1):
if a[i] <= a[i + 1]:
pref[i] = 1 & pref[i + 1]
else:
pref[i] = 0
for i, v in enumerate(a):
if v > x:
a[i], x = x, a[i]
moves += 1
if pref[i]:
break
b = a[:]
if sorted(b) == a:
print(moves)
else:
print(-1)
def main():
t = 1
t = int(input())
for _ in range(t):
solve()
main()
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
from sys import stdin
stdin.readline
def mp():
return list(map(int, stdin.readline().strip().split()))
def it():
return int(stdin.readline().strip())
def not_sorted(l):
for i in range(1, len(l)):
if l[i] < l[i - 1]:
return 1
return 0
for _ in range(it()):
n, x = mp()
l = mp()
count, flag, i = 0, 0, 0
while not_sorted(l):
count += 1
while i < n and l[i] <= x:
i += 1
if i == n:
flag = 1
break
l[i], x = x, l[i]
if flag:
print(-1)
else:
print(count)
|
EXPR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE FUNC_CALL VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
import sys
sys.setrecursionlimit(10**5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II():
return int(sys.stdin.buffer.readline())
def MI():
return map(int, sys.stdin.buffer.readline().split())
def LI():
return list(map(int, sys.stdin.buffer.readline().split()))
def LLI(rows_number):
return [LI() for _ in range(rows_number)]
def BI():
return sys.stdin.buffer.readline().rstrip()
def SI():
return sys.stdin.buffer.readline().rstrip().decode()
inf = 10**16
md = 10**9 + 7
def isinc(aa, n):
for i in range(n - 1):
if aa[i] > aa[i + 1]:
return False
return True
def solve(n, x, aa):
if isinc(aa, n):
return 0
aa += [-inf]
res = 0
for i in range(n):
if aa[i] > x and aa[i - 1] <= x:
aa[i], x = x, aa[i]
res += 1
if aa[i - 1] > aa[i]:
return -1
if isinc(aa, n):
return res
return res
for _ in range(II()):
n, x = MI()
aa = LI()
print(solve(n, x, aa))
|
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN NUMBER VAR LIST VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR RETURN VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
for q11 in range(int(input())):
n, x = map(int, input().split())
a = list(map(int, input().split()))
max1 = max2 = -float("inf")
for q in range(n):
if a[q] > max1:
max2, max1 = max1, a[q]
elif a[q] > max2:
max2 = a[q]
if a[q] < max2:
print(-1)
break
else:
seg = []
for q in range(1, n):
if a[q - 1] > a[q]:
seg.append(q - 1)
if len(seg) == 0:
print(0)
elif a[seg[0] + 1] < x:
print(-1)
else:
q1, q2, plus = seg[0] - 1, 0, 1
while q1 > -1 and a[q1] > x:
plus += q1 == 0 or a[q1] != a[q1 - 1]
q1 -= 1
for q in range(seg[0] + 1, seg[-1] + 1):
if q == seg[q2 + 1]:
plus += 1
q2 += 1
else:
plus += a[q] > a[seg[q2]] and a[q] != a[q - 1]
print(plus)
|
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 FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER WHILE VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given a sequence $a$ consisting of $n$ integers $a_1, a_2, \dots, a_n$, and an integer $x$. Your task is to make the sequence $a$ sorted (it is considered sorted if the condition $a_1 \le a_2 \le a_3 \le \dots \le a_n$ holds).
To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $i$ such that $1 \le i \le n$ and $a_i > x$, and swap the values of $a_i$ and $x$.
For example, if $a = [0, 2, 3, 5, 4]$, $x = 1$, the following sequence of operations is possible:
choose $i = 2$ (it is possible since $a_2 > x$), then $a = [0, 1, 3, 5, 4]$, $x = 2$;
choose $i = 3$ (it is possible since $a_3 > x$), then $a = [0, 1, 2, 5, 4]$, $x = 3$;
choose $i = 4$ (it is possible since $a_4 > x$), then $a = [0, 1, 2, 3, 4]$, $x = 5$.
Calculate the minimum number of operations you have to perform so that $a$ becomes sorted, or report that it is impossible.
-----Input-----
The first line contains one integer $t$ ($1 \le t \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains two integers $n$ and $x$ ($1 \le n \le 500$, $0 \le x \le 500$) β the number of elements in the sequence and the initial value of $x$.
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 500$).
The sum of values of $n$ over all test cases in the input does not exceed $500$.
-----Output-----
For each test case, print one integer β the minimum number of operations you have to perform to make $a$ sorted, or $-1$, if it is impossible.
-----Examples-----
Input
6
4 1
2 3 5 4
5 6
1 1 3 4 4
1 10
2
2 10
11 9
2 10
12 11
5 18
81 324 218 413 324
Output
3
0
0
-1
1
3
-----Note-----
None
|
def is_sorted(a):
n = len(a)
for i in range(n - 1):
if a[i + 1] < a[i]:
return False
return True
t = int(input())
for case in range(t):
n, x = [int(s) for s in input().split(" ")]
a = [int(s) for s in input().split(" ")]
ans = 0
i = 0
while i < n:
if is_sorted(a):
break
if a[i] > x:
a[i], x = x, a[i]
ans += 1
i += 1
if not is_sorted(a):
ans = -1
print(ans)
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER 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 STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
def find(A, x, k, flag):
if flag == 1:
codd = 1
count = 0
for i in range(len(A)):
if codd == 1:
if A[i] <= x:
count += 1
codd = 0
else:
count += 1
codd = 1
if count >= k:
return True
else:
ceven = 0
count = 0
for i in range(len(A)):
if ceven == 1:
if A[i] <= x:
count += 1
ceven = 0
else:
count += 1
ceven = 1
if count >= k:
return True
return False
def answer(n, k, A):
if k == 2:
return min(A)
l = 0
r = max(A)
while r >= l:
mid = l + (r - l) // 2
fodd = find(A, mid, k, 1)
feven = find(A, mid, k, 0)
if fodd or feven:
r = mid - 1
else:
l = mid + 1
return l
n, k = map(int, input().split())
A = list(map(int, input().split()))
print(answer(n, k, A))
|
FUNC_DEF IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
def fun(m):
global a1, k
c1 = 0
c2 = 0
for i in a1:
if i <= m or c1 % 2 == 0:
c1 += 1
if i <= m or c2 % 2 != 0:
c2 += 1
f1 = bool(c1 >= k)
f2 = bool(c2 >= k)
return f1 or f2
n, k = map(int, input().split())
a1 = list(map(int, input().split()))
l = 0
b = max(a1)
while l < b:
m = (l + b) // 2
if fun(m):
b = m
else:
l = m + 1
print(l)
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN 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 NUMBER ASSIGN VAR FUNC_CALL VAR 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
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
import sys
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II():
return int(sys.stdin.readline())
def MI():
return map(int, sys.stdin.readline().split())
def LI():
return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number):
return [LI() for _ in range(rows_number)]
def SI():
return sys.stdin.readline()[:-1]
def main():
def ok(i):
s = ss[i]
if k & 1:
cnt = 0
pre = False
for a in aa:
if pre:
pre = False
continue
if a <= s:
cnt += 1
pre = True
if cnt == k // 2 + 1:
return True
cnt = 0
pre = False
for a in aa[1 : n - 1]:
if pre:
pre = False
continue
if a <= s:
cnt += 1
pre = True
if cnt == k // 2:
return True
else:
cnt = 0
pre = False
for a in aa[: n - 1]:
if pre:
pre = False
continue
if a <= s:
cnt += 1
pre = True
if cnt == k // 2:
return True
cnt = 0
pre = False
for a in aa[1:]:
if pre:
pre = False
continue
if a <= s:
cnt += 1
pre = True
if cnt == k // 2:
return True
return False
n, k = MI()
aa = LI()
ss = list(sorted(set(aa)))
sn = len(ss)
l = -1
r = sn
while l + 1 < r:
i = (l + r) // 2
if ok(i):
r = i
else:
l = i
print(ss[r])
main()
|
IMPORT ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER FUNC_DEF FUNC_DEF ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER VAR 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 VAR EXPR FUNC_CALL VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
from sys import stdin, stdout
def MI():
return map(int, stdin.readline().split())
def LI():
return list(map(int, stdin.readline().split()))
def isValidOdd(a, n, k, v):
cnt = 0
for x in a:
if cnt % 2 == 0:
if x <= v:
cnt += 1
else:
cnt += 1
return cnt >= k
def isValidEven(a, n, k, v):
cnt = 0
for x in a:
if cnt % 2 == 1:
if x <= v:
cnt += 1
else:
cnt += 1
return cnt >= k
def bs(a, n, k, l, h):
ans = h
while l <= h:
mid = (l + h) // 2
if isValidOdd(a, n, k, mid) or isValidEven(a, n, k, mid):
ans = mid
h = mid - 1
else:
l = mid + 1
return ans
n, k = MI()
a = LI()
ans = bs(a, n, k, 1, 10**9)
stdout.write(str(ans) + "\n")
|
FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
def isValidOdd(mid):
cnt = 0
for x in range(n):
if cnt % 2 == 0:
if arr[x] <= mid:
cnt += 1
else:
cnt += 1
if cnt >= k:
return True
return False
def isValidEven(mid):
cnt = 0
for x in range(n):
if cnt % 2 == 1:
if arr[x] <= mid:
cnt += 1
else:
cnt += 1
if cnt >= k:
return True
return False
def bs(left, right):
ans = right
while left <= right:
mid = (left + right) // 2
if isValidOdd(mid) or isValidEven(mid):
right = mid - 1
ans = mid
else:
left = mid + 1
print(ans)
n, k = map(int, input().split())
arr = list(map(int, input().split()))
bs(1, max(arr))
|
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR 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 EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
def check(a, m, chk, k):
ct = 0
for i in a:
if i <= m or ct % 2 != chk:
ct += 1
if ct >= k:
return True
else:
return False
n, k = map(int, input().split())
a = list(map(int, input().split()))
l = 1
h = 10**9 + 1
m = 0
while l < h:
m = (l + h) // 2
if check(a, m, 0, k) or check(a, m, 1, k):
h = m
else:
l = m + 1
print(l)
|
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
def can_alterate(vs, max_v, num_to_take, begin_i, end_i):
i = begin_i
while i < end_i and num_to_take > 0:
if vs[i] <= max_v:
i += 1
num_to_take -= 1
i += 1
return num_to_take == 0
def can_fit_max(vs, max_v, num_to_take):
return can_alterate(
vs, max_v, (num_to_take + 1) // 2, 0, len(vs) - 1 + num_to_take % 2
) or can_alterate(vs, max_v, num_to_take // 2, 1, len(vs) - num_to_take % 2)
def main():
n, k = [int(t) for t in input().strip().split()]
vs = [int(t) for t in input().strip().split()]
min_target = 0
max_target = max(vs)
while max_target > min_target + 1:
mid_target = (min_target + max_target) // 2
if can_fit_max(vs, mid_target, k):
max_target = mid_target
else:
min_target = mid_target
print(max_target)
main()
|
FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
def testing():
n, k = [int(x) for x in input().split()]
arr = [int(x) for x in input().split()]
min_ = 1
max_ = 10**9
while min_ < max_:
target = (min_ + max_) // 2
skip = 0
count = 0
for i in range(len(arr)):
if skip or arr[i] <= target:
count += 1
skip = 1 - skip
if count >= k:
max_ = target
continue
skip = 1
count = 0
for i in range(len(arr)):
if skip or arr[i] <= target:
count += 1
skip = 1 - skip
if count >= k:
max_ = target
else:
min_ = target + 1
return max_
print(testing())
|
FUNC_DEF 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 NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
def check(M, x):
if u[M][0] > x:
return False
cnt = 1
if M < n - 1:
cnt += 1
if M > 0:
cnt += 1
i = M + 2
while i < n:
if u[i][0] <= x:
cnt += 1
if i < n - 1:
cnt += 1
i += 1
i += 1
i = M - 2
while i >= 0:
if u[i][0] <= x:
cnt += 1
if i > 0:
cnt += 1
i -= 1
i -= 1
return cnt >= k
n, k = map(int, input().split())
u = list(map(int, input().split()))
for i in range(n):
u[i] = u[i], i
s = sorted(u)
s.reverse()
L = 0
R = n
while R - L > 1:
M = (L + R) // 2
if (
check(s[M][1], s[M][0])
or s[M][1] > 0
and check(s[M][1] - 1, s[M][0])
or s[M][1] < n - 1
and check(s[M][1] + 1, s[M][0])
):
L = M
else:
R = M
print(s[L][0])
|
FUNC_DEF IF VAR VAR NUMBER VAR RETURN NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN 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 FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL 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 VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
n, k = [int(x) for x in input().split()]
arr = [int(x) for x in input().split()]
l = -1
r = 10**9
while r - l != 1:
tmp = 0
j = 0
mid = (r + l) // 2
while j < len(arr):
if arr[j] <= mid:
tmp += 1
if j != len(arr) - 1:
tmp += 1
j += 2
else:
j += 1
if tmp >= k:
r = mid
else:
l = mid
r1 = r
l = -1
r = 10**9
l = -1
r = 10**9
while r - l != 1:
tmp = 1
j = 1
mid = (r + l) // 2
while j < len(arr):
if arr[j] <= mid:
tmp += 1
if j != len(arr) - 1:
tmp += 1
j += 2
else:
j += 1
if tmp >= k:
r = mid
else:
l = mid
print(min(r1, r))
|
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 NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
def check(a, mid, t):
l = 0
for i in range(len(a)):
if t:
if l % 2 and a[i] <= mid:
l += 1
elif not l % 2:
l += 1
elif not l % 2 and a[i] <= mid:
l += 1
elif l % 2:
l += 1
return l
def main():
n, k = map(int, input().split())
a = list(map(int, input().split()))
lo, hi = min(a), max(a)
mi = hi
while lo <= hi:
mid = (lo + hi) // 2
e = max(check(a, mid, 0), check(a, mid, 1))
if e >= k:
mi, hi = min(mid, mi), mid - 1
else:
lo = mid + 1
print(mi)
main()
|
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF 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 FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
def valid(m, even=True):
l = 0
for i in range(n):
if not even:
l += 1
even = not even
elif a[i] <= m:
l += 1
even = not even
return l >= k
n, k = map(int, input().strip().split())
a = list(map(int, input().strip().split()))
l = 1
h = 1000000000.0
while l < h:
m = l + (h - l) // 2
if valid(m, even=True) or valid(m, even=False):
h = m
else:
l = m + 1
print(int(h))
|
FUNC_DEF NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
n, k = map(int, input().split())
a = list(map(int, input().split()))
aOrderSorter = []
aOrderList = []
for i in range(n):
aOrderSorter.append((a[i], i))
aOrderList.append(-1)
aOrderSorter.sort(key=lambda x: x[0])
for i in range(n):
aOrderList[aOrderSorter[i][1]] = i
if k % 2 == 0:
k = k // 2
oddMin = -1
evenMin = -1
l = 0
r = k
while r != l:
m = (r + l) // 2
subseq = []
subseqLen = 0
for i in range(n):
if i == n - 1:
break
if aOrderList[i] < k + m and (
subseqLen == 0 or subseq[subseqLen - 1] != i - 1
):
subseq.append(i)
subseqLen += 1
if subseqLen >= k:
break
if subseqLen >= k:
r = m
else:
l = m + 1
oddMin = r
l = 0
r = k
while r != l:
m = (r + l) // 2
subseq = []
subseqLen = 0
for i in range(n):
if i == 0:
continue
if aOrderList[i] < k + m and (
subseqLen == 0 or subseq[subseqLen - 1] != i - 1
):
subseq.append(i)
subseqLen += 1
if subseqLen >= k:
break
if subseqLen >= k:
r = m
else:
l = m + 1
evenMin = r
print(aOrderSorter[k + min(oddMin, evenMin) - 1][0])
else:
k = k // 2
l = 0
r = k + 2
while r != l:
m = (r + l) // 2
subseq = []
subseqLen = 0
for i in range(n):
if aOrderList[i] < k + m and (
subseqLen == 0 or subseq[subseqLen - 1] != i - 1
):
subseq.append(i)
subseqLen += 1
if subseqLen >= k + 1:
break
if subseqLen >= k + 1:
r = m
else:
l = m + 1
oddMin = r
l = 0
r = k
while r != l:
m = (r + l) // 2
subseq = []
subseqLen = 0
for i in range(n):
if i == 0:
continue
if i == n - 1:
break
if aOrderList[i] < k + m and (
subseqLen == 0 or subseq[subseqLen - 1] != i - 1
):
subseq.append(i)
subseqLen += 1
if subseqLen >= k:
break
if subseqLen >= k:
r = m
else:
l = m + 1
evenMin = r
print(aOrderSorter[k + min(oddMin, evenMin) - 1][0])
|
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 EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
n, k = map(int, input().split())
arr = list(map(int, input().split()))
low = 1
high = 10**9
def checkodd(mid):
count = 0
for i in range(0, n):
if count % 2 == 0:
if arr[i] <= mid:
count += 1
else:
count += 1
return count >= k
def checkeven(mid):
count = 0
for i in range(0, n):
if count % 2 != 0:
if arr[i] <= mid:
count += 1
else:
count += 1
return count >= k
while low < high:
mid = (low + high) // 2
if checkodd(mid) or checkeven(mid):
high = mid
else:
low = mid + 1
print(low)
|
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 BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
n, k = map(int, input().split())
lo = 0
hi = 10**9
l = list(map(int, input().split()))
while hi - lo > 1:
test = lo + (hi - lo) // 2
odd = 0
skip = False
for v in l:
if skip:
skip = False
odd += 1
elif v <= test:
odd += 1
skip = True
even = 0
skip = True
for v in l:
if skip:
skip = False
even += 1
elif v <= test:
even += 1
skip = True
if odd >= k or even >= k:
hi = test
else:
lo = test
print(hi)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
from sys import gettrace, stdin
if gettrace():
inputi = input
else:
def input():
return next(stdin)[:-1]
def inputi():
return stdin.buffer.readline()
def main():
n, k = map(int, inputi().split())
aa = [int(a) for a in inputi().split()]
lo = 0
hi = max(aa) + 1
while lo < hi:
mid = (lo + hi) // 2
if valid_sequence(aa, k, mid, n):
hi = mid
else:
lo = mid + 1
print(lo)
def valid_sequence(aa, k, mid, n):
i = 0
count = 0
while i < n - 1 + k % 2 and count < (k + 1) // 2:
if aa[i] <= mid:
i += 2
count += 1
else:
i += 1
found = count == (k + 1) // 2
if not found:
i = 1
count = 0
while i < n - k % 2 and count < k // 2:
if aa[i] <= mid:
i += 2
count += 1
else:
i += 1
found = count == k // 2
return found
main()
|
IF FUNC_CALL VAR ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF 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 ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
n, k = map(int, input().split())
arr = list(map(int, input().split()))
def check(m, ind):
c = 0
for i in arr:
if i <= m or c % 2 != ind:
c += 1
return c >= k
l, h = 1, 10**9
while l < h:
m = (l + h) // 2
if check(m, 0) or check(m, 1):
h = m
else:
l = m + 1
print(l)
|
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 FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
n, k = [int(i) for i in input().split()]
array = [int(i) for i in input().split()]
def check_possible(array, x):
i = 0
removed = 0
while i < n:
if array[i] <= x:
i += 2
else:
i += 1
removed += 1
odd = removed <= n - k
i = 1
removed = 0
while i < n:
if array[i] <= x:
i += 2
else:
i += 1
removed += 1
even = removed <= n - k
return odd or even
x_min = 1
x_max = 1000000001
while x_max - x_min > 1:
if (x_max + x_min) % 2 == 0:
x_mid = (x_max + x_min) // 2
else:
x_mid = (x_max + x_min - 1) // 2
if check_possible(array, x_mid):
x_max = x_mid
else:
x_min = x_mid
if check_possible(array, x_min):
print(x_min)
else:
print(x_max)
|
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 FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
n, k = map(int, input().split())
def jest(x, s, k1):
bo = map(lambda y: y <= s, x)
p = 0
b = False
for i in bo:
if i:
if b:
b = False
else:
b = True
p += 1
else:
b = False
return p >= k1
a = list(map(int, input().split()))
kon = max(a)
pocz = min(a)
while pocz < kon:
v = (pocz + kon) // 2
if k % 2:
if jest(a, v, k // 2 + 1) or jest(a[1:-1], v, k // 2):
kon = v
else:
pocz = v + 1
elif jest(a[:-1], v, k // 2) or jest(a[1:], v, k // 2):
kon = v
else:
pocz = v + 1
print(kon)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR IF VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
import sys
def is_possible(val, arr, k):
count = 0
just_added = False
for el in arr:
if el > val:
just_added = False
elif not just_added:
just_added = True
count += 1
if count == k:
return True
else:
just_added = False
return False
def binary_search(possible_sorted_answers, arr, to_pick):
left = 0
right = len(possible_sorted_answers)
while True:
x = (left + right) // 2
p1 = is_possible(possible_sorted_answers[x], arr, to_pick)
if p1 and x == 0:
return possible_sorted_answers[0]
elif p1:
right = x
elif x == len(possible_sorted_answers) - 1:
return possible_sorted_answers[-1]
else:
p2 = is_possible(possible_sorted_answers[x + 1], arr, to_pick)
if p2:
return possible_sorted_answers[x + 1]
else:
left = x
def input():
return sys.stdin.readline().rstrip()
def input_split():
return [int(i) for i in input().split()]
n, k = input_split()
arr = input_split()
arr_sorted = arr.copy()
arr_sorted.sort()
possible_sorted_answers = arr_sorted[k // 2 - 1 : k]
if k % 2 == 0:
ans = min(
binary_search(possible_sorted_answers, arr[:-1], k // 2),
binary_search(possible_sorted_answers, arr[1:], k // 2),
)
else:
ans = min(
binary_search(possible_sorted_answers, arr[1:-1], k // 2),
binary_search(possible_sorted_answers, arr, k // 2 + 1),
)
print(ans)
|
IMPORT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER RETURN VAR NUMBER IF VAR ASSIGN VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR RETURN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
n, k = map(int, input().split())
a = list(map(int, input().split()))
def bs(mid, s, k):
count = 0
for i in range(n):
if s or a[i] <= mid:
count += 1
s ^= 1
if count >= k:
return True
return False
left = 0
right = 1000000000.0
while left < right:
mid = int(left + right) // 2
if bs(mid, 0, k) or bs(mid, 1, k):
right = mid
else:
left = mid + 1
print(left)
|
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 FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
import sys
input = sys.stdin.readline
inputr = lambda: input().rstrip("\n")
INF = 10**18
n, k = map(int, input().split())
A = list(map(int, input().split()))
if k == 1:
print(min(A))
sys.exit(0)
need = k // 2
def test(V, takeo):
g = 0
o = 0
for v in A:
if takeo:
o += 1
takeo = False
elif v <= V:
g += 1
takeo = True
if o + g == k:
return True
return False
SA = sorted(set(A))
lo = 0
hi = len(SA)
best = INF
while lo < hi:
m = (lo + hi) // 2
V = SA[m]
if test(V, False) or test(V, True):
best = min(best, V)
hi = m
else:
lo = m + 1
print(best)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP NUMBER 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 IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
n, k = map(int, input().split())
l = 0
r = 10**9
a = list(map(int, input().split()))
def is_ok(num):
length = 0
for i in range(len(a)):
if length % 2 == 0 and a[i] <= num:
length += 1
elif length % 2 == 1:
length += 1
if length >= k:
return True
length = 0
for i in range(len(a)):
if length % 2 == 1 and a[i] <= num:
length += 1
elif length % 2 == 0:
length += 1
if length >= k:
return True
return False
while r - l > 1:
m = (r + l) // 2
if is_ok(m):
r = m
else:
l = m
print(r)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN 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 EXPR FUNC_CALL VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
from sys import stdin
input = stdin.readline
n, k = [int(x) for x in input().split()]
arr = [int(x) for x in input().split()]
min_ = 1
max_ = 10**9
def works(arr, k, target, skip):
count = 0
for i in range(len(arr)):
if skip or arr[i] <= target:
count += 1
skip = 1 - skip
return count >= k
while min_ < max_:
target = (min_ + max_) // 2
if works(arr, k, target, 0) or works(arr, k, target, 1):
max_ = target
else:
min_ = target + 1
print(max_)
|
ASSIGN 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 NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR RETURN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
n, k = [int(i) for i in input().split()]
arr = [int(i) for i in input().split()]
def check(x, p):
ans = 0
for i in range(len(arr)):
if not p:
ans += 1
p ^= 1
elif arr[i] <= x:
ans += 1
p ^= 1
return ans >= k
l = 1
r = 10**9
res = 0
while l <= r:
mid = l + (r - l) // 2
if check(mid, 0) or check(mid, 1):
res = mid
r = mid - 1
else:
l = mid + 1
print(res)
|
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 FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
import sys
input = sys.stdin.readline
n, k = map(int, input().split())
a = [int(item) for item in input().split()]
if k <= 2:
print(min(a))
exit()
l = 0
r = 10**9 + 10
while r - l > 1:
mid = (l + r) // 2
is_small = []
for i, item in enumerate(a):
if item <= mid:
is_small.append(1)
else:
is_small.append(0)
if is_small[:3] == [1, 1, 0]:
is_small[0] = 0
max_cnt = 0
for i in range(2):
cnt = 0
state = i
for item in is_small:
if item:
if state == 0:
cnt += 1
state = 1
else:
cnt += 1
state = 0
elif state == 1:
cnt += 1
state = 0
max_cnt = max(max_cnt, cnt)
if max_cnt >= k:
r = mid
else:
l = mid
print(r)
|
IMPORT ASSIGN 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 IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER LIST NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
N, K = map(int, input().split(" "))
a = [int(x) for x in input().split(" ")]
def solve(b):
c = [(0) for _ in range(len(b))]
for i, x in enumerate(b):
if i == 0:
c[i] = 1 if x else 0
else:
c[i] = c[i - 1]
if x:
c[i] = max(c[i], 1 + (c[i - 2] if i >= 2 else 0))
return c[-1]
def ok(maxv):
if K % 2 == 0:
b = [(x <= maxv) for x in a[1:]]
c = [(x <= maxv) for x in a[:-1]]
return solve(b) >= K // 2 or solve(c) >= K // 2
else:
b = [(x <= maxv) for x in a]
c = [(x <= maxv) for x in a[1:-1]]
return solve(b) >= (K + 1) // 2 or solve(c) >= (K - 1) // 2
lo, hi = 1, int(1000000000.0)
while lo != hi:
mi = (lo + hi) // 2
if ok(mi):
hi = mi
else:
lo = mi + 1
print(lo)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER RETURN VAR NUMBER FUNC_DEF IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL 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 VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
def subsolve(a, x):
c = 0
i = 0
while i < len(a):
if a[i] > x:
i += 1
continue
j = i
while j < len(a) and a[j] <= x:
j += 1
c += (j - i) // 2 + (j - i) % 2
i = j
return c
def odd(n, k, a):
if k >= 2 and k % 2 == 0:
a = a[:-1]
l, r = 0, max(a) + 1
while r - l > 1:
mid = (l + r) // 2
if subsolve(a, mid) >= k // 2 + k % 2:
r = mid
else:
l = mid
return r
def even(n, k, a):
if k == 1:
return max(a) + 1
a = a[1:]
if k % 2 == 1:
a.pop(-1)
l, r = 0, max(a) + 1
while r - l > 1:
mid = (l + r) // 2
if subsolve(a, mid) >= k // 2:
r = mid
else:
l = mid
return r
def solve():
n, k = map(int, input().split())
a = list(map(int, input().split()))
print(min(odd(n, k, a), even(n, k, a)))
solve()
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF 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 EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
import sys
input = sys.stdin.readline
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = a.copy()
b.sort()
low = 0
high = len(b) - 1
mid = len(b) // 2
answer1 = 10**10
while low <= high:
need = k // 2
if k % 2 == 0:
mom = len(a)
else:
mom = len(a) - 1
ans = b[mid]
i = 1
while need > 0 and i < mom:
if a[i] <= ans:
need -= 1
i += 2
else:
i += 1
if need == 0:
answer1 = min(answer1, ans)
if need > 0:
low = mid + 1
else:
high = mid - 1
mid = (low + high) // 2
low = 0
high = len(b) - 1
mid = len(b) // 2
answer2 = 10**10
while low <= high:
need = k // 2 + k % 2
ans = b[mid]
i = 0
mam = 0
if k % 2 == 0:
mam = len(a) - 1
else:
mam = len(a)
while need > 0 and i < mam:
if a[i] <= ans:
need -= 1
i += 2
else:
i += 1
if need == 0:
answer2 = min(answer2, ans)
if need > 0:
low = mid + 1
else:
high = mid - 1
mid = (low + high) // 2
print(min(answer1, answer2))
|
IMPORT ASSIGN 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 FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
def check(x, p):
ans = 0
for i in range(n):
if a[i] <= x or ans % 2 == p:
ans += 1
if ans >= k:
return True
return False
n, k = map(int, input().split())
a = list(map(int, input().split()))
lo, hi = min(a), max(a)
while lo < hi:
x = (lo + hi) // 2
if check(x, 0) or check(x, 1):
hi = x
else:
lo = x + 1
print(lo)
|
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
def ss_length(arr, x, k, oe):
l = 0
for i in range(len(arr)):
if oe:
if arr[i] <= x:
l += 1
oe ^= 1
else:
l += 1
oe ^= 1
return l >= k
def bin_search(arr, li, hi, k):
if li >= hi:
return li
mi = (li + hi) // 2
ss_len_check = ss_length(arr, mi, k, 0) or ss_length(arr, mi, k, 1)
if ss_len_check:
return bin_search(arr, li, mi, k)
else:
return bin_search(arr, mi + 1, hi, k)
n, k = map(int, input().strip().split())
arr = list(map(int, input().strip().split()))
ans = bin_search(arr, 1, int(1000000000.0), k)
print(ans)
|
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
def check(num):
count = 0
flag = -1
s = 0
for i in range(n):
if a[i] <= num:
count += 1
s += 1
elif flag == -1:
flag = s % 2
count += 1
s += 1
elif (s + 1) % 2 != flag:
count += 1
s += 1
if count == k:
return True
return False
n, k = map(int, input().split())
a = list(map(int, input().split()))
if n == k:
m1 = 0
m2 = 0
for i in range(0, n, 2):
m1 = max(m1, a[i])
for i in range(1, n, 2):
m2 = max(m2, a[i])
print(min(m1, m2))
return
s = set(a)
minn, maxx = min(a), max(a)
low = minn
high = maxx
while low < high:
mid = (low + high) // 2
if check(mid):
high = mid - 1
else:
low = mid + 1
if check(low):
print(low)
else:
print(low + 1)
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR 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 EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
import sys
n, k = [int(x) for x in input().split()]
arr = [int(x) for x in input().split()]
start, end = min(arr), max(arr)
ans = -1
def good(mid, curr):
now = "odd"
temp = 0
for i in range(n):
if now == "odd" and curr == "odd":
if arr[i] <= mid:
temp += 1
now = "even"
continue
elif now == "even" and curr == "even":
if arr[i] <= mid:
temp += 1
now = "odd"
continue
else:
temp += 1
if now == "odd":
now = "even"
else:
now = "odd"
return temp >= k
while start <= end:
mid = (start + end) // 2
if good(mid, "odd") or good(mid, "even"):
ans = mid
end = mid - 1
else:
start = mid + 1
print(ans)
|
IMPORT 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 VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR STRING VAR STRING IF VAR VAR VAR VAR NUMBER ASSIGN VAR STRING IF VAR STRING VAR STRING IF VAR VAR VAR VAR NUMBER ASSIGN VAR STRING VAR NUMBER IF VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING RETURN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
l = lambda: map(int, input().split())
n, k = l()
a = list(l())
def check(x, cur):
ans = 0
for i in range(n):
if cur == 0:
ans += 1
cur = 1
elif a[i] <= x:
ans += 1
cur = 0
return ans >= k
lo = 1
hi = int(1000000000.0)
while lo < hi:
mid = (lo + hi) // 2
if check(mid, 0) or check(mid, 1):
hi = mid
else:
lo = mid + 1
print(lo)
|
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
n, k = map(int, input().split())
(*arr,) = map(int, input().split())
lo = 0
hi = max(arr)
def possible(arr, k, mid):
odd_cnt = 0
even_cnt = 0
for v in arr:
if v <= mid:
even_cnt, odd_cnt = max(odd_cnt + 1, even_cnt), max(even_cnt + 1, odd_cnt)
else:
odd_cnt = max(odd_cnt, even_cnt + 1)
return max(even_cnt, odd_cnt) >= k
while lo < hi:
mid = (lo + hi) // 2
if possible(arr, k, mid):
hi = mid
else:
lo = mid + 1
print(lo)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
n, k = map(int, input().split())
a = list(map(int, input().split()))
l = 0
r = 10**9
while r - l != 1:
m = (l + r) // 2
ind = 0
cnt = 0
while ind < n:
if a[ind] <= m and not (k % 2 == 0 and ind == n - 1):
cnt += 1
ind += 2
else:
ind += 1
ind = 1
cnt2 = 0
while ind < n:
if a[ind] <= m and not (k % 2 == 1 and ind == n - 1):
cnt2 += 1
ind += 2
else:
ind += 1
if cnt >= (k + 1) // 2 or cnt2 >= k // 2:
r = m
else:
l = m
print(r)
|
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 BIN_OP NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
n, k = map(int, input().split())
arr = list(map(int, input().split()))
def done(x, s):
c = 0
for i in range(len(arr)):
if s or arr[i] <= x:
c += 1
s = s ^ 1
return c >= k
lb = 1
rb = max(arr)
ans = rb
while lb < rb:
mid = (lb + rb) // 2
if done(mid, 0) or done(mid, 1):
rb = mid
ans = min(ans, mid)
else:
lb = mid + 1
print(ans)
|
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 FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR 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 NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
import sys
def check(a, pos):
u = 0
for i in range(n):
if pos == 0:
u += 1
pos = 1
elif t[i] <= a:
u += 1
pos = 0
if u >= k:
return 1
else:
return 0
def bina(l, r):
while l < r:
m = l + (r - l) // 2
if check(m, 0) | check(m, 1):
r = m
else:
l = m + 1
return l
input = sys.stdin.readline
n, k = map(int, input().split())
t = list(map(int, input().split()))
print(bina(1, 1000000000))
|
IMPORT FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN 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 EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
def isvalid0(x, num):
count = 0
curr = 0
for i in range(n):
if curr % 2 == 0:
if li[i] <= x:
count += 1
curr ^= 1
else:
curr ^= 1
count += 1
if count >= num:
return 1
return 0
def isvalid1(x, num):
count = 0
curr = 0
for i in range(n):
if curr % 2 == 1:
if li[i] <= x:
count += 1
curr ^= 1
else:
curr ^= 1
count += 1
if count >= num:
return 1
return 0
def binary_search(low, high):
while low < high:
mid = (low + high) // 2
if isvalid0(mid, k) or isvalid1(mid, k):
high = mid
else:
low = mid + 1
return low
l = input().split()
n = int(l[0])
k = int(l[1])
l = input().split()
li = [int(i) for i in l]
print(binary_search(1, 10**9))
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
n, k = map(int, input().split())
a = [int(j) for j in input().split()]
def check(num, idx):
ans = 0
for i in range(n):
if idx == 0:
ans += 1
idx = 1
elif idx == 1 and a[i] <= num:
ans += 1
idx = 0
return ans >= k
def bsearch(lo, hi):
while lo < hi:
mid = lo + (hi - lo) // 2
if check(mid, 0) or check(mid, 1):
hi = mid
else:
lo = mid + 1
return lo
ans = bsearch(1, max(a))
print(ans)
|
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER RETURN VAR VAR FUNC_DEF WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
import sys
input = sys.stdin.readline
def ceil(x, y):
return (x + y - 1) // y
def swaparr(arr, a, b):
temp = arr[a]
arr[a] = arr[b]
arr[b] = temp
def gcd(a, b):
if b == 0:
return a
return gcd(b, a % b)
def nCr(n, k):
if k > n - k:
k = n - k
res = 1
for i in range(k):
res = res * (n - i)
res = res / (i + 1)
return int(res)
def upper_bound(a, x, lo=0):
hi = len(a)
while lo < hi:
mid = (lo + hi) // 2
if a[mid] < x:
lo = mid + 1
else:
hi = mid
return lo
def primefs(n):
primes = {}
while n % 2 == 0 and n > 0:
primes[2] = primes.get(2, 0) + 1
n = n // 2
for i in range(3, int(n**0.5) + 2, 2):
while n % i == 0 and n > 0:
primes[i] = primes.get(i, 0) + 1
n = n // i
if n > 2:
primes[n] = primes.get(n, 0) + 1
return primes
def power(x, y, p):
res = 1
x = x % p
if x == 0:
return 0
while y > 0:
if y & 1 == 1:
res = res * x % p
y = y >> 1
x = x * x % p
return res
def swap(a, b):
temp = a
a = b
b = temp
return a, b
def find(x, link):
p = x
while p != link[p]:
p = link[p]
while x != p:
nex = link[x]
link[x] = p
x = nex
return p
def union(x, y, link, size):
x = find(x, link)
y = find(y, link)
if size[x] < size[y]:
x, y = swap(x, y)
if x != y:
size[x] += size[y]
link[y] = x
def sieve(n):
prime = [(True) for i in range(n + 1)]
p = 2
while p * p <= n:
if prime[p] == True:
for i in range(p * p, n + 1, p):
prime[i] = False
p += 1
return prime
MAXN = int(10000000.0 + 5)
def spf_sieve():
spf[1] = 1
for i in range(2, MAXN):
spf[i] = i
for i in range(4, MAXN, 2):
spf[i] = 2
for i in range(3, ceil(MAXN**0.5), 2):
if spf[i] == i:
for j in range(i * i, MAXN, i):
if spf[j] == j:
spf[j] = i
def factoriazation(x):
ret = {}
while x != 1:
ret[spf[x]] = ret.get(spf[x], 0) + 1
x = x // spf[x]
return ret
def int_array():
return list(map(int, input().strip().split()))
def str_array():
return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float("inf")
NINF = -float("inf")
def check(x, cur):
ans = 0
for i in range(n):
if not cur:
ans += 1
cur ^= 1
elif a[i] <= x:
ans += 1
cur ^= 1
return ans >= k
def bin_search(lo, hi):
while lo < hi:
mid = (lo + hi) // 2
if check(mid, 0) or check(mid, 1):
hi = mid
else:
lo = mid + 1
return lo
n, k = int_array()
a = int_array()
ans = bin_search(0, int(1000000000.0 + 1))
print(ans)
|
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_DEF IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_DEF NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT WHILE BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER WHILE VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR 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 VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_DEF ASSIGN VAR DICT WHILE VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
n, k = map(int, input().split())
s = list(map(int, input().split()))
def f(m):
t = []
for x in s:
if x > m:
t.append(False)
else:
t.append(True)
LEN = 0
for x in t:
if x == True:
LEN += 1
else:
break
small = False
for i in range(LEN, len(t)):
if small == False:
small = True
LEN += 1
elif small == True and t[i] == True:
small = False
LEN += 1
if LEN >= k:
return True
return False
def bi(a, b):
if a == b:
return a
m = (a + b) // 2
if f(m) == True:
return bi(a, m)
else:
return bi(m + 1, b)
print(bi(1, 10**9))
|
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 FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER NUMBER
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
import sys
input = sys.stdin.buffer.readline
def possible(start, k, x):
c = 0
for i in range(start, len(arr)):
if c % 2 == 1:
c += 1
elif arr[i] <= x:
c += 1
if c == k:
return True
return False
n, k = [int(x) for x in input().split()]
arr = [int(x) for x in input().split()]
maxCost = max(arr)
b = maxCost // 2 + 1
maxImpossible = -1
while b > 0:
while (
possible(1, k - 1, maxImpossible + b)
== possible(0, k, maxImpossible + b)
== False
):
maxImpossible += b
b //= 2
minCost = maxImpossible + 1
print(minCost)
|
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER 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 FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER WHILE FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
import sys
input = sys.stdin.readline
inp, ip = lambda: int(input()), lambda: [int(w) for w in input().split()]
n, k = ip()
x = ip()
l, r = 0, max(x)
ans = 0
while l <= r:
mid = (l + r) // 2
flag = 0
for start in [0, 1]:
ct = 0
alt = start
for i in x:
if not alt or i <= mid:
ct += 1
alt = not alt
if ct >= k:
flag = 1
if flag:
ans = mid
r = mid - 1
else:
l = mid + 1
print(ans)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR 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
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
def main():
n, k = [int(s) for s in input().split()]
a = [int(s) for s in input().split()]
low = min(a)
high = max(a)
while low < high:
mid = (low + high) // 2
if possible(a, k, mid):
high = mid
else:
low = mid + 1
print(low)
def possible(a, k, x):
return greedy(a, k, x, 0) or greedy(a, k, x, 1)
def greedy(a, k, x, parity):
i = 0
s = []
while i < len(a) and len(s) < k:
if len(s) % 2 == parity:
if a[i] <= x:
s.append(a[i])
else:
s.append(a[i])
i += 1
return len(s) == k
main()
|
FUNC_DEF 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 FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
n, k = map(int, input().split())
a = list(map(int, input().split()))
def f(x):
global n, k, a
check = 0
now = 1
for i in range(n):
if now:
if a[i] <= x:
now = 1 - now
check += 1
else:
now = 1 - now
check += 1
if check >= k:
return True
check = 0
now = 0
for i in range(n):
if now:
if a[i] <= x:
now = 1 - now
check += 1
else:
now = 1 - now
check += 1
if check >= k:
return True
else:
return False
l, r = 0, 10**9 + 1
while l + 1 < r:
m = l + (r - l) // 2
if f(m):
r = m
else:
l = m
print(r)
|
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 FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR IF VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR IF VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
import sys
input = sys.stdin.readline
f = lambda: list(map(int, input().split()))
_N = 1111111111
n, k = f()
inp = f()
l, r = min(inp), max(inp)
ans = r
while l <= r:
m = (l + r) // 2
length = 0
flag = False
for i in inp:
if length % 2:
length += 1
elif i <= m:
length += 1
else:
pass
if length >= k:
flag = True
break
length = 0
for i in inp:
if not length % 2:
length += 1
elif i <= m:
length += 1
else:
pass
if length >= k:
flag = True
break
if flag:
ans = min(ans, m)
r = m - 1
else:
l = m + 1
print(ans)
|
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
def nd(lst, num, prc):
dp = [(0) for i in range(len(lst))]
dp[0] = int(lst[0] <= prc)
for i in range(1, len(lst)):
if lst[i] > prc:
dp[i] = dp[i - 1]
elif i >= 2:
dp[i] = max(dp[i - 1], dp[i - 2] + 2)
else:
dp[i] = 2
return max(dp[-1], dp[-2] + 1) >= num
n, k = map(int, input().split())
lst = list(map(int, input().split()))
l = min(lst)
r = max(lst)
while r - l > 1:
m = (l + r) // 2
if nd(lst, k, m):
r = m
else:
l = m
if nd(lst, k, l):
print(l)
else:
print(r)
|
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER 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 VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
|
Ashish has an array $a$ of size $n$.
A subsequence of $a$ is defined as a sequence that can be obtained from $a$ by deleting some elements (possibly none), without changing the order of the remaining elements.
Consider a subsequence $s$ of $a$. He defines the cost of $s$ as the minimum between: The maximum among all elements at odd indices of $s$. The maximum among all elements at even indices of $s$.
Note that the index of an element is its index in $s$, rather than its index in $a$. The positions are numbered from $1$. So, the cost of $s$ is equal to $min(max(s_1, s_3, s_5, \ldots), max(s_2, s_4, s_6, \ldots))$.
For example, the cost of $\{7, 5, 6\}$ is $min( max(7, 6), max(5) ) = min(7, 5) = 5$.
Help him find the minimum cost of a subsequence of size $k$.
-----Input-----
The first line contains two integers $n$ and $k$ ($2 \leq k \leq n \leq 2 \cdot 10^5$) Β β the size of the array $a$ and the size of the subsequence.
The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) Β β the elements of the array $a$.
-----Output-----
Output a single integer Β β the minimum cost of a subsequence of size $k$.
-----Examples-----
Input
4 2
1 2 3 4
Output
1
Input
4 3
1 2 3 4
Output
2
Input
5 3
5 3 4 2 6
Output
2
Input
6 4
5 3 50 2 4 5
Output
3
-----Note-----
In the first test, consider the subsequence $s$ = $\{1, 3\}$. Here the cost is equal to $min(max(1), max(3)) = 1$.
In the second test, consider the subsequence $s$ = $\{1, 2, 4\}$. Here the cost is equal to $min(max(1, 4), max(2)) = 2$.
In the fourth test, consider the subsequence $s$ = $\{3, 50, 2, 4\}$. Here the cost is equal to $min(max(3, 2), max(50, 4)) = 3$.
|
N, k = map(int, input().split())
a = list(map(int, input().split()))
if k % 2 == 0:
def cond(n):
res = 0
L = 0
for i in range(N):
if a[i] > n:
res += (L + 1) // 2
L = 0
else:
L += 1
res += L // 2
if res >= k // 2:
return True
res = 0
L = 0
for i in range(N - 1, -1, -1):
if a[i] > n:
res += (L + 1) // 2
L = 0
else:
L += 1
res += L // 2
if res >= k // 2:
return True
return False
start = 0
end = 10**9
while end - start > 1:
test = (end + start) // 2
if cond(test):
end = test
else:
start = test
print(end)
else:
def cond(n):
res = 0
L = 0
for i in range(N):
if a[i] > n:
res += (L + 1) // 2
L = 0
else:
L += 1
res += (L + 1) // 2
if res >= k // 2 + 1:
return True
res = 0
L = 0
for i in range(N - 2, -1, -1):
if a[i] > n:
res += (L + 1) // 2
L = 0
else:
L += 1
res += L // 2
if res >= k // 2:
return True
return False
start = 0
end = 10**9
while end - start > 1:
test = (end + start) // 2
if cond(test):
end = test
else:
start = test
print(end)
|
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 IF BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER 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 EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER 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 EXPR FUNC_CALL VAR VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.