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



Sample input:
3
1 3 2
1 2 3


Sample output:
Yes

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

def calc(n, A, B):
    count = 0
    for i in range(n-1):#scanA
        if A[i] > B[i]:
            return 'No'
        
        Bmin = B[i]
        swapnum = i
        for j in range(i+1,n):
            if Bmin > B[j]:
                Bmin = B[j]
                swapnum = j            
        
        #A[i+1]<=Bminでなければ入れ替える。
        if A[i+1] > Bmin:
            B[swapnum] = B[i]
            count += 1
        
    if count > n -2:
        return 'No'
    else:
        return 'Yes'

                
#main

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

#print(A)
#print(B)

pair = list(zip(A,B))
pair.sort(key=itemgetter(0))#sortA

A, B = zip(*pair)

ltA = list(A)
ltB = list(B)

#print(ltA)
#print(ltB)

#print(pair)

print(calc(n,ltA,ltB))

Submission status: Wrong Answer

Solution code:
from operator import itemgetter

def calc(n, A, B):
    count = 0
    for i in range(n):#scanA
        Bmin = B[i]
        swapnum = i
        for j in range(i,n):
            if Bmin > B[j]:
                Bmin = B[j]
                swapnum = j            
        
        #最小値がA[i]より小さければ並べ変えは不可能
        if A[i] > Bmin:
            return 'No'
        
        #A[i]<=B[i]でなければ入れ替える。
        if A[i]>B[i]:
            B[swapnum] = B[i]
            count += 1
        
    if count > n -2:
        return 'No'
    else:
        return 'Yes'

                
#main

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

#print(A)
#print(B)

pair = list(zip(A,B))
pair.sort(key=itemgetter(0))#sortA

A, B = zip(*pair)

ltA = list(A)
ltB = list(B)

#print(ltA)
#print(ltB)

#print(pair)

print(calc(n,ltA,ltB))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math
import collections
import operator
import numpy

def check_cycle(i_pair):
    i_orig_sorted_dict = {i_orig: i_sort for i_orig, i_sort in i_pair}
    cycle_dict = {}
    for i_orig, i_sort in i_pair:
        i_current = i_orig
        while True:
            if not i_current in cycle_dict:
                cycle_dict[i_current] = i_orig
            else:
                break
            i_current = i_orig_sorted_dict[i_current]
    return len(numpy.unique(list(cycle_dict.values())))


N, = map(int, sys.stdin.readline().rstrip().split(" "))
As = list(map(int, sys.stdin.readline().rstrip().split(" ")))
Bs = list(map(int, sys.stdin.readline().rstrip().split(" ")))
AB_sorted = list(sorted(zip(As, Bs), key=operator.itemgetter(1)))
Bs = list(map(operator.itemgetter(1), AB_sorted))
As_original = list(map(operator.itemgetter(0), AB_sorted))
A_i_pair = [(i_orig, i_sort) for i_sort, (i_orig, value) in enumerate(sorted(enumerate(As_original), key=operator.itemgetter(1)))]
As.sort()

# print(Bs)
# print(As_original)
# print(As)

A_reserve_dict = {}
last_reserve = None
for i in range(len(As)):
    if As[i] > Bs[i]:
        print("No")
        break
else:
    # if check_cycle(A_i_pair) >= 2:
    #     print("Yes")
    for i in range(len(As)):
        if As[i] == As_original[i]:  # すでに一致してる = 1回減らせる
            print("Yes")
            break
    else:
        for i in range(len(As) - 1):
            if As[i + 1] <= Bs[i]:  # i と i+1はどちらでもいい = 1回減らせる
                print("Yes")
                break
        else:
            print("No")
exit(0)

# Fails
# 1 2 3
# 2 3 1

# Fails
# 1 2 3 4
# 3 4 2 1

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math
import collections
import operator
import numpy

def check_cycle(i_pair):
    i_orig_sorted_dict = {i_orig: i_sort for i_orig, i_sort in i_pair}
    cycle_dict = {}
    for i_orig, i_sort in i_pair:
        i_current = i_orig
        while True:
            if not i_current in cycle_dict:
                cycle_dict[i_current] = i_orig
            else:
                break
            i_current = i_orig_sorted_dict[i_current]
    return len(numpy.unique(list(cycle_dict.values())))


N, = map(int, sys.stdin.readline().rstrip().split(" "))
As = list(map(int, sys.stdin.readline().rstrip().split(" ")))
Bs = list(map(int, sys.stdin.readline().rstrip().split(" ")))
AB_sorted = list(sorted(zip(As, Bs), key=operator.itemgetter(1)))
Bs = list(map(operator.itemgetter(1), AB_sorted))
As_original = list(map(operator.itemgetter(0), AB_sorted))
A_i_pair = [(i_orig, i_sort) for i_sort, (i_orig, value) in enumerate(sorted(enumerate(As_original), key=operator.itemgetter(1)))]
As.sort()

# print(Bs)
# print(As_original)
# print(As)

A_reserve_dict = {}
last_reserve = None
for i in range(len(As)):
    if As[i] > Bs[i]:
        print("No")
        break
else:
    for i in range(len(As)):
        if As[i] == As_original[i]:  # すでに一致してる = 1回減らせる
            print("Yes")
            break
    # if check_cycle(A_i_pair) >= 2:
    #     print("Yes")
    else:
        for i in range(len(As) - 1):
            if As[i + 1] <= Bs[i]:  # i と i+1はどちらでもいい = 1回減らせる
                print("Yes")
                break
        else:
            print("No")
exit(0)

# Fails
# 1 2 3
# 2 3 1

# Fails
# 1 2 3 4
# 3 4 2 1

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math
import collections
import operator
import numpy

def check_cycle(i_pair):
    i_orig_sorted_dict = {i_orig: i_sort for i_orig, i_sort in i_pair}
    cycle_dict = {}
    for i_orig, i_sort in i_pair:
        i_current = i_orig
        while True:
            if not i_current in cycle_dict:
                cycle_dict[i_current] = i_orig
            else:
                break
            i_current = i_orig_sorted_dict[i_current]
    return len(numpy.unique(list(cycle_dict.values())))


N, = map(int, sys.stdin.readline().rstrip().split(" "))
As = list(map(int, sys.stdin.readline().rstrip().split(" ")))
Bs = list(map(int, sys.stdin.readline().rstrip().split(" ")))
AB_sorted = list(sorted(zip(As, Bs), key=operator.itemgetter(1)))
As_original = list(map(operator.itemgetter(0), AB_sorted))
A_i_pair = [(i_orig, i_sort) for i_sort, (i_orig, value) in enumerate(sorted(enumerate(As_original), key=operator.itemgetter(1)))]
As.sort()

A_reserve_dict = {}
last_reserve = None
for i in range(len(As)):
    if As[i] > Bs[i]:
        print("No")
        break
else:
    # for i in range(len(As)):
    #     if As[i] == As_original[i]:  # すでに一致してる = 1回減らせる
    #         print("Yes")
    #         break
    if check_cycle(A_i_pair) >= 2:
        print("Yes")
    else:
        for i in range(len(As) - 1):
            if As[i + 1] <= Bs[i]:  # i と i+1はどちらでもいい = 1回減らせる
                print("Yes")
                break
        else:
            print("No")
exit(0)

# Fails
# 1 2 3
# 2 3 1

# Fails
# 1 2 3 4
# 3 4 2 1

Submission status: Wrong Answer

Solution code:
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.root = [-1]*(n+1)
        self.rnk = [0]*(n+1)
    def Find_Root(self, x):
        if(self.root[x] < 0):
            return x
        else:
            self.root[x] = self.Find_Root(self.root[x])
            return self.root[x]
    def Unite(self, x, y):
        x = self.Find_Root(x)
        y = self.Find_Root(y)
        if(x == y):
            return 
        elif(self.rnk[x] > self.rnk[y]):
            self.root[x] += self.root[y]
            self.root[y] = x
        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if(self.rnk[x] == self.rnk[y]):
                self.rnk[y] += 1
    def isSameGroup(self, x, y):
        return self.Find_Root(x) == self.Find_Root(y)
    def Count(self, x):
        return -self.root[self.Find_Root(x)]
n = int(input())
a_arr = list(map(int,input().split()))
s_a = sorted(a_arr)
b_arr = sorted(list(map(int,input().split())))
swap = False
pb = 0
for a,b in zip(s_a,b_arr):
    if a > b:
        print('No')
        exit()
    if pb >= a:
        swap =  True
    pb = b
if swap:
    print('Yes')
    exit()
uf = UnionFind(n)
print('Yes')

Submission status: Wrong Answer

Solution code:
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.root = [-1]*(n+1)
        self.rnk = [0]*(n+1)
    def Find_Root(self, x):
        if(self.root[x] < 0):
            return x
        else:
            self.root[x] = self.Find_Root(self.root[x])
            return self.root[x]
    def Unite(self, x, y):
        x = self.Find_Root(x)
        y = self.Find_Root(y)
        if(x == y):
            return 
        elif(self.rnk[x] > self.rnk[y]):
            self.root[x] += self.root[y]
            self.root[y] = x
        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if(self.rnk[x] == self.rnk[y]):
                self.rnk[y] += 1
    def isSameGroup(self, x, y):
        return self.Find_Root(x) == self.Find_Root(y)
    def Count(self, x):
        return -self.root[self.Find_Root(x)]
n = int(input())
a_arr = list(map(int,input().split()))
s_a = sorted(a_arr)
b_arr = sorted(list(map(int,input().split())))
swap = False
pb = 0
for a,b in zip(s_a,b_arr):
    if a > b:
        print('No')
        exit()
    if pb >= a:
        swap =  True
    pb = b
if swap:
    print('Yes')
    exit()
print('Yes')

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math
import collections
import operator

# def check_cycle

N, = map(int, sys.stdin.readline().rstrip().split(" "))
As = list(map(int, sys.stdin.readline().rstrip().split(" ")))
Bs = list(map(int, sys.stdin.readline().rstrip().split(" ")))
AB_sorted = list(sorted(zip(As, Bs), key=operator.itemgetter(1)))
As_original = list(map(operator.itemgetter(0), AB_sorted))
As.sort()

A_reserve_dict = {}
last_reserve = None
for i in range(len(As)):
    if As[i] > Bs[i]:
        print("No")
        break
else:
    for i in range(len(As)):
        if As[i] == As_original[i]: # すでに一致してる = 1回減らせる
            print("Yes")
            break
    else:
        for i in range(len(As)-1):
            if As[i+1] <= Bs[i]: # i と i+1はどちらでもいい = 1回減らせる
                print("Yes")
                break
        else:
            print("No")
exit(0)

# Fails
# 1 2 3
# 2 3 1

# Fails
# 1 2 3 4
# 3 4 2 1

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math
import collections

N, = map(int, sys.stdin.readline().rstrip().split(" "))
As = list(map(int, sys.stdin.readline().rstrip().split(" ")))
Bs = list(map(int, sys.stdin.readline().rstrip().split(" ")))
As_original = As
As.sort()
Bs.sort()

A_reserve_dict = {}
last_reserve = None
for i in range(len(As)):
    if As[i] > Bs[i]:
        print("No")
        break
else:
    for i in range(len(As)):
        if As[i] == As_original[i]: # すでに一致してる = 1回減らせる
            print("Yes")
            break
    else:
        for i in range(len(As)-1):
            if As[i+1] <= Bs[i]: # i と i+1はどちらでもいい = 1回減らせる
                print("Yes")
                break
        else:
            print("No")
exit(0)

# Fails
# 1 2 3
# 2 3 1

# Fails
# 1 2 3 4
# 3 4 2 1

Submission status: Wrong Answer

Solution code:
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.root = [-1]*(n+1)
        self.rnk = [0]*(n+1)
    def Find_Root(self, x):
        if(self.root[x] < 0):
            return x
        else:
            self.root[x] = self.Find_Root(self.root[x])
            return self.root[x]
    def Unite(self, x, y):
        x = self.Find_Root(x)
        y = self.Find_Root(y)
        if(x == y):
            return 
        elif(self.rnk[x] > self.rnk[y]):
            self.root[x] += self.root[y]
            self.root[y] = x
        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            if(self.rnk[x] == self.rnk[y]):
                self.rnk[y] += 1
    def isSameGroup(self, x, y):
        return self.Find_Root(x) == self.Find_Root(y)
    def Count(self):
        cnt = 0
        for i in self.root[1:]:
            cnt += 1 if i < 0 else 0
        return cnt
from collections import Counter
n = int(input())
a_arr = list(map(int,input().split()))
s_a = sorted(a_arr)
b_arr = sorted(list(map(int,input().split())))
swap = 'False'
pb = 0
for a,b in zip(s_a,b_arr):
    if a > b:
        print('No')
        exit()
    if pb >= a:
        swap =  'True'
    pb = b
if swap:
    print('Yes')
    exit()
uf = UnionFind(n)
for a,sa in zip(a_arr,s_a):
    if not uf.isSameGroup(a,sa):
        uf.Unite(a,sa)
if uf.Count() > 1:
    print('Yes')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import math
import collections

N, = map(int, sys.stdin.readline().rstrip().split(" "))
As = list(map(int, sys.stdin.readline().rstrip().split(" ")))
Bs = list(map(int, sys.stdin.readline().rstrip().split(" ")))
ABpairs = list(zip(As, Bs))
As.sort()
Bs.sort()
BAdict = {B: A for A, B in zip(As, Bs)}

A_reserve_dict = {}
last_reserve = None
for i in range(len(As)):
    if As[i] > Bs[i]:
        print("No")
        break
else:
    for i in range(len(As)-1):
        if As[i+1] <= Bs[i]: # i と i+1はどちらでもいい = 1回減らせる
            print("Yes")
            break
    else:
        print("No")
exit(0)

# Fails
# 1 2 3
# 2 3 1

# Fails
# 1 2 3 4
# 3 4 2 1

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
xa = sorted([[a[i], i] for i in range(N)], key = lambda x: (x[0], x[1]))
xb = sorted([[b[i], i] for i in range(N)], key = lambda x: (x[0], x[1]))
for i in range(N):
  if xa[i][0] > xb[i][0]:
    print("No")
    exit(0)
class UnionFind():
  def __init__(self, n):
    self.n = n
    self.root = [-1] * (n + 1)
    self.rnk = [0] * (n + 1)

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

  def Unite(self, x, y):
    x = self.Find_Root(x)
    y = self.Find_Root(y)
    if x == y:
      return 
    elif self.rnk[x] > self.rnk[y]:
      self.root[x] += self.root[y]
      self.root[y] = x
    else:
      self.root[y] += self.root[x]
      self.root[x] = y
      if self.rnk[x] == self.rnk[y]:
        self.rnk[y] += 1

  def isSameGroup(self, x, y):
    return self.Find_Root(x) == self.Find_Root(y)

  def Count(self, x):
    return -self.root[self.Find_Root(x)]
uf = UnionFind(N)
c = 0
#print(xa, xb)
for i in range(N):
  if xa[i][0] == xb[i][0]:
    xb[i][1] = xa[i][1]
for i in range(N):
  #print((xa[i][1], xb[i][1]))
  if uf.isSameGroup(xa[i][1], xb[i][1]):
    c += 1
  uf.Unite(xa[i][1], xb[i][1])
if c >= 2:
  print("Yes")
else:
  print("No")
#print(c)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
xa = sorted([(a[i], i) for i in range(N)], key = lambda x: x[0])
xb = sorted([(b[i], i) for i in range(N)], key = lambda x: x[0])
for i in range(N):
  if xa[i][0] > xb[i][0]:
    print("No")
    exit(0)
class UnionFind():
  def __init__(self, n):
    self.n = n
    self.root = [-1] * (n + 1)
    self.rnk = [0] * (n + 1)

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

  def Unite(self, x, y):
    x = self.Find_Root(x)
    y = self.Find_Root(y)
    if x == y:
      return 
    elif self.rnk[x] > self.rnk[y]:
      self.root[x] += self.root[y]
      self.root[y] = x
    else:
      self.root[y] += self.root[x]
      self.root[x] = y
      if self.rnk[x] == self.rnk[y]:
        self.rnk[y] += 1

  def isSameGroup(self, x, y):
    return self.Find_Root(x) == self.Find_Root(y)

  def Count(self, x):
    return -self.root[self.Find_Root(x)]
uf = UnionFind(N)
c = 0
#print(xa, xb)
f = [-1] * N
for i in range(N):
  #print((xa[i][1], xb[i][1]))
  if uf.isSameGroup(xa[i][1], xb[i][1]):
    c += 1
  uf.Unite(xa[i][1], xb[i][1])
if c >= 2:
  print("Yes")
else:
  print("No")

Submission status: Wrong Answer

Solution code:
n = int(input())
a_arr = sorted(list(map(int,input().split())))
b_arr = sorted(list(map(int,input().split())))
for a,b in zip(a_arr,b_arr):
    if a > b:
        print('No')
        exit()
print('Yes')

Submission status: Wrong Answer

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

def count_loop(start, idx_map):
    count = 0
    idx = start
    while True:
        idx = idx_map[idx]
        count += 1
        if idx == 0:
            break
    return count

idx_map = sorted(range(N), key=lambda i: A[i])
A.sort()
B.sort()

check_1 = all(A[i] <= B[i] for i in range(N))
check_2 = any(A[i + 1] <= B[i] for i in range(N - 1))
check_3 = count_loop(0, idx_map) <= N - 2
print("Yes" if (check_1 and (check_2 or check_3)) else "No")

Submission status: Wrong Answer

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

def count_loop(start, idx_map):
    count = 0
    idx = start
    while count != 0 and idx != 0:
        idx = idx_map[idx]
        count += 1
    return count

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

check_1 = all(A[i] <= B[i] for i in range(N))
check_2 = any(A[i + 1] <= B[i] for i in range(N - 1))
check_3 = count_loop(0, idx_map) <= N - 2
print("Yes" if (check_1 and (check_2 or check_3)) else "No")

Submission status: Wrong Answer

Solution code:
N = int(input())
As = list(map(int, input().strip().split()))
Bs = list(map(int, input().strip().split()))


def swap(i, j, lis):
    temp = lis[i]
    lis[i] = lis[j]
    lis[j] = temp


def search(lis, i, most):
    ret = -1
    for j in range(i + 1, len(lis)):
        if most >= lis[j] > ret:
            ret = j
    return ret


for i in range(N):
    for j in range(i + 1, N):
        if Bs[i] > Bs[j]:
            swap(i, j, Bs)
            swap(i, j, As)

# print(As)
# print(Bs)
cnt = 0
exissw = 1
for i in range(N):
    if As[i] > Bs[i]:
        lessMostIdx = search(As, i, Bs[i])
        if lessMostIdx != -1:
            swap(i, lessMostIdx, As)
            cnt += 1
        else:
            exissw = 0
            break
        if cnt > N - 2:
            exissw = 0
        break

if exissw == 0:
    print("No")
else:
    print("Yes")
    # print(As)

Submission status: Wrong Answer

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

def count_loop(start, idx_map):
    count = 1
    idx = start
    while idx != 0:
        idx = idx_map[idx]
        count += 1
    return count

X = list(zip(A, B))
X.sort(key=lambda x: x[1])

C = sorted((x[0], i) for i, x in enumerate(X))
idx_map = [C[i][1] for i in range(N)]
A = sorted(A)
B = sorted(B)

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

if any(A[i + 1] <= B[i] for i in range(N - 1)):
    print("Yes")
    exit()

if count_loop(0, idx_map) < N:
    print("Yes")
    exit()

print("No")

Submission status: Wrong Answer

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

def count_loop(start, idx_map):
    count = 0
    idx = start
    while idx != 0:
        idx = idx_map[idx]
        count += 1
    return count

X = list(zip(A, B))
X.sort(key=lambda x: x[1])

C = sorted((x[0], i) for i, x in enumerate(X))
idx_map = [C[i][1] for i in range(N)]
A = sorted(A)
B = sorted(B)

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

if any(A[i + 1] <= B[i] for i in range(N - 1)):
    print("Yes")
    exit()

if count_loop(0, idx_map) < N:
    print("Yes")
    exit()

print("No")

Submission status: Wrong Answer

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

def count_loop(start, idx_map):
    count = 0
    idx = start
    while idx != 0:
        idx = idx_map[idx]
        count += 1
    return count

X = list(zip(A, B))
X.sort(key=lambda x: x[1])

C = sorted((x[0], i) for i, x in enumerate(X))
idx_map = [C[i][1] for i in range(N)]
A = sorted(A)
B = sorted(B)

check_1 = all(A[i] <= B[i] for i in range(N))
check_2 = any(A[i + 1] <= B[i] for i in range(N - 1))
check_3 = count_loop(0, idx_map) <= N - 2
print("Yes" if (check_1 and (check_2 or check_3)) else "No")

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
xa = sorted([(a[i], i) for i in range(N)], key = lambda x: x[0])
xb = sorted([(b[i], i) for i in range(N)], key = lambda x: x[0])
for i in range(N):
  if xa[i][0] > xb[i][0]:
    print("No")
    exit(0)
class UnionFind():
  def __init__(self, n):
    self.n = n
    self.root = [-1] * (n + 1)
    self.rnk = [0] * (n + 1)

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

  def Unite(self, x, y):
    x = self.Find_Root(x)
    y = self.Find_Root(y)
    if x == y:
      return 
    elif self.rnk[x] > self.rnk[y]:
      self.root[x] += self.root[y]
      self.root[y] = x
    else:
      self.root[y] += self.root[x]
      self.root[x] = y
      if self.rnk[x] == self.rnk[y]:
        self.rnk[y] += 1

  def isSameGroup(self, x, y):
    return self.Find_Root(x) == self.Find_Root(y)

  def Count(self, x):
    return -self.root[self.Find_Root(x)]
uf = UnionFind(N)
c = 0
for i in range(N):
  if uf.isSameGroup(xa[i][1], i):
    c += 1
  uf.Unite(xa[i][1], xb[i][1])
if c >= 2:
  print("Yes")
else:
  print("No")

Submission status: Wrong Answer

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

def count_loop(start, idx_map):
    count = 0
    idx = start
    while idx != 0:
        idx = idx_map[idx]
        count += 1
    return count

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

check_1 = all(A[i] <= B[i] for i in range(N))
check_2 = any(A[i + 1] <= B[i] for i in range(N - 1))
check_3 = count_loop(0, idx_map) <= N - 2
print("Yes" if (check_1 and (check_2 or check_3)) else "No")

Submission status: Wrong Answer

Solution code:
n=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
perm=sorted(range(n), key=lambda i: A[i])

A.sort()
B.sort()

if(any(A[i]>B[i] for i in range(n))):
    print("No")
    exit()

if(any(A[i+1]<=B[i] for i in range(n-1))):
    print("Yes")
    exit()

p=0
cnt=0
while(True):
    cnt+=1
    p=perm[p]
    if(p==0): break
print("Yes" if(cnt<n) else "No")

Submission status: Wrong Answer

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

def count_loop(start, idx_map):
    count = 0
    idx = start
    while idx != 0:
        idx = idx_map[idx]
        count += 1
    return count

idx_map = sorted(range(N), key=lambda i: A[i])
A.sort()
B.sort()

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

if any(A[i + 1] <= B[i] for i in range(N - 1)):
    print("Yes")
    exit()

if count_loop(0, idx_map) < N:
    print("Yes")
    exit()

print("No")

Submission status: Wrong Answer

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

def count_loop(start, idx_map):
    count = 0
    idx = start
    while idx != 0:
        idx = idx_map[idx]
        count += 1
    return count

idx_map = sorted(range(N), key=lambda i: A[i])
A.sort()
B.sort()

check_1 = all(A[i] <= B[i] for i in range(N))
check_2 = any(A[i + 1] <= B[i] for i in range(N - 1))
check_3 = count_loop(0, idx_map) <= N - 2
print("Yes" if (check_1 and (check_2 or check_3)) else "No")

Submission status: Wrong Answer

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

def count_loop(start, idx_map):
    count = 0
    idx = start
    while idx != 0:
        idx = idx_map[idx]
        count += 1
    return count

idx_map = sorted(range(N), key=lambda i: A[i])
A.sort()
B.sort()

check_1 = all(A[i] <= B[i] for i in range(N))
check_2 = any(A[i + 1] <= B[i] for i in range(N - 1))
check_3 = count_loop(0, idx_map) <= N - 2
print("Yes" if check_1 and (check_2 or check_3) else "No")

Submission status: Wrong Answer

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

def count_loop(start, idx_map):
    count = 0
    idx = start
    while idx != 0:
        idx = idx_map[idx]
        count += 1
    return count

idx_map = sorted(range(N), key=lambda i: A[i])
A.sort()
B.sort()

check_1 = all(A[i] <= B[i] for i in range(N))
check_2 = any(A[i + 1] <= B[i] for i in range(N - 1))
check_3 = count_loop(0, idx_map) < N
print("Yes" if check_1 and (check_2 or check_3) else "No")

Submission status: Wrong Answer

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

def count_loop(start, idx_map):
    count = 0
    idx = start
    while idx != 0:
        idx = idx_map[idx]
        count += 1
    return count

idx_map = sorted(range(N), key=lambda i: A[i])
A.sort()
B.sort()

check_1 = all(A[i] <= B[i] for i in range(N))
check_2 = any(A[i + 1] < B[i] for i in range(N - 1))
check_3 = count_loop(0, idx_map) < N
print("Yes" if check_1 and (check_2 or check_3) else "No")

Submission status: Wrong Answer

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

def count_loop(start, idx_map):
    count = 0
    idx = start
    while idx != 0:
        idx = idx_map[idx]
        count += 1
    return count

idx_map = sorted(range(N), key=lambda i: A[i])
A.sort()
B.sort()

check_1 = all(A[i] <= B[i] for i in range(N))
check_2 = any(A[i + 1] < B[i] for i in range(N - 1))
check_3 = count_loop(0, idx_map) <= N - 2
print("Yes" if check_1 and (check_2 or check_3) else "No")

Submission status: Wrong Answer

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

def count_loop(start, idx_map):
    count = 0
    idx = start
    while idx != 0:
        idx = idx_map[idx]
        count += 1
    return count

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

check_1 = all(A[i] <= B[i] for i in range(N))
check_2 = any(A[i + 1] < B[i] for i in range(N - 1))
check_3 = count_loop(0, idx_map) < N
print("Yes" if check_1 and (check_2 or check_3) else "No")

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
xa = sorted([(a[i], i) for i in range(N)], key = lambda x: x[0])
xb = sorted(b)
for i in range(N):
  if xa[i][0] > xb[i]:
    print("No")
    exit(0)
class UnionFind():
  def __init__(self, n):
    self.n = n
    self.root = [-1] * (n + 1)
    self.rnk = [0] * (n + 1)

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

  def Unite(self, x, y):
    x = self.Find_Root(x)
    y = self.Find_Root(y)
    if x == y:
      return 
    elif self.rnk[x] > self.rnk[y]:
      self.root[x] += self.root[y]
      self.root[y] = x
    else:
      self.root[y] += self.root[x]
      self.root[x] = y
      if self.rnk[x] == self.rnk[y]:
        self.rnk[y] += 1

  def isSameGroup(self, x, y):
    return self.Find_Root(x) == self.Find_Root(y)

  def Count(self, x):
    return -self.root[self.Find_Root(x)]
uf = UnionFind(N)
c = 0
for i in range(N):
  if uf.isSameGroup(xa[i][1], i):
    c += 1
  uf.Unite(xa[i][1], i)
if c >= 2:
  print("Yes")
else:
  print("No")

Submission status: Wrong Answer

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

def count_loop(start, idx_map):
    count = 0
    idx = start
    while idx != 0:
        idx = idx_map[idx]
        count += 1
    return count

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

check_1 = all(A[i] <= B[i] for i in range(N))
check_2 = any(A[i + 1] <= B[i] for i in range(N - 1))
check_3 = count_loop(0, idx_map) < N
print("Yes" if check_1 and (check_2 or check_3) else "No")

Submission status: Wrong Answer

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

def count_loop(start, idx_map):
    count = 0
    idx = start
    while idx != 0:
        idx = idx_map[idx]
        count += 1
    return count

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

check_1 = all(A[i] <= B[i] for i in range(N))
check_2 = any(A[i + 1] <= B[i] for i in range(N - 1))
check_3 = count_loop(0, idx_map) <= N - 2
print("Yes" if check_1 and (check_2 or check_3) else "No")

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
viable = False
for i in range(n):
  if a[i] <= b[i]:
    viable = True
a.sort()
b.sort()
for i in range(n):
  if a[i] > b[i]:
    viable = False
if viable:
  print("Yes")
else:
  print("No")

Submission status: Wrong Answer

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

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

check_1 = all(A[i] <= B[i] for i in range(N))
check_2 = any(A[i + 1] <= B[i] for i in range(N - 1))
check_3 = N <= 3
print("Yes" if check_1 and (check_2 or check_3) else "No")

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
xa = sorted(a)
xb = sorted(b)
for i in range(N):
  if xa[i] > xb[i]:
    print("No")
    exit(0)
print("Yes")

Submission status: Wrong Answer

Solution code:
n=int(input())
a=sorted(list(map(int,input().split())))
b=sorted(list(map(int,input().split())))

for i in range(len(a)):
  if a[i]>b[i]:
    print("No")
    break
else:
  print("Yes")