Hemose Shopping (Codeforces 1592B)
Tags: Math, dsu

Hemose was shopping with his friends Samez, AhmedZ, AshrafEzz, TheSawan and O_E in Germany. As you know, Hemose and his friends are problem solvers, so they are very clever. Therefore, they will go to all discount markets in Germany.Hemose has an array of $$$n$$$ integers. He wants Samez to sort the array in the non-decreasing order. Since it would be a too easy problem for Samez, Hemose allows Samez to use only the following operation:Choose indices $$$i$$$ and $$$j$$$ such that $$$1 \le i, j \le n$$$, and $$$\lvert i - j \rvert \geq x$$$. Then, swap elements $$$a_i$$$ and $$$a_j$$$.Can you tell Samez if there's a way to sort the array in the non-decreasing order by using the operation written above some finite number of times (possibly $$$0$$$)?

Input: Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ $$$(1 \leq t \leq 10^5)$$$. Description of the test cases follows.The first line of each test case contains two integers $$$n$$$ and $$$x$$$ $$$(1 \leq x \leq n \leq 10^5)$$$.The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ..., a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$.It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.

Output: For each test case, you should output a single string. If Samez can sort the array in non-decreasing order using the operation written above, output "YES" (without quotes). Otherwise, output "NO" (without quotes).You can print each letter of "YES" and "NO" in any case (upper or lower).

Note: In the first test case, you can't do any operations.In the second test case, the array is already sorted.In the third test case, you can do the operations as follows:   $$$[5,1,2,3,4]$$$, $$$swap(a_1,a_3)$$$  $$$[2,1,5,3,4]$$$, $$$swap(a_2,a_5)$$$  $$$[2,4,5,3,1]$$$, $$$swap(a_2,a_4)$$$  $$$[2,3,5,4,1]$$$, $$$swap(a_1,a_5)$$$  $$$[1,3,5,4,2]$$$, $$$swap(a_2,a_5)$$$  $$$[1,2,5,4,3]$$$, $$$swap(a_3,a_5)$$$  $$$[1,2,3,4,5]$$$ (Here $$$swap(a_i, a_j)$$$ refers to swapping elements at positions $$$i$$$, $$$j$$$).

Sample Inputs:
4
3 3
3 2 1
4 3
1 2 3 4
5 2
5 1 2 3 4
5 4
1 2 3 4 4

Sample Outputs:
NO
YES
YES
YES

Solution (Python):
import sys
input = lambda : sys.stdin.readline()[:-1]
ni = lambda : int(input())
na = lambda : list(map(int, input().split()))
sys.setrecursionlimit(10 ** 7)
yes = lambda : print('yes')
Yes = lambda : print('Yes')
YES = lambda : print('YES')
no = lambda : print('no')
No = lambda : print('No')
NO = lambda : print('NO')

def f(a):
    for i in range(len(a) - 1):
        if a[i] > a[i + 1]:
            return False
    return True
t = ni()
for _ in range(t):
    (n, x) = na()
    a = na()
    if x * 2 <= n:
        YES()
        continue
    else:
        x = n - x
        if x == 0:
            if f(a):
                YES()
            else:
                NO()
            continue
        b = a[:x] + a[-x:]
        b = sorted(b)
        ans = 1
        if b[x - 1] > a[x]:
            ans = 0
        if b[x] < a[n - x - 1]:
            ans = 0
        for i in range(x, n - x - 1):
            if a[i] > a[i + 1]:
                ans = 0
        if ans:
            YES()
        else:
            NO()

Solution (Python):
import sys
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
    return int(sys.stdin.readline())

def LI():
    return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
    return [LI() for _ in range(rows_number)]

def LI1():
    return list(map(int1, sys.stdin.readline().split()))

def LLI1(rows_number):
    return [LI1() for _ in range(rows_number)]

def SI():
    return sys.stdin.readline().rstrip()
inf = 10 ** 16
md = 998244353

def solve():
    (n, x) = LI()
    aa = LI()
    l = n - x
    r = x
    if l >= r:
        print('YES')
        return
    bb = aa[:l] + aa[r:]
    bb.sort()
    aa = bb[:l] + aa[l:r] + bb[l:]
    for i in range(n - 1):
        if aa[i] > aa[i + 1]:
            print('NO')
            return
    print('YES')
for testcase in range(II()):
    solve()

Solution (Python):
from sys import stdin
for _ in range(int(stdin.readline())):
    (n, x) = list(map(int, stdin.readline().split()))
    a = list(map(int, stdin.readline().split()))
    b = sorted(a)
    end = x
    start = n - x
    res = 'YES'
    for i in range(start, end):
        if a[i] != b[i]:
            res = 'NO'
            break
    print(res)