Problem:
 Score : 600 points    Problem Statement Given are two integer sequences of N elements each: A_1,...,A_N and B_1,...,B_N .
Determine if it is possible to do the following operation at most N-2 times (possibly zero) so that, for every integer i from 1 to N , A_i \leq B_i holds:   Choose two distinct integers x and y between 1 and N (inclusive), and swap the values of A_x and A_y .       Constraints  2 \leq N \leq 10^5  1 \leq A_i,B_i \leq 10^9         Input Input is given from Standard Input in the following format:  N  A_1  A_2  ...  A_N  B_1  B_2  ...  B_N       Output If the objective is achievable, print Yes ; if it is not, print No .        Sample Input 1 3
1 3 2
1 2 3      Sample Output 1 Yes  We should swap the values of A_2 and A_3 .       Sample Input 2 3
1 2 3
2 2 2      Sample Output 2 No       Sample Input 3 6
3 1 2 6 3 4
2 2 8 3 4 3      Sample Output 3 Yes

Solution (Python):
def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    ab = sorted([[a[i], b[i]] for i in range(n)], key=lambda x: x[1])

    num=1
    while num < n:
        num *= 2
    num-=1
    inf=10**9+1

    m=num+n
    segtree=[inf]*m
    for i in range(num,m):
        segtree[i] = ab[i - num][0]
    for i in range(num-1,-1,-1):
        j=2*i+1
        if j>=m:
            segtree[i]=inf
        elif j+1>=m:
            segtree[i]=segtree[j]
        else:
            segtree[i] = min(segtree[j], segtree[j + 1])

    
    def search(s):
        i=0
        while i < num:
            k = 2 * i + 1
            if k+2<=m:
                if segtree[k+1] <= s:
                    i = k + 1
                    continue
            if k+1<=m:
                if segtree[k] <= s:
                    i = k
                    continue
            return - 1
        return i - num
    
    
    def update_good(i, s):
        while True:
            if segtree[i] > s:
                segtree[i] = s
                i = (i - 1) // 2
                continue
            break
    
    def update_bad(i):
        while i!=0:
            k=(i - 1) // 2
            if i % 2 == 1:
                if m > i + 1:
                    temp=min(segtree[i+1],segtree[i])
                    if segtree[k]<temp:
                        segtree[k] = temp
                        i = k
                        continue
                else:
                    segtree[k] = segtree[i]
                    i = k
                    continue
            else:
                temp=min(segtree[i-1],segtree[i])
                if segtree[k]<temp:
                    segtree[k] = temp
                    i = k
                    continue
            break

    cnt = 0
    for i in range(n):
        a,b=ab[i][0],ab[i][1]
        if a > b:
            t = search(b)
            if t <= i:
                print("No")
                return 0
            update_good(num + i, a)
            segtree[num + t] = a
            update_bad(num + t)
            ab[i][0], ab[t][0] = ab[t][0], a
            cnt+=1
    if cnt <= n - 2:
        print("Yes")
    else:
        print("No")
    
main()

Solution (Python):
def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    ab = sorted([[a[i], b[i]] for i in range(n)], key=lambda x: x[1])

    num=1
    while num < n:
        num *= 2
    num-=1
    inf=10**9+1

    m=num+n
    segtree=[inf]*m
    for i in range(num,m):
        segtree[i] = ab[i - num][0]
    for i in range(num-1,-1,-1):
        j=2*i+1
        if j>=m:
            segtree[i]=inf
        elif j+1>=m:
            segtree[i]=segtree[j]
        else:
            segtree[i] = min(segtree[j], segtree[j + 1])

    
    def search(s):
        i=0
        while i < num:
            k = 2 * i + 1
            if k+2<=m:
                if segtree[k+1] <= s:
                    i = k + 1
                    continue
            if k+1<=m:
                if segtree[k] <= s:
                    i = k
                    continue
            return - 1
        return i - num
    
    
    def update_good(i, s):
        while True:
            if segtree[i] > s:
                segtree[i] = s
                i = (i - 1) // 2
                continue
            break
    
    def update_bad(i):
        while i!=0:
            k=(i - 1) // 2
            if i % 2 == 1:
                if m > i + 1:
                    temp=min(segtree[i+1],segtree[i])
                    if segtree[k]<temp:
                        segtree[k] = temp
                        i = k
                        continue
                else:
                    segtree[k] = segtree[i]
                    i = k
                    continue
            else:
                temp=min(segtree[i-1],segtree[i])
                if segtree[k]<temp:
                    segtree[k] = temp
                    i = k
                    continue
            break

    cnt = 0
    for i in range(n):
        a,b=ab[i][0],ab[i][1]
        if a > b:
            t = search(b)
            if t <= i:
                print("No")
                return 0
            update_good(num + i, a)
            segtree[num + t] = a
            update_bad(num + t)
            ab[i][0], ab[t][0] = ab[t][0], a
            cnt+=1
    if cnt <= n - 2:
        print("Yes")
    else:
        print("No")
    
main()

Solution (Python):
def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    ab = sorted([[a[i], b[i]] for i in range(n)], key=lambda x: x[1])

    num=1
    while num < n:
        num *= 2
    num-=1
    inf=10**9+1

    m=num+n
    segtree=[inf]*m
    for i in range(num,m):
        segtree[i] = ab[i - num][0]
    for i in range(num-1,-1,-1):
        j=2*i+1
        if j>=m:
            segtree[i]=inf
        elif j+1>=m:
            segtree[i]=segtree[j]
        else:
            segtree[i] = min(segtree[j], segtree[j + 1])

    
    def search(s):
        i=0
        while i < num:
            k = 2 * i + 1
            if k+2<=m:
                if segtree[k+1] <= s:
                    i = k + 1
                    continue
            if k+1<=m:
                if segtree[k] <= s:
                    i = k
                    continue
            return - 1
        return i - num
    
    
    def update_good(i, s):
        if segtree[i] > s:
            segtree[i] = s
            update_good((i - 1) // 2, s)
    
    def update_bad(i):
        k=(i - 1) // 2
        if i % 2 == 1:
            if m > i + 1:
                temp=min(segtree[i+1],segtree[i])
                if segtree[k]<temp:
                    segtree[k] = temp
                    update_bad(k)
            else:
                segtree[k] = segtree[i]
                update_bad(k)
        else:
            if i != 0:
                temp=min(segtree[i-1],segtree[i])
                if segtree[k]<temp:
                    segtree[k] = temp
                    update_bad(k)

    cnt = 0
    for i in range(n):
        a,b=ab[i][0],ab[i][1]
        if a > b:
            t = search(b)
            if t <= i:
                print("No")
                return 0
            update_good(num + i, a)
            segtree[num + t] = a
            update_bad(num + t)
            ab[i][0], ab[t][0] = ab[t][0], a
            cnt+=1
    if cnt <= n - 2:
        print("Yes")
    else:
        print("No")
    
main()

Solution (Python):
def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    ab = sorted([[a[i], b[i]] for i in range(n)], key=lambda x: x[1])

    num=1
    while num < n:
        num *= 2
    num-=1
    inf=10**9+1

    m=num+n
    segtree=[inf]*m
    for i in range(num,m):
        segtree[i] = ab[i - num][0]
    for i in range(num-1,-1,-1):
        j=2*i+1
        if j>=m:
            segtree[i]=inf
        elif j+1>=m:
            segtree[i]=segtree[j]
        else:
            segtree[i] = min(segtree[j], segtree[j + 1])

    
    def search(i, s):
        if i >= num:
            return i - num
        k = 2 * i + 1
        if k+2<=m:
            if segtree[k+1] <= s:
                return search(k+1, s)
        if 2*i+2<=m:
            if segtree[k] <= s:
                return search(k, s)
        return -1
    
    
    def update_good(i, s):
        if segtree[i] > s:
            segtree[i] = s
            update_good((i - 1) // 2, s)
    
    def update_bad(i):
        k=(i - 1) // 2
        if i % 2 == 1:
            if m > i + 1:
                temp=min(segtree[i+1],segtree[i])
                if segtree[k]<temp:
                    segtree[k] = temp
                    update_bad(k)
            else:
                segtree[k] = segtree[i]
                update_bad(k)
        else:
            if i != 0:
                temp=min(segtree[i-1],segtree[i])
                if segtree[k]<temp:
                    segtree[k] = temp
                    update_bad(k)

    cnt = 0
    for i in range(n):
        a,b=ab[i][0],ab[i][1]
        if a > b:
            t = search(0, b)
            if t <= i:
                print("No")
                return 0
            update_good(num + i, a)
            segtree[num + t] = a
            update_bad(num + t)
            ab[i][0], ab[t][0] = ab[t][0], a
            cnt+=1
    if cnt <= n - 2:
        print("Yes")
    else:
        print("No")
    
main()

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ab = [(a[i], b[i]) for i in range(n)]
ab.sort(key=lambda x: x[1])
a = [ab[i][0] for i in range(n)]
b = [ab[i][1] for i in range(n)]
ai = [(ab[i][0], i) for i in range(n)]
a.sort()
ai.sort(key=lambda x: x[0])

cond1 = True
for i in range(n):
    if a[i] > b[i]:
        cond1 = False
        break

idx = ai[0][1]
cnt = 1
while idx != 0:
    idx = ai[idx][1]
    cnt += 1



if cnt == n:
    cond2 = False
    for i in range(n - 1):
        if b[i] >= a[i + 1]:
            cond2 = True
            break
else:
    cond2 = True

ans = "Yes" if cond1 and cond2 else "No"
print(ans)

Solution (Python):
import sys

def wantp(n):

    nq = []
    while n != p[n]:
        nq.append(n)
        n = p[n]

    for i in nq:
        p[i] = n

    return n


N = int(input())

A = list(map(int,input().split()))
B = list(map(int,input().split()))

A2 = []
B2 = []

for i in range(N):

    A2.append([A[i],i])
    B2.append([B[i],i])

A2.sort()
B2.sort()

p = [i for i in range(N)]
rank = [1] * N

for i in range(N):

    if A2[i][0] > B2[i][0]:
        print ("No")
        sys.exit()

for i in range(N):

    na = A2[i][1]
    nb = B2[i][1]

    ap = wantp(na)
    bp = wantp(nb)

    if ap != bp:

        if rank[ap] > rank[bp]:
            p[bp] = ap

        elif rank[bp] > rank[ap]:
            p[ap] = bp

        else:
            p[ap] = bp
            rank[bp] += 1

nloop = 0

for i in range(N):

    if p[i] == i:
        nloop += 1

if nloop != 1:
    print ("Yes")
    sys.exit()


for i in range(N-1):

    if B2[i][0] >= A2[i+1][0]:
        print ("Yes")
        sys.exit()

print ("No")

Solution (Python):



import sys
sys.setrecursionlimit(10**6)
readline = sys.stdin.readline 

n = int(input())
a = [int(i) for i in readline().split()]
b = [int(i) for i in readline().split()]
pb = [(bi,i) for i,bi in enumerate(b)]

sa = sorted(a)
sb = sorted(b)
pb.sort()

na = [a[i] for bi,i in pb]


ok = 1
for i,j in zip(sa,sb):
    if i > j: ok = 0

for i,j in zip(sa[1:],sb):
    if i <= j: break
else:
    
    d = {ai:i for i,ai in enumerate(sa)}
    ra = [d[ai] for ai in na]
    
    
    now = 0
    for i in range(n-1):
        now = ra[now]
        
        if now == 0:
            break
    else: ok = 0    


    



if ok: print("Yes")    
else: print("No")

Solution (Python):
def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    ab = sorted([[a[i], b[i]] for i in range(n)],key=lambda x:x[1])
    num=131071

    m=num+n
    segtree=[10**9+1]*m
    for i in range(num,m):
        segtree[i] = ab[i - num][0]
    for i in range(num-1,-1,-1):
        j=2*i+1
        if j>=m:
            segtree[i]=10**9+1
        elif j+1>=m:
            segtree[i]=segtree[j]
        else:
            segtree[i] = min(segtree[j], segtree[j + 1])

    
    def search(i, s):
        if i >= num:
            return i - num
        k = 2 * i + 1
        if k+2<=m:
            if segtree[k+1] <= s:
                return search(k+1, s)
        if 2*i+2<=m:
            if segtree[k] <= s:
                return search(k, s)
        return -1
    
    
    def update_good(i, s):
        if segtree[i] > s:
            segtree[i] = s
            update_good((i - 1) // 2, s)
    
    def update_bad(i):
        k=(i - 1) // 2
        if i % 2 == 1:
            if m > i + 1:
                temp=min(segtree[i+1],segtree[i])
                if segtree[k]<temp:
                    segtree[k] = temp
                    update_bad(k)
            else:
                segtree[k] = segtree[i]
                update_bad(k)
        else:
            if i != 0:
                temp=min(segtree[i-1],segtree[i])
                if segtree[k]<temp:
                    segtree[k] = temp
                    update_bad(k)

    cnt = 0
    for i in range(n):
        a,b=ab[i][0],ab[i][1]
        if a > b:
            t = search(0, b)
            if t <= i:
                print("No")
                return 0
            update_good(num + i, a)
            segtree[num + t] = a
            update_bad(num + t)
            ab[i][0], ab[t][0] = ab[t][0], a
            cnt+=1
    if cnt <= n - 2:
        print("Yes")
    else:
        print("No")
    
main()

Solution (Python):
class UnionFind:
    
    def __init__(self, n):
        
        self.par = [-1] * n
        
        self.rank = [0] * n

    
    def find(self, x):
        if self.par[x] < 0:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    
    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        
        if x != y:
            
            if self.rank[x] < self.rank[y]:
                x,y = y,x
            
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            
            self.par[x] += self.par[y]
            self.par[y] = x

    
    def same(self, x, y):
        return self.find(x) == self.find(y)

    
    def size(self, x):
        return -self.par[self.find(x)]

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))


c = sorted(a)
d = sorted(b)


for i in range(n):
    if c[i]>d[i]:
        print('No')
        exit()


for i in range(n-1):
    if c[i+1]<=d[i]:
        print('Yes')
        exit()


aa = {}  
bb = {}  
cc = {}  
dd = {}  
for i in range(n):
    aa[a[i]]=i
    bb[b[i]]=i
    cc[c[i]]=i
    dd[d[i]]=i


u = UnionFind(n)
for i in range(n):
    j = aa[c[dd[b[i]]]]
    u.unite(i,j)


for i in range(n):
    if u.same(0,i)==False:
        print('Yes')
        exit()

print('No')

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
AB = [[A[i],B[i]] for i in range(N)]
AB.sort(key=lambda x:x[1])
A = [[AB[i][0],i] for i in range(N)]
A.sort(key=lambda x:x[0])
da = {}
for i in range(N):
    j = A[i][1]
    da[j] = i 

ans = True
i = 0
for j in range(N):
    i = da[i]
    if i == 0:
        break

A = [AB[i][0] for i in range(N)]
A.sort()
B.sort()
if j == N-1:
    ok = False
    for i in range(N-1):
        if B[i]>=A[i+1]:
            ok = True
    ans = ok

for i in range(N):
    if A[i]>B[i]:
        ans = False

if ans:
    print('Yes')
else:
    print('No')

Solution (Python):
N = int(input())
A = [int(a) for a in input().split()]
B = [int(a) for a in input().split()]
A = [(A[i]<<18) + i for i in range(N)]
B = [(B[i]<<18) + i + (1<<17) for i in range(N)]
AA = sorted(A)
BB = sorted(B)

IA = {}
IB = {}
for i in range(N):
    IA[AA[i]] = i
    IB[BB[i]] = i

for i in range(N):
    if AA[i] > BB[i]:
        print("No")
        break
else:
    for i in range(1, N):
        if AA[i] <= BB[i-1]:
            print("Yes")
            break
    else:
        for i in range(N):
            if IA[A[i]] == IB[B[i]]:
                print("Yes")
                break
        else:
            c = 1
            i = 0
            P = [-1] * N
            for i in range(N):
                P[IA[A[i]]] = IB[B[i]]
            i = P[0]
            while i != 0:
                i = P[i]
                c += 1
            if c < N:
                print("Yes")
            else:
                print("No")

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
BA = [(b, a) for (b, a) in zip(B, A)]
BA.sort()
B, A = list(map(list, zip(*BA)))  
AI = [(a, i) for (i, a) in enumerate(A)]
AI.sort()
C, P = list(map(list, zip(*AI)))  

def can_divide_cycle(P):
    start = P[1]
    ncycle = 1
    cur = start
    while P[cur] != start:
        cur = P[cur]
        ncycle += 1
    return ncycle < len(P)

vld = all(c <= b for (c, b) in zip(C, B)) \
    and (can_divide_cycle(P) or any(c <= b for (c, b) in zip(C[1:], B[:-1])))
print('Yes' if vld else 'No')

Solution (Python):
def solve(n, a_list, b_list):
    a_list_s = sorted(a_list)
    b_list_s = sorted(b_list)

    
    for i in range(n):
        if a_list_s[i] > b_list_s[i]:
            return "No"

    
    for i in range(n - 1):
        if a_list_s[i + 1] <= b_list_s[i]:
            return "Yes"

    
    
    
    a_list_s_arg = dict()
    b_list_s_org = dict()
    res_list = [0] * n
    for j in range(n):
        a_list_s_arg[a_list_s[j]] = j
    for k in range(n):
        b_list_s_org[b_list[k]] = k
    for i in range(n):
        j = a_list_s_arg[a_list[i]]
        k = b_list_s_org[b_list_s[j]]
        res_list[i] = k

    i = 0
    cnt = 0
    while True:
        i = res_list[i]
        cnt += 1
        if i == 0:
            break
    if cnt == n:
        return "No"
    else:
        return "Yes"


def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    b_list = list(map(int, input().split()))
    res = solve(n, a_list, b_list)
    print(res)


def test():
    assert solve(3, [1, 3, 2], [1, 2, 3]) == "Yes"
    assert solve(3, [1, 2, 3], [2, 2, 2]) == "No"
    assert solve(6, [3, 1, 2, 6, 3, 4], [2, 2, 8, 3, 4, 3]) == "Yes"


if __name__ == "__main__":
    test()
    main()

Solution (Python):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))



N = I()
A = III()
B = III()

def index_sort(A):
    a = []
    for i,x in enumerate(A):
        a.append([i,x])
    asort = sorted(a,key=lambda x: x[1])
    return [asort[i][0] for i in range(len(a))]

A2 = sorted(A)
B2 = sorted(B)

for i in range(N):
    if A2[i]>B2[i]:
        print('No')
        exit()

Aindex = index_sort(A)
Bindex = index_sort(B)
move = [0]*N
for i in range(N):
    move[Aindex[i]] = Bindex[i]

now = 0
for i in range(N):
    if move[now]==0 and i!=N-1:
        print('Yes')
        exit()
    now = move[now]

for i in range(N-1):
    if B2[i]>=A2[i+1]:
        print('Yes')
        exit()

print('No')

Solution (Python):
import sys
import math
MAX_INT = int(10e12)
MIN_INT = -MAX_INT
mod = 998244353
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

def init(n):
  for i in range(N+1):
    par[i] = i
    rnk[i] = 0
def find(x):
  if par[x] == x:
    return x
  else:
    par[x] = find(par[x])
    return par[x]
def unite(x, y):
  x = find(x)
  y = find(y)
  if x == y:
    return
  if (rnk[x] < rnk[y]):
    par[x] = y
  else:
    par[y] = x
    if(rnk[x] == rnk[y]):
      rnk[x] += 1
def same(x, y):
  return find(x) == find(y)

N = I()
a = IL()
b = IL()

par = [0 for i in range(N+1)]
rnk = [0 for i in range(N+1)]
init(N)

adata = []
bdata = []
for i in range(N):
  adata.append([a[i], i])
  bdata.append([b[i], i])
adata.sort()
bdata.sort()



a.sort()
b.sort()
f = 0
for i in range(N-1):
  atmp = a[i]
  btmp = b[i]
  atmp2 = a[i + 1]
  btmp2 = b[i + 1]
  if atmp > btmp:
    print("No")
    exit()
  if atmp2 > btmp2:
    print("No")
    exit()
  if atmp2 <= btmp:
    f = 1
else:
  if f == 1:
    print("Yes")
    exit()

for i in range(N-1):
  atmp, aID = adata[i]
  btmp, bID = bdata[i]
  
  
  
  if aID == bID:
    break
  if same(aID, bID):
    break
  else:
    unite(aID, bID)
else:
  print("No")
  exit()
print("Yes")

Solution (Python):
import numpy as np
N = int(input())
A = np.array([int(x) for x in input().split()])
B = np.array([int(x) for x in input().split()])
I = B.argsort()
A = A[I]
B = B[I]
calc = max(A) <= max(B)
if not calc:
    print("No")
    exit(0)
if any(np.sort(A)[1:] <= B[:-1]):
    print('Yes')
    exit()
p = A.argsort().tolist()
x = 0
c = 0
while True:
    c += 1
    x = p[x]
    if x == 0:
        break
print("Yes" if c != N else "No")

Solution (Python):

import copy
import random
import bisect 
import fractions 
import math
import sys
import bisect

mod = 10**9+7
sys.setrecursionlimit(mod) 

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

N = int(input())
A = LI()
B = LI()


A, B = zip(*sorted(zip(A, B), key=lambda x:x[1]))

A_c = copy.deepcopy(A)

A = sorted(A)
B = sorted(B)

for i in range(N):
    if A[i] > B[i]:
        print("No")
        sys.exit(0)

for i in range(1,N):
    if A[i] < B[i-1]:
        print("Yes")
        sys.exit(0)









si2i = sorted(range(len(A_c)), key=lambda x:A_c[x])
a = 0
count = 0
while True:
    a = si2i[a]
    if a == 0:
        break
    count += 1

if count <= N - 2:
    print("Yes")
else:
    print("No")

Solution (Python):
import sys; readline = sys.stdin.readline
from collections import deque
from collections import Counter
 
N = int(readline())
A = list(map(int, readline().split()))
B = list(map(int, readline().split()))
 
A, B = zip(*sorted(zip(A, B), key=lambda x:x[1]))
 
A_s = sorted(A)
B_s = sorted(B)
 
no = "No"
yes = "Yes"
 
for i in range(len(A_s)):
    if A_s[i] > B_s[i]:
        print(no)
        exit(0)
 
for i in range(1, len(A_s)):
    if A_s[i] < B_s[i - 1]:
        print(yes)
        exit(0)
 
si2i = sorted(range(len(A)), key=lambda x:A[x])
 
a = 0
count = 0
while True:
    a = si2i[a]
    if a == 0:
        break
    count += 1
 
if count <= N - 2:
    print(yes)
else:
    print(no)

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
A_s, B_s = sorted(A), sorted(B)
if any(a > b for a, b in zip(A_s, B_s)):
    print("No")
    quit()
A_idx = [(a, i) for i, a in enumerate(A)]
A_idx.sort()
B = [B[idx] for _, idx in A_idx]
B_idx = [(b, i) for i, b in enumerate(B)]
B_idx.sort()
perm = [i for _, i in B_idx]
cycle_len = 0
used = [False] * N
for i in range(N):
    if used[i]:
        continue
    cnt = 0
    now = i
    while not used[now]:
        used[now] = True
        cnt += 1
        now = perm[now]
    cycle_len += cnt - 1
if cycle_len < N - 1:
    print("Yes")
    quit()

if all(B_idx[i][0] < A_idx[i+1][0] for i in range(N-1)):
    print("No")
else:
    print("Yes")

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

A = sorted(enumerate(A), key=lambda x: x[1])
B = sorted(enumerate(B), key=lambda x: x[1])

flag = False
for i in range(N):
  if A[i][1] > B[i][1]:
    print('No')
    break
  elif i > 0 and A[i][1] <= B[i-1][1]:
    flag = True
else:
  if flag:
    print('Yes')
  else:
    D = dict((a[0], b[0]) for a, b in zip(A, B))
    count = 0
    x = D[0]
    while x != 0:
      x = D[x]
      count += 1
    print('Yes' if count < N-1 else 'No')

Solution (Python):
import sys

N=int(input())

a=list(map(int,input().split()))
b=list(map(int,input().split()))
xx=[[b[i],a[i]] for i in range(N)]
xx.sort()

a=[xx[i][1] for i in range(N)]

aa=[[xx[i][1],i] for i in range(N)]
bb=[[xx[i][0],i] for i in range(N)]

aa.sort()
bb.sort()


for i in range(N):
    if aa[i][0]>bb[i][0]:
        print("No")
        sys.exit()

for i in range(N-1):
    if aa[i][0]==aa[i+1][0] or bb[i][0]==bb[i+1][0]:
        print("Yes")
        sys.exit()

for i in range(1,N):
    if aa[i][0]<=bb[i-1][0]:
        print("Yes")
        sys.exit()


dic={}
for i in range(N):
    dic[aa[i][0]]=i

inij=1
cycle=1
j=inij

while True:
    j=dic[a[j]]
    if j==inij:
        break
    cycle+=1

print("Yes" if cycle<N else "No")

Solution (Python):
class UnionFind:
    
    def __init__(self, n):
        
        self.par = [-1] * n
        
        self.rank = [0] * n

    
    def find(self, x):
        if self.par[x] < 0:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    
    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        
        if x != y:
            
            if self.rank[x] < self.rank[y]:
                x,y = y,x
            
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            
            self.par[x] += self.par[y]
            self.par[y] = x

    
    def same(self, x, y):
        return self.find(x) == self.find(y)

    
    def size(self, x):
        return -self.par[self.find(x)]

n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))


c = sorted(a)
d = sorted(b)
for i in range(n):
    if c[i]>d[i]:
        print('No')
        exit()

for i in range(n-1):
    if c[i+1]<=d[i]:
        print('Yes')
        exit()

for i in range(n-1):
    if c[i]==c[i+1]:
        print('Yes')
        exit()
    if d[i]==d[i+1]:
        print('Yes')
        exit()


e = {}
f = {}
for i in range(n):
    e[c[i]]=d[i]
    f[a[i]]=b[i]


ee = {v: k for k, v in e.items()}
ff = {v: k for k, v in f.items()}

for x in a:
    if e[x]==f[x]:
        print('Yes')
        exit()

z = [0]*n
u = UnionFind(n)

g = {}
for i in range(n):
    g[a[i]]=i

for i in range(n):
    if z[i]==1:
        continue
    x = a[i]
    y = f[x]
    j = i
    while z[j]==0:
        z[j]=1
        x = ee[y]
        y = f[x]
        jj = g[x]
        u.unite(j,jj)
        j=jj

for i in range(n):
    if u.same(0,i)==False:
        print('Yes')
        exit()


print('No')

Solution (Python):
from bisect import bisect_left
N = int(input())
A = [int(i) for i in input().split()]
B = [int(i) for i in input().split()]

def solve() :
    sA, tB = zip(*sorted(zip(A, B)))
    sB = sorted(B)
    
    for i in range(N) :
        if sA[i] > sB[i] :
            return 'No'
            
    for i in range(1, N) :
        if sA[i] <= sB[i-1] :
            return 'Yes'
    
    tB = [bisect_left(sB, tB[i]) for i in range(N)]
    pos = [0] * N
    for k, v in enumerate(tB) :
        pos[v] = k
    
    
    
    count = 0
    for i in range(N) :
        if pos[i] == i :
            continue
        x = pos[i]
        y = tB[i]
        pos[i], pos[y] = pos[y], pos[i]
        tB[i], tB[x] = tB[x], tB[i]
        
        count += 1
        
    
    if count <= N - 2 :
        return 'Yes'
    else :
        return 'No'
    
print(solve())

Solution (Python):
import sys; readline = sys.stdin.readline
from collections import deque
from collections import Counter

N = int(readline())
A = list(map(int, readline().split()))
B = list(map(int, readline().split()))

A, B = zip(*sorted(zip(A, B), key=lambda x:x[1]))

A_s = sorted(A)
B_s = sorted(B)

no = "No"
yes = "Yes"

for i in range(len(A_s)):
    if A_s[i] > B_s[i]:
        print(no)
        exit(0)

for i in range(1, len(A_s)):
    if A_s[i] < B_s[i - 1]:
        print(yes)
        exit(0)

si2i = sorted(range(len(A)), key=lambda x:A[x])

a = 0
count = 0
while True:
    a = si2i[a]
    if a == 0:
        break
    count += 1

if count <= N - 2:
    print(yes)
else:
    print(no)

Solution (Python):



def main():
    N = int(input())
    A = list(map(int, input().split()))
    B = list(map(int, input().split()))

    A2 = sorted(A)
    B2 = sorted(B)
    
    for i in range(0, N):
        if A2[i] > B2[i]:
            print('No')
            return

    for i in range(1, N):
        if A2[i] <= B2[i-1]:
            print('Yes')
            return
            
    B3 = sorted(map(lambda x: (x[1][1], x[0]),
                  enumerate(sorted(map(lambda x: (x[1], x[0]),
                                       enumerate(B))))))
    C = list(map(lambda x: x[1][1], sorted(zip(A, B3))))

    p = 0
    for i in range(N-1):
         p = C[p]
         if p == 0:
             print('Yes')
             return
            
    print('No')


if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 18
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 998244353


def main():
    class UnionFind:
        def __init__(self, n):
            
            
            self.table = [-1] * n
            self.size = [1] * n
            self.group_num = n

        def root(self, x):
            if self.table[x] < 0:
                return x
            else:
                self.table[x] = self.root(self.table[x])
                return self.table[x]

        def is_same(self, x, y):
            return self.root(x) == self.root(y)

        def get_size(self, x):
            r = self.root(x)
            return self.size[r]

        def union(self, x, y):
            r1 = self.root(x)
            r2 = self.root(y)
            if r1 == r2:
                return
            
            d1 = self.table[r1]
            d2 = self.table[r2]
            if d1 <= d2:
                self.table[r2] = r1
                self.size[r1] += self.size[r2]
                if d1 == d2:
                    self.table[r1] -= 1
            else:
                self.table[r1] = r2
                self.size[r2] += self.size[r1]
            self.group_num -= 1


    n = I()
    A = LI()
    B = LI()
    a_sort = sorted([(a, i) for i, a in enumerate(A)])
    b_sort = sorted([(b, i) for i, b in enumerate(B)])
    U = UnionFind(n)
    for i in range(n):
        if a_sort[i][0] > b_sort[i][0]:
            return 'No'
    for j in range(n):
        if j != 0 and a_sort[j][0] <= b_sort[j - 1][0]:
            return 'Yes'
    b_ind_to_a_ind = {}
    for i in range(n):
        b_ind_to_a_ind[b_sort[i][1]] = a_sort[i][1]
    for j in range(n):
        U.union(j, b_ind_to_a_ind[j])
    if U.group_num == 1:
        return 'No'
    else:
        return 'Yes'



print(main())