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):
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a_ = sorted(a)
b_ = sorted(b)
f = sum(b_[i]<a_[i] for i in range(n))
if f:
    print("No")
    exit(0)
g = sum(b_[i-1]>=a_[i] for i in range(1,n))
if g:
    print("Yes")
    exit(0)
da = dict()
for i in range(n):
    da[a_[i]] = i
ca = list(map(lambda x:da[x],a))
db = dict()
for i in range(n):
    db[b_[i]] = i
cb = list(map(lambda x:db[x],b))
cb2 = [None]*n
for i in range(n):
    cb2[ca[i]] = cb[i]
cnt = 1
c = cb2[0]
while c:
    c = cb2[c]
    cnt += 1
if cnt ==  n:
    print("No")
else:
    print("Yes")

Solution (Python):
from operator import itemgetter

def solve():
    N = int(input())
    As = list(map(int, input().split()))
    Bs = list(map(int, input().split()))

    Cs = sorted(As)
    Ds = sorted(Bs)

    for A, B in zip(Cs, Ds):
        if A > B:
            return False

    for i in range(N-1):
        if Cs[i] <= Ds[i+1] and Cs[i+1] <= Ds[i]:
            return True

    ABs = [(A, B) for A, B in zip(As, Bs)]
    ABs.sort(key=itemgetter(1))

    As = [A for A, B in ABs]
    Bs = [B for A, B in ABs]
    iAs = list(range(N))
    iAs.sort(key=lambda iA: As[iA])
    convs = dict()
    for i in range(N):
        convs[As[iAs[i]]] = i

    for i in range(N-2):
        if As[i] <= Bs[i]:
            return True
        X, Y = As[i], As[iAs[i]]
        As[i], As[iAs[i]] = As[iAs[i]], As[i]
        iAs[convs[X]], iAs[convs[Y]] = iAs[convs[Y]], iAs[convs[X]]

    if As[N-2] <= Bs[N-2] and As[N-1] <= Bs[N-1]:
        return True
    else:
        return False

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

Solution (Python):
import bisect
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7



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

A = np.array(A, dtype=int)
B = np.array(B, dtype=int)


idx = B.argsort()
A = A[idx]
B = B[idx]

positions = np.searchsorted(B, A, side='left')

heap = [(a, -p) for p, a in enumerate(positions)]
heapq.heapify(heap)



cnt = 0
for i in range(N):
    a = positions[i]
    if a <= i:
        continue
    cnt += 1
    while heap and -heap[0][1] < i:
        heapq.heappop(heap)
        new_a, p = heap[0]
        p *= -1
        if positions[p] != new_a:
            continue
    new_a, p = heapq.heappop(heap)
    p *= -1
    positions[i] = new_a
    positions[p] = a
    heapq.heappush(heap, (a, -p))
    



if cnt <= N - 2 and np.all(positions <= np.arange(N)):
    print('Yes')
else:
    print('No')

Solution (Python):
import bisect,sys
input = sys.stdin.readline

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

aa = sorted(a)
bb = sorted(b)

for i in range(n):
    if aa[i] > bb[i]:
        print('No')
        exit()
    
for i in range(1,n):
    if aa[i] <= bb[i-1]:
        print('Yes')
        exit()

dg = 10**10

ab = []
for i in range(n):
    ab.append(b[i]*dg+a[i])
ab.sort()

a = []
for e in ab:
    a.append(e%dg)


idx = {}
for i in range(n):
    k = bisect.bisect_left(aa,a[i])
    idx[k] = i


for i in range(n-1):
    if a[i] == aa[i]:
        print('Yes')
        exit()
    a[idx[i]] = a[i]
    k = bisect.bisect_left(aa,a[i])
    idx[k] = idx[i]

print('No')

Solution (Python):
N=int(input())
A1=[int(s) for s in input().split()]
B1=[int(s) for s in input().split()]
A2=[(A1[i],i) for i in range(N)]
B2=[(B1[i],i) for i in range(N)]
A=sorted(A2)
B=sorted(B2)


for i in range(N):
  if B[i][0]<A[i][0]:
    print("No")
    break
else:
  for i in range(N-1):
    if B[i][0]>=A[i+1][0]:
      print("Yes")
      break
  else:
    C=[(A[i][1],B[i][1]) for i in range(N)]
    C.sort()

    k=0
    for i in range(N-1):
      k=C[k][1]
      if k==0:
        print("Yes")
        break
    else:
      print("No")

Solution (Python):
import numpy as np
yes, no = "Yes", "No"
n = int(input())
a = np.array(list(map(int, input().split())))
b = np.array(list(map(int, input().split())))
sb = np.sort(b)
sa = np.sort(a)
sbi = np.argsort(b)
sai = np.argsort(a[sbi])
if np.any(sb < sa):
    print(no)
    exit(0)

if np.any(sb[:-1] >= sa[1:]):
    print(yes)
    exit(0)

count = 1
i = 0
while True:
    j = sai[i]
    if j == 0:
        break
    i = j
    count += 1
if count == n:
    print(no)
else:
    print(yes)

Solution (Python):
from numpy import argsort
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A_sorted = sorted(A)
B_sorted = sorted(B) + [0]
for i, (a, b) in enumerate(zip(A_sorted, B_sorted)):
    if a > b:
        print("No")
        exit()
for i, (a, b) in enumerate(zip(A_sorted, B_sorted)):
    if a <= B_sorted[i-1]:
        print("Yes")
        exit()

A_rank = list(argsort(argsort(A)))
B_rank = list(argsort(argsort(B)))
B_rank_inv = [2*N] * N
for i, b in enumerate(B_rank):
    B_rank_inv[b] = i
a = 0
for i in range(N-1):
    a = A_rank[B_rank_inv[a]]
    if a==0:
        print("Yes")
        exit()
print("No")

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np

N = int(readline())
A = np.array(readline().split(),np.int32)
B = np.array(readline().split(),np.int32)


I = B.argsort(); B = B[I]; A = A[I]

Asorted = np.msort(A)

if (Asorted > B).any():
    print('No')
    exit()




if any(Asorted[1:] <= B[:-1]):
    print('Yes')
    exit()



perm = A.argsort().tolist()

x = 0
period = 0
while True:
    period += 1
    x = perm[x]
    if x == 0:
        break

print('Yes' if period != N else 'No')

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


A = sorted(a)
B = sorted(b)


check = 1
for i in range(n):
  if A[i] > B[i]:
    check = 0
    break
for i in range(n-1):
  if A[i+1] <= B[i]:
    check *= 2
    
    break

for i in range(n-1):
  if A[i] == A[i+1]:
    check *= 2
    break
  if B[i] == B[i+1]:
    check *= 2
    break
c = []
for i in range(n):
  c.append([a[i],i])
c.sort()

d = [-1 for _ in range(n)]
for i in range(n):
  d[c[i][1]] = i

count = 0
now = 0
while True:
  count += 1
  now = d[now]
  if now == 0:
    break

if check == 0:
  print('No')
elif check > 1:
  print('Yes')
elif count == n:
  print('No')
else:
  print('Yes')

Solution (Python):
N=int(input())
A=[int(i) for i in input().split()]
B=[int(i) for i in input().split()]
X=sorted([(A[i],B[i]) for i in range(N)],key=lambda x:x[1])
A=[X[i][0] for i in range(N)]
B=[X[i][1] for i in range(N)]
C=sorted([(A[i],i) for i in range(N)],key=lambda x:x[0])
for i in range(N):
    if C[i][0]>B[i]:
        print("No")
        exit()
if len(set(A))<N:
    print("Yes")
    exit()
Y=[0 for i in range(N)]
j=0
for i in range(N):
    Y[j]=1
    j=C[j][1]
    
if sum(Y)==N:
    
    
    for i in range(N-1):
        if C[i+1][0]<=X[i][1]:
            print("Yes")
            exit()
    print("No")
else:
    print("Yes")

Solution (Python):

from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())

class UnionFind:
    def __init__(self,N): 
        self.table = [i for i in range(N)]    
        self.rank  = [1 for i in range(N)]    
        self.size  = [1 for i in range(N)]    

    def Find(self,x):    
        if self.table[x] == x:
            return x
        else:
            self.table[x] = self.Find(self.table[x]) 
            self.size[x] = self.size[self.table[x]]
            return self.table[x]

    def Unite(self,x,y): 
        x,y = self.Find(x), self.Find(y)
        sx,sy = self.Size(x), self.Size(y)
        if x == y: return
        if self.rank[x] > self.rank[y]:
            self.table[y] = x
            self.size[x] = sx + sy
        else:
            self.table[x] = y
            self.size[y] = sx + sy
            if self.rank[x] == self.rank[y]:
                self.rank[y] += 1

    def Check(self,x,y):
        return self.Find(x) == self.Find(y)

    def Size(self,x):
        return self.size[self.Find(x)]


N = inp()
AA = inpl()
BB = inpl()

BA = []
for i in range(N):
    BA.append((BB[i],AA[i]))


BA.sort()
act_AA = [(A,ai) for ai,(B,A) in enumerate(BA)]

sorted_AA = sorted(act_AA)

UF = UnionFind(N)



for i,(A,ai) in enumerate(sorted_AA):
    UF.Unite(i,ai)


if UF.Size(0) != N:
    for i in range(N):
        A = sorted_AA[i][0]
        B = BA[i][0]
        if A <= B:
            continue
        else:
            print("No")
            sys.exit()
    print("Yes")
else:

    for i in range(N):
        A = sorted_AA[i][0]
        B = BA[i][0]
        if A <= B:
            continue
        else:
            print("No")
            sys.exit()

    for i in range(N-1):
        A1 = sorted_AA[i][0]
        A2 = sorted_AA[i+1][0]
        B1 = BA[i][0]
        B2 = BA[i+1][0]
        if A1 <= B2 and A2 <= B1:
            print("Yes")
            sys.exit()

    print("No")