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:
import sys
from collections import Counter, deque, defaultdict, OrderedDict
from math import factorial
from fractions import Fraction
import heapq, bisect
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))


n = INT()
a = LIST()
b = LIST()
dic = []

for i in range(n):
    dic.append([a[i], b[i]])

dic = sorted(dic, key = lambda x:x[1])
for i in range(n):
    b[i] = dic[i][1]
for i in range(n):
    dic[i][1] = i
dic = sorted(dic, key = lambda x:x[0])
ind = [0]*n
for i in range(n):
    a[i] = dic[i][0]
    ind[i] = dic[i][1]

for i in range(n):
    if a[i] > b[i]:
        print('No')
        sys.exit()

if ind[0] == n-1:
    fl = True
    for i in range(1, n):
        if ind[i] != i-1:
            fl = False
    if fl:
        print('No')
        sys.exit()
if ind[n-1] == 0:
    fl = True
    for i in range(n-1):
        if ind[i] != i+1:
            fl = False
    if fl:
        print('No')
        sys.exit()

print('Yes')

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter, deque, defaultdict, OrderedDict
from math import factorial
from fractions import Fraction
import heapq, bisect
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))


n = INT()
a = LIST()
b = LIST()
dic = []

for i in range(n):
    dic.append([a[i], b[i]])

dic = sorted(dic, key = lambda x:x[1])
for i in range(n):
    b[i] = dic[i][1]
for i in range(n):
    dic[i] = [dic[i][0], i]
dic = sorted(dic, key = lambda x:x[0])
ind = [0]*n
for i in range(n):
    a[i] = dic[i][0]
    ind[i] = dic[i][1]

for i in range(n):
    if a[i] > b[i]:
        print('No')
        sys.exit()

if ind[0] == n-1:
    fl = True
    for i in range(1, n):
        if ind[i] != i-1:
            fl = False
    if fl:
        print('No')
        sys.exit()
if ind[n-1] == 0:
    fl = True
    for i in range(n-1):
        if ind[i] != i+1:
            fl = False
    if fl:
        print('No')
        sys.exit()

print('Yes')

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter, deque, defaultdict, OrderedDict
from math import factorial
from fractions import Fraction
import heapq, bisect
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))


n = INT()
a = LIST()
b = LIST()
dic = []

for i in range(n):
    dic.append([a[i], b[i]])

dic = sorted(dic, key = lambda x:x[1])
for i in range(n):
    b[i] = dic[i][1]
for i in range(n):
    dic[i] = [dic[i][0], i]
dic = sorted(dic, key = lambda x:x[0])
ind = [0]*n
for i in range(n):
    a[i] = dic[i][0]
    ind[i] = dic[i][1]

for i in range(n):
    if a[i] > b[i]:
        print('No')
        sys.exit()

if ind[0] == n-1:
    fl = True
    for i in range(1, n):
        if ind[i] != i-1:
            fl = False
    if fl:
        print('No')
        sys.exit()
if ind[n-1] == 0:
    fl = True
    for i in range(n-1):
        if ind[i] == i+1:
            fl = False
    if fl:
        print('No')
        sys.exit()

print('Yes')

Submission status: Wrong Answer

Solution code:
class UnionFindTree():
    def __init__(self, N):
        self.N = N
        self.__parent_of = [None] * N
        self.__rank_of = [0] * N
        self.__size = [1] * N

    def root(self, value):
        if self.__parent_of[value] is None:
            return value
        else:
            self.__parent_of[value] = self.root(self.__parent_of[value])
            return self.__parent_of[value]

    def unite(self, a, b):
        r1 = self.root(a)
        r2 = self.root(b)
        if r1 != r2:
            if self.__rank_of[r1] < self.__rank_of[r2]:
                self.__parent_of[r1] = r2
                self.__size[r2] += self.__size[r1]
            else:
                self.__parent_of[r2] = r1
                self.__size[r1] += self.__size[r2]
                if self.__rank_of[r1] == self.__rank_of[r2]:
                    self.__rank_of[r1] += 1

    def is_same(self, a, b):
        return self.root(a) == self.root(b)

    def size(self, a):
        return self.__size[self.root(a)]

    def groups(self):
        groups = {}
        for k in range(self.N):
            r = self.root(k)
            if r not in groups:
                groups[r] = []
            groups[r].append(k)
        return [groups[x] for x in groups]


def main():
    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")
            return
    for i in range(N-1):
        if AA[i+1] <= BB[i]:
            print("Yes")
            return
    uft = UnionFindTree(N)
    rev = {AA[i]: i for i in range(N)}
    for i in range(N):
        uft.unite(i, rev[A[i]])
    if len(uft.groups()) == 1:
        print("No")
    else:
        print("Yes")


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ba = sorted((y, x) for x, y in zip(a, b))
a = [x[1] for x in ba]
b = [x[0] for x in ba]

pa = sorted((x, i) for i, x in enumerate(a))
pb = sorted((x, i) for i, x in enumerate(b))
for i in range(n):
    if pa[i][0] > pb[i][0]:
        print('No')
        exit()

rep = [pa[i][1] for i in range(n)]
used = [False] * n
c = 0
for i in range(n):
    if used[i]:
        break
    c += 1
    p = rep[i]
    while True:
        if used[p]:
            break
        used[p] = True
        p = rep[p]
if c > 1:
    print('Yes')
    exit()

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

print('No')

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
inf = float('inf')

# 一点更新　区間取得　1-indexed
class SegmentTree:
    def __init__(self, n):
        tmp = 1
        while tmp < n:
            tmp <<= 1
        # self.N : 完全二分木の葉の数(N以上の最小の2べき)
        self.N = tmp
        # 2*self.N - 1 : 完全二分木のノード数
        # 1-indexedで管理するので１つ余分に設ける
        self.Data = [(inf, -1) for _ in range(2*self.N)]

    # k (0-indexed)
    def update(self, k, x):
        #　インデックスを補正
        k += self.N
        # 指定ノードを更新
        self.Data[k] = x
        # kを含む区間ノードを更新していく
        while k > 1:
            k >>= 1
            a, i = self.Data[2*k]
            b, j = self.Data[2*k+1]
            if a == b:
                self.Data[k] = max([a, i], [b, j])
            else:
                self.Data[k] = min([a, i], [b, j])

    # [l, r) (0-indexed)
    def query(self, l, r):
        #　インデックスを補正
        l += self.N
        r += self.N
        # 返り値を初期化
        res = (inf, -1)
        while l < r:
            # 右側なら右側を更新 右上に行く
            # 左側なら更新しない　上に行く
            if l & 1:
                a, i = res
                b, j = self.Data[l]
                if a == b:
                    res = max([a, i], [b, j])
                else:
                    res = min([a, i], [b, j])
                l += 1
            # 右側なら左側を更新　上に行く
            # 左側なら更新しない 上に行く
            if r & 1:
                r -= 1
                a, i = res
                b, j = self.Data[r]
                if a == b:
                    res = max([a, i], [b, j])
                else:
                    res = min([a, i], [b, j])
            l >>= 1; r >>= 1
        return res

    def value(self, k):
        return st.query(k, k+1)

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

C = [(a, b) for a, b in zip(A, B)]
C = sorted(C, key=lambda x:(x[1], x[0]))
A = [c[0] for c in C]
B = [c[1] for c in C]

st = SegmentTree(N)
for i, a in enumerate(A):
    st.update(i, (a, i))

ans = 0
for i, b in enumerate(B):
    a = st.value(i)[0]
    if a <= b:
        pass
    else:
        # 見ているところより右から最小のものを選んでくる
        v, vi = st.query(i, N)
        if not v <= b:
            print("No")
            exit()
        st.update(i, (v, i))
        st.update(vi, (a, vi))
        ans += 1

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

Submission status: Wrong Answer

Solution code:
def main():
    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")
            return
    for i in range(N-1):
        if AA[i+1] <= BB[i]:
            print("Yes")
            return
    for i in range(N):
        if AA[i] == A[i]:
            print("Yes")
            return
    print("No")


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

pa = sorted((x, i) for i, x in enumerate(a))
pb = sorted((x, i) for i, x in enumerate(b))
for i in range(n):
    if pa[i][0] > pb[i][0]:
        print('No')
        exit()

rep = [pa[i][1] for i in range(n)]
used = [False] * n
c = 0
for i in range(n):
    if used[i]:
        break
    c += 1
    p = rep[i]
    while True:
        if used[p]:
            break
        used[p] = True
        p = rep[p]
if c > 1:
    print('Yes')
    exit()

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

print('No')

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

pa = sorted((x, i) for i, x in enumerate(a))
pb = sorted((x, i) for i, x in enumerate(b))
for i in range(n):
    if pa[i][0] > pb[i][0]:
        print('No')
        exit()

rep = [pa[i][1] for i in range(n)]
used = [False] * n
c = 0
for i in range(n):
    if used[i]:
        break
    c += 1
    p = rep[i]
    while True:
        if used[p]:
            break
        used[p] = True
        p = rep[p]
if c >= 1:
    print('Yes')
    exit()

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

print('No')

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
inf = float('inf')

# 一点更新　区間取得　1-indexed
class SegmentTree:
    def __init__(self, n):
        tmp = 1
        while tmp < n:
            tmp <<= 1
        # self.N : 完全二分木の葉の数(N以上の最小の2べき)
        self.N = tmp
        # 2*self.N - 1 : 完全二分木のノード数
        # 1-indexedで管理するので１つ余分に設ける
        self.Data = [(inf, -1) for _ in range(2*self.N)]

    # k (0-indexed)
    def update(self, k, x):
        #　インデックスを補正
        k += self.N
        # 指定ノードを更新
        self.Data[k] = x
        # kを含む区間ノードを更新していく
        while k > 1:
            k >>= 1
            self.Data[k] = min([self.Data[2*k], self.Data[2*k+1]])

    # [l, r) (0-indexed)
    def query(self, l, r):
        #　インデックスを補正
        l += self.N
        r += self.N
        # 返り値を初期化
        res = (inf, -1)
        while l < r:
            # 右側なら右側を更新 右上に行く
            # 左側なら更新しない　上に行く
            if l & 1:
                res = min([res, self. Data[l]])
                l += 1
            # 右側なら左側を更新　上に行く
            # 左側なら更新しない 上に行く
            if r & 1:
                r -= 1
                res = min([res, self.Data[r]])
            l >>= 1; r >>= 1
        return res

    def value(self, k):
        return st.query(k, k+1)

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

C = [(a, b) for a, b in zip(A, B)]
C = sorted(C, key=lambda x:(x[1], x[0]))
A = [c[0] for c in C]
B = [c[1] for c in C]

st = SegmentTree(N)
for i, a in enumerate(A):
    st.update(i, (a, i))

ans = 0
for i, b in enumerate(B):
    a = st.value(i)[0]
    if a <= b:
        pass
    else:
        # 見ているところより右から最小のものを選んでくる
        v, vi = st.query(i, N)
        if not v <= b:
            print("No")
            exit()
        st.update(i, (v, i))
        st.update(vi, (a, vi))
        ans += 1

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

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
b=[int(i) for i in 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=[[a[i],i] for i in range(n)]
ai.sort(key=lambda x:x[0])

flag1=True
for i in range(n):
  if ai[i][0]>b[i]:
    flag1=False
    break
    
idx=ai[0][1]
cnt=0
while idx!=0:
  idx=ai[idx][1]
  cnt+=1

if cnt==n:
  flag2=False
  for i in range(n-1):
    if b[i]>=ai[i+1][0]:
      flag2=True
      break
else:
  flag2=True
  
if flag1 and flag2:
  ans='Yes'
else:
  ans='No'
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
b=[int(i) for i in 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=[[a[i],i] for i in range(n)]
ai.sort(key=lambda x:x[0])

flag1=True
for i in range(n):
  if ai[i][0]>b[i]:
    flag1=False
    break
    
idx=ai[0][1]
cnt=0
while idx!=0:
  idx=ai[idx][1]
  cnt+=1

if cnt==n:
  flag2=False
  for i in range(n-1):
    if b[i]>=ai[i+1][0]:
      flag=True
      break
else:
  flag2=True
  
if flag1 and flag2:
  ans='Yes'
else:
  ans='No'
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
b=[int(i) for i in 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=[[a[i],i] for i in range(n)]
ai.sort(key=lambda x:x[0])

flag1=True
for i in range(n):
  if ai[i][0]>b[i]:
    flag1=False
    break
    
idx=ai[0][1]
cnt=0
while idx!=0:
  idx=ai[idx][1]
  cnt+=1

if cnt==n:
  flag2=False
else:
  flag2=True
  
if flag1 and flag2:
  ans='Yes'
else:
  ans='No'
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
inf = float('inf')

# 一点更新　区間取得　1-indexed
class SegmentTree:
    def __init__(self, n):
        tmp = 1
        while tmp < n:
            tmp <<= 1
        # self.N : 完全二分木の葉の数(N以上の最小の2べき)
        self.N = tmp
        # 2*self.N - 1 : 完全二分木のノード数
        # 1-indexedで管理するので１つ余分に設ける
        self.Data = [(inf, -1) for _ in range(2*self.N)]

    # k (0-indexed)
    def update(self, k, x):
        #　インデックスを補正
        k += self.N
        # 指定ノードを更新
        self.Data[k] = x
        # kを含む区間ノードを更新していく
        while k > 1:
            k >>= 1
            self.Data[k] = min([self.Data[2*k], self.Data[2*k+1]])

    # [l, r) (0-indexed)
    def query(self, l, r):
        #　インデックスを補正
        l += self.N
        r += self.N
        # 返り値を初期化
        res = (inf, -1)
        while l < r:
            # 右側なら右側を更新 右上に行く
            # 左側なら更新しない　上に行く
            if l & 1:
                res = min([res, self. Data[l]])
                l += 1
            # 右側なら左側を更新　上に行く
            # 左側なら更新しない 上に行く
            if r & 1:
                r -= 1
                res = min([res, self.Data[r]])
            l >>= 1; r >>= 1
        return res

    def value(self, k):
        return st.query(k, k+1)

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

C = [(a, b) for a, b in zip(A, B)]
C = sorted(C, key=lambda x:(x[1], x[0]))
A = [c[0] for c in C]
B = [c[1] for c in C]

st = SegmentTree(N)
for i, a in enumerate(A):
    st.update(i, (a, i))

ans = 0
for i, b in enumerate(B):
    a = st.value(i)[0]
    if a <= b:
        pass
    else:
        # 見ているところより右から最小のものを選んでくる
        v, vi = st.query(i, N)
        if not v <= b:
            print("No")
            exit()
        st.update(i, (vi, i))
        st.update(vi, (b, vi))
        ans += 1

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
inf = float('inf')

# 一点更新　区間取得　1-indexed
class SegmentTree:
    def __init__(self, n):
        tmp = 1
        while tmp < n:
            tmp <<= 1
        # self.N : 完全二分木の葉の数(N以上の最小の2べき)
        self.N = tmp
        # 2*self.N - 1 : 完全二分木のノード数
        # 1-indexedで管理するので１つ余分に設ける
        self.Data = [[inf, -1] for _ in range(2*self.N)]

    # k (0-indexed)
    def update(self, k, x):
        #　インデックスを補正
        k += self.N
        # 指定ノードを更新
        self.Data[k] = x
        # kを含む区間ノードを更新していく
        while k > 1:
            k >>= 1
            self.Data[k] = min([self.Data[2*k], self.Data[2*k+1]])

    # [l, r) (0-indexed)
    def query(self, l, r):
        #　インデックスを補正
        l += self.N
        r += self.N
        # 返り値を初期化
        res = [inf, -1]
        while l < r:
            # 右側なら右側を更新 右上に行く
            # 左側なら更新しない　上に行く
            if l & 1:
                res = min([res, self. Data[l]])
                l += 1
            # 右側なら左側を更新　上に行く
            # 左側なら更新しない 上に行く
            if r & 1:
                r -= 1
                res = min([res, self.Data[r]])
            l >>= 1; r >>= 1
        return res

    def value(self, k):
        return st.query(k, k+1)

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

C = [(a, b) for a, b in zip(A, B)]
C = sorted(C, key=lambda x:(x[1], x[0]))
A = [c[0] for c in C]
B = [c[1] for c in C]

st = SegmentTree(N)
for i, a in enumerate(A):
    st.update(i, [a, i])

ans = 0
for i, b in enumerate(B):
    a = st.value(i)[0]
    if a <= b:
        pass
    else:
        # 見ているところより右から最小のものを選んでくる
        v, vi = st.query(i, N)
        if not v <= b:
            print("No")
            exit()
        st.update(i, [vi, i])
        st.update(vi, [b, vi])
        ans += 1

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(s) for s in input().split()]
B = [int(s) for s in input().split()]

if max(A) > max(B):
  print('No')
  exit()

A.sort()
B.sort()
for i in range(N):
  if A[i] > B[i]:
    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()))
A=sorted(A)
B=sorted(B)
for i in range(N):
  if A[i]>B[i]:
    print('No')
    exit()
for i in range(N-1):
  if A[i+1]<= B[i]:
    print('Yes')
    exit()
print('No')

Submission status: Wrong Answer

Solution code:
import sys
import copy

n = int(input())
alis = list(map(int, input().split()))
blis = list(map(int, input().split()))
alist = copy.deepcopy(alis)
blist = copy.deepcopy(blis)
alis.sort()
blis.sort()
for i in range(n):
    if alis[i] > blis[i]:
        print("No")
        sys.exit()


for i in range(n-1):
    if alist[i+1] > blist[i]:
        pass
    else:
        print("Yes")
        sys.exit()
print("No")

Submission status: Wrong Answer

Solution code:
import sys
import copy

n = int(input())
alis = list(map(int, input().split()))
blis = list(map(int, input().split()))
alist = copy.deepcopy(alis)
blist = copy.deepcopy(blis)
alis.sort()
blis.sort()
for i in range(n):
    if alis[i] > blis[i]:
        print("No")
        sys.exit()


for i in range(n-1):
    if alist[i+1] >= blist[i]:
        pass
    else:
        print("Yes")
        sys.exit()
print("No")

Submission status: Wrong Answer

Solution code:
import sys
import copy

n = int(input())
alis = list(map(int, input().split()))
blis = list(map(int, input().split()))
alis.sort()
blis.sort()
for i in range(n):
    if alis[i] > blis[i]:
        print("No")
        sys.exit()


for i in range(n-1):
    if alis[i+1] >= blis[i]:
        pass
    else:
        print("Yes")
        sys.exit()
print("No")

Submission status: Wrong Answer

Solution code:
import sys
import copy

n = int(input())
alis = list(map(int, input().split()))
blis = list(map(int, input().split()))
alist = copy.deepcopy(alis)
blist = copy.deepcopy(blis)
alis.sort()
blis.sort()
for i in range(n):
    if alis[i] > blis[i]:
        print("No")
        sys.exit()


for i in range(n-1):
    if alis[i+1] >= blis[i]:
        pass
    else:
        print("Yes")
        sys.exit()
print("No")

Submission status: Wrong Answer

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

sA = sorted(A)
sB = sorted(B)
# 必要条件
if not all(a <= b for a, b in zip(sA, sB)):
    print("No")
    exit()

C = [(a, b) for a, b in zip(A, B)]
C = sorted(C, key=lambda x:(x[1], x[0]))
A = [c[0] for c in C]

# 操作にN-1回かかる
if (A[1:] + A[:1]) == sA:
    print("No")
else:
    print("Yes")

Submission status: Wrong Answer

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

sA = sorted(A)
sB = sorted(B)
# 必要条件
if not all(a <= b for a, b in zip(sA, sB)):
    print("No")
    exit()

C = [(a, b) for a, b in zip(A, B)]
C = sorted(C, key=lambda x:(x[1]))
A = [c[0] for c in C]

# 操作にN-1回かかる
if (A[1:] + A[:1]) == sorted(A):
    print("No")
else:
    print("Yes")

Submission status: Wrong Answer

Solution code:
import sys
import copy

n = int(input())
alis = list(map(int, input().split()))
blis = list(map(int, input().split()))
alist = copy.deepcopy(alis)
blist = copy.deepcopy(blis)
alis.sort()
blis.sort()
for i in range(n):
    if alis[i] > blis[i]:
        print("No")
        sys.exit()


for i in range(n-1):
    if alis[i+1] > blis[i]:
        pass
    else:
        print("Yes")
        sys.exit()
print("No")

Submission status: Wrong Answer

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

if all(a <= b for a, b in zip(A, B)):
    ans = "Yes"
else:
    ans = "No"

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
n = int(input())
alis = list(map(int, input().split()))
blis = list(map(int, input().split()))
alis.sort()
blis.sort()
for i in range(n):
    if alis[i] > blis[i]:
        print("No")
        sys.exit()


for i in range(n-1):
    if alis[i+1] > blis[i]:
        pass
    else:
        print("Yes")
        sys.exit()
print("No")

Submission status: Wrong Answer

Solution code:
import sys
n = int(input())
alis = list(map(int, input().split()))
blis = list(map(int, input().split()))
alis.sort()
blis.sort()
for i in range(n):
    if alis[i] > blis[i]:
        print("No")
        sys.exit()


for i in range(n-1):
    if alis[i+1] > blis[i]:
        pass
    else:
        print("Yes")
        sys.exit()
print("No")

Submission status: Wrong Answer

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

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 = N <= 3
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 True:
        idx = idx_map[idx]
        if idx == 0:
            break
        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 = not all(B[i] < A[i + 1] 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
n = int(input())
alis = list(map(int, input().split()))
blis = list(map(int, input().split()))
alis.sort()
blis.sort()
for i in range(n):
    if alis[i] > blis[i]:
        print("No")
        sys.exit()


for i in range(n-1):
    if alis[i+1] > blis[i]:
        continue
    else:
        print("Yes")
        sys.exit()
print("No")

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]


class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = list(range(n + 1))
        self.ranks = [0 for _ in range(n + 1)]

    def get_root(self, x):
        if self.parents[x] == x:
            return x
        self.parents[x] = self.get_root(self.parents[x])
        return self.parents[x]

    def merge(self, x, y):
        x = self.get_root(x)
        y = self.get_root(y)
        if x != y:
            if self.ranks[x] < self.ranks[y]:
                self.parents[x] = y
            else:
                self.parents[y] = x
                if self.ranks[x] == self.ranks[y]:
                    self.ranks[x] += 1


n = ri()
ls0 = ril()
ls1 = ril()

a0 = sorted(ls0)
a1 = sorted(ls1)
for x, y in zip(a0, a1):
    if x > y:
        print('No')
        exit()

ls = list(zip(ls0, ls1))
ls.sort(key=lambda x: x[1])
ls = [(i, x) for i, (x, _) in enumerate(ls)]
ls.sort(key=lambda x: x[1])
ls = list(zip(*ls))[0]

uf = UnionFind(n)
for i, x in enumerate(ls):
    uf.merge(i + 1, x + 1)
for i in range(n + 1):
    uf.get_root(i)

if len(set(uf.parents[1:])) == 1:
    print('No')
else:
    print('Yes')

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]


class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = list(range(n + 1))
        self.ranks = [0 for _ in range(n + 1)]

    def get_root(self, x):
        if self.parents[x] == x:
            return x
        self.parents[x] = self.get_root(self.parents[x])
        return self.parents[x]

    def merge(self, x, y):
        x = self.get_root(x)
        y = self.get_root(y)
        if x != y:
            if self.ranks[x] < self.ranks[y]:
                self.parents[x] = y
            else:
                self.parents[y] = x
                if self.ranks[x] == self.ranks[y]:
                    self.ranks[x] += 1


n = ri()
ls0 = ril()
ls1 = ril()

a0 = sorted(ls0)
a1 = sorted(ls1)
for x, y in zip(a0, a1):
    if x > y:
        print('No')
        exit()

ls = list(zip(ls0, ls1))
ls.sort(key=lambda x: x[1])
ls = [(i, x) for i, (x, _) in enumerate(ls)]
ls.sort(key=lambda x: x[1])
ls = list(zip(*ls))[0]

uf = UnionFind(n)
for i, x in enumerate(ls):
    uf.merge(i, x)
for i in range(n + 1):
    uf.get_root(i)
if len(set(uf.parents[1:])) == 1:
    print('No')
else:
    print('Yes')

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]


class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = list(range(n + 1))
        self.ranks = [0 for _ in range(n + 1)]

    def get_root(self, x):
        if self.parents[x] == x:
            return x
        self.parents[x] = self.get_root(self.parents[x])
        return self.parents[x]

    def merge(self, x, y):
        x = self.get_root(x)
        y = self.get_root(y)
        if x != y:
            if self.ranks[x] < self.ranks[y]:
                self.parents[x] = y
            else:
                self.parents[y] = x
                if self.ranks[x] == self.ranks[y]:
                    self.ranks[x] += 1


n = ri()
ls0 = ril()
ls1 = ril()

a0 = sorted(ls0)
a1 = sorted(ls1)
for x, y in zip(a0, a1):
    if x > y:
        print('No')
        exit()

ls = list(zip(ls0, ls1))
ls.sort(key=lambda x: x[1])
ls = [(i, x) for i, (x, _) in enumerate(ls)]
ls.sort(key=lambda x: x[1])
ls = list(zip(*ls))[0]

uf = UnionFind(n)
for i, x in enumerate(ls):
    uf.merge(i, x)
for i in range(n + 1):
    uf.get_root(i)
if len(set(uf.parents[1:])) == n:
    print('No')
else:
    print('Yes')

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    n = int(input())
    aa = list(map(int, input().split()))
    bb = list(map(int, input().split()))
    aaii = []
    bbii = []
    for i, (a, b) in enumerate(zip(aa, bb)):
        aaii.append([a, i])
        bbii.append([b, i])
    aaii.sort()
    bbii.sort()
    # print(aaii)
    # print(bbii)
    swap = False
    biai = []
    for i in range(n):
        biai.append([bbii[i][1], aaii[i][1]])
        if aaii[i][0] > bbii[i][0]:
            print("No")
            exit()
        if swap: continue
        if i + 1 < n and aaii[i + 1][0] <= bbii[i][0]:
            swap = True
    biai.sort()
    # print(biai)
    if swap:
        print("Yes")
        exit()
    now = 0
    cnt = 0
    while 1:
        now = biai[now][1]
        cnt += 1
        if now == 0:
            print(cnt)
            if cnt < n:
                print("Yes")
            else:
                print("No")
            break

main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#第二回全国統一プログラミング王決定戦予選 C

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = LI()
b = LI()
c = sorted(a)
d = sorted(b)
for i in range(n):
    if c[i] > d[i]:
        print('No')
        quit()
#print(c,d)
f = defaultdict(int)
fi = defaultdict(int)
fib = defaultdict(int)
for i in range(n):
    f[a[i]] = b[i]
    fi[a[i]] = i
    fib[b[i]] = i
lst = []
cnt = 0
flgf = defaultdict(lambda : True)
for i in range(n):
    if not flgf[i]:
        continue
    if f[c[i]] != d[i]:
        flgf[fi[c[i]]] = False
        cnt += 1
if cnt > n-2:
    print('No')
else:
    print('Yes')