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: Runtime Error

Solution code:
import sys
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = sorted([[a[i], b[i]] for i in range(n)], key=lambda x: x[1])
a = [(x[0], i) for i, x in enumerate(c)]
b = [x[1] for x in c]
a.sort()
for i in range(n):
	if a[i][0] > b[i]:
		print("No")
		sys.exit()
cur, cnt = 0, 0
while a[cur][1] != 0:
	cnt += 1
	cur = a[cur][1]
if cnt < n-1:
	print("Yes")
else:
	for i in range(n-1):
		if a[i] <= b[i+1]:
			print("Yes")
			break
	else:
		print("No")

Submission status: Runtime Error

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

a.sort()
b.sort()

i = 0
while i <= N:
  if (b[i] - a[i] < 0):
    print('No')
    break
  i += 1
else:
  print('Yes')

Submission status: Runtime Error

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

a.sort()
b.sort()

while i <= N:
  if (b[i] - a[i] < 0):
    print('No')
    break
  i += 1
else:
  print('Yes')

Submission status: Runtime Error

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

a.sort()
b.sort()
c = []

while i <= N:
  if (b[i] - a[i] < 0):
    print('No')
    break
  i += 1
else:
  print('Yes')

Submission status: Runtime Error

Solution code:
def examA():
    N = I()
    ans = (N-1)//2
    print(ans)
    return

def examB():
    N = I()
    D = LI()
    d = Counter(D)
    loop = max(d.keys())
    if D[0]!=0 or d[0]!=1:
        print(0)
        return
#    print(d,loop)
    ans = 1; cur = 1
    for i in range(loop+1):
        if d[i]==0:
            print(0)
            return
        for _ in range(d[i]):
            ans *= cur
            ans %= mod2
        cur = d[i]
    print(ans)
    return

def examC():
    N = I()
    A = LI()
    B = LI()
    indices = [*range(len(B))]
    sorted_indices = sorted(indices, key=lambda i: B[i])
    sorted_B = [B[i] for i in sorted_indices]
    sorted_A = sorted(A)
#    print(sorted_B); print(sorted_indices)
    d = defaultdict(int)
#    print(sortA,sortB)
    for i in range(N):
        d[sorted_A[i]] = sorted_B[i]
        if sorted_A[i]>sorted_B[i]:
            print("No")
            return
    for i in range(N-1):
        if sorted_A[i+1]<=sorted_B[i]:
            print("Yes")
            return
    for i in range(N):
        a = sorted_indices[i]
        if d[A[a]]==B[i]:
            print("Yes")
            return
    print("No")
    return

def examD():
    class Dijkstra(object):
        """
        construct: O(ElogV)
        """

        def __init__(self, edges, start=0):
            """
            :param list of list of list of int edges:
            :param int start=0:
            """
            self.__dist = [inf] * len(edges)
            self.__dist[start] = 0
            self.__calculate(edges, start)

        @property
        def dist(self):
            return self.__dist

        def __calculate(self, edges, start):
            Q = [(0, start)]  # (dist,vertex)
            while (Q):
                dist, v = heapq.heappop(Q)
                if self.dist[v] < dist: continue  # 候補として挙がったd,vだが、他に短いのがある
                for u, cost in edges[v]:
                    if self.dist[u] > self.dist[v] + cost:
                        self.__dist[u] = self.dist[v] + cost
                        heapq.heappush(Q, (self.dist[u], u))
    N, M = LI()
    V = [[]for _ in range(N)]
    for _ in range(M):
        l, r, c = LI()
        l -=1; r -=1
        V[l].append((r,c))
    for i in range(N-1):
        V[i+1].append((i,0))
#    print(V)
    D = Dijkstra(V)
    ans = D.dist[N-1]
    if ans==inf:
        ans = -1
    print(ans)
    return

def examE():
    N, K = LI()
    ans = [[]for _ in range(N)]


    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LFI(): return list(map(float,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examC()

"""

"""

Submission status: Runtime Error

Solution code:
from bisect import bisect
inf=float("inf")
N=int(input())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
C=[]
D=[]
for i in range(N):
    C.append([A[i],i])
    D.append([B[i],i])
C.sort()
D.sort()

count=0
data=[0 for i in range(N)]
for i in range(N):
    d=D[i][0]
    b=bisect(C,[d,inf])
    #dより小さいAの要素がbこある
    if b<=i:
        print("No")
        break
    elif d>i+1:
        count=1
    data[C[i][1]]=D[i][1]
else:
    if count==1:
        print(1/0)
    else:
        flag=[0]*N
        num=0
        flag[0]=1
        count=1
        for i in range(N+1):
            num=data[num]
            if flag[num]==0:
                flag[num]=1
                count+=1
                continue
            else:
                break
        if count==N:
            print("No")
        else:
            print("Yes")

Submission status: Runtime Error

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

sa = sorted(A)
sb = sorted(B)

flag = True
for i in range(N):
    if sa[i] > sb[i]:
        flag = False
        break

if flag:
    for i in range(N):
        if a[i] == b[i]:
            flag = True
            break
    else:
        flag = False

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

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define debug(x) cerr << #x << " " << x << '\n'
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
const int INF = 0x3f3f3f3f, N = 2e5 + 5;
const ll LINF = 1e18 + 5;
int to[N], n;
bool vis[N];
struct node
{
	int val, pos;
	bool operator < (const node& x) { return val < x.val; }
}a[N], b[N];
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++) 
	{
		scanf("%d",&a[i].val);
		a[i].pos = i;
	}
	for(int i=1;i<=n;i++) 
	{
		scanf("%d",&b[i].val);
		b[i].pos = i;
	}
	sort(a+1,a+n+1);
	sort(b+1,b+n+1);
	for(int i=1;i<=n;i++) if(a[i].val>b[i].val) return puts("No"), 0;
	for(int i=2;i<=n;i++) if(a[i].val>=b[i-1].val) return puts("Yes"), 0;
	for(int i=1;i<=n;i++) to[a[i].pos] = b[i].pos;
	int cnt = 0;
	for(int i=1;i<=n;i++)
	{
		int now = i;
		if(vis[now]) continue;
		int loop = 1;
		vis[now] = 1;
		while(1)
		{
			now = to[now];
			if(now==i) break;
			loop++;
			vis[now] = 1;
		}
		cnt += loop - 1;
	}
	if(cnt<n-1) puts("Yes");
	else puts("No");
	return 0;
}

Submission status: Runtime Error

Solution code:
N = int(input())
A = map(int, input().split(" "))
B = map(int, input().split(" "))
count = 0
for i in range(N):
    if A[i] > B[i]:
        count = count + 1
    else:
        continue
if count > N - 2:
    print("No")
else:
    print("Yes")

Submission status: Runtime Error

Solution code:
N = int(input())
A = map(int, input().split(" "))
B = map(int, input().split(" "))
count = 0
for i in range(N):
    if A[i] > B[i]:
        count = count + 1
if count > N - 2:
    print("No")
else:
    print("Yes")

Submission status: Runtime Error

Solution code:
N = int(input())
A = map(int, input().split(" "))
B = map(int, input().split(" "))
count = 0
for i in range(N):
    if A[i] >= B[i]:
        count = count + 1
if count > N - 2:
    print("No")
else:
    print("Yes")

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
l = sorted(list(zip(b, a)))
 
a.sort()
b.sort()
for i in range(n):
  if a[i] > b[i]:
    print('No')
    exit()
    
fir, las = {}, {}
s = [0] * n
for i in range(n):
  fir[a[i]] = min(fir.get(a[i], n), i)
  las[a[i]] = max(las.get(a[i], 0), i)
  if i > 0 and a[i] <= b[i - 1]:
    s[i] = 1
for i in range(1, n):
  s[i] += s[i - 1]
 
for i, (x, y) in enumerate(l):
  if x >= y:
    if y >= a[i] or s[fir[x]] - s[las[y]] == fir[x] - las[y]:
      print('Yes')
      exit()
 
print('No')

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
l = sorted(list(zip(b, a)))
 
a.sort()
b.sort()
for i in range(n):
  if a[i] > b[i]:
    print('No')
    exit()
    
fir, las = {}, {}
s = [0] * n
for i in range(n):
  fir[a[i]] = min(fir.get(a[i], n), i)
  las[a[i]] = max(las.get(a[i], 0), i)
  if i > 0 and a[i] <= b[i - 1]:
    s[i] = 1
for i in range(1, n):
  s[i] += s[i - 1]
 
for i, (x, y) in enumerate(l):
  if x >= y:
    if y >= a[i] or s[fir[x]] - s[las[y]] == fir[x] - las[y]:
      print('Yes')
      exit()
 
print('No')

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
l = sorted(list(zip(b, a)))
 
a.sort()
b.sort()
for i in range(n):
  if a[i] > b[i]:
    print('No')
    exit()
    
fir, las = {}, {}
s = [0] * n
for i in range(n):
  fir[a[i]] = min(fir.get(a[i], n), i)
  las[a[i]] = max(fir.get(a[i], 0), i)
  if i > 0 and a[i] <= b[i - 1]:
    s[i] = 1
for i in range(1, n):
  s[i] += s[i - 1]
 
for i, (x, y) in enumerate(l):
  if x >= y:
    if y >= a[i] or s[fir[x]] - s[las[y]] == fir[x] - las[y]:
      print('Yes')
      exit()
 
print('No')

Submission status: Runtime Error

Solution code:
from operator import itemgetter

DEBUG = 0;

def calc(n, A, Bs, As):
    count = 0
    for i in range(n):#scanB
        if DEBUG:print('i=',i)#Debug
        if As[i] <= Bs[i]:
            continue
        else:
            if DEBUG:print('As <= Bs')
            return 'No'

    for i in range(n):#scanB
        if DEBUG:print('i=',i)#Debug
        if A[i] == Bs[i]:
            if DEBUG:print('A[i] <= Bs[i]')
            return 'Yes'
    
    return 'No'

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

if DEBUG:print(f'A:{A}') 
if DEBUG:print(f'B:{B}')

pair = list(zip(B,A))
pair.sort()#sortB

B, A = zip(*pair)#unzip

A = list(A)
B = list(B)
As = sorted(A)

if DEBUG:print(f'A:{A}')
if DEBUG:print(f'B:{B}')
if DEBUG:print(f'As:{As}')

print(calc(n,A,B,As))#for stdout

Submission status: Runtime Error

Solution code:
from operator import itemgetter

DEBUG = 0;

def calc(n, A, Bs, As):
    count = 0
    for i in range(n):#scanB
        if DEBUG:print('i=',i)#Debug
        if A[i] == Bs[i]:
            if DEBUG:print('A[i] <= Bs[i]')
            return 'Yes'
        if As[i] <= Bs[i]:
            continue
        else:
            if DEBUG:print('As <= Bs')
            return 'No'

    return 'Yes'

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

if DEBUG:print(f'A:{A}') 
if DEBUG:print(f'B:{B}')

pair = list(zip(B,A))
pair.sort()#sortB

B, A = zip(*pair)#unzip

A = list(A)
B = list(B)
As = sorted(A)

if DEBUG:print(f'A:{A}')
if DEBUG:print(f'B:{B}')
if DEBUG:print(f'As:{As}')

print(calc(n,A,B,As))#for stdout

Submission status: Runtime Error

Solution code:
from operator import itemgetter

DEBUG = 0;

def calc(n, A, Bs, As):
    count = 0
    for i in range(n):#scanB
        if DEBUG:print('i=',i)#Debug
        if As[i] <= Bs[i]:
            continue
        else:
            if DEBUG:print('As <= Bs')
            return 'No'
    tmpA = As[1:n]+As[0:1]
    if A == tmpA:
        if DEBUG:print('As[1:n]+As[0:1]')
        return 'No'
    tmpA = As[n-1:n]+As[0:n-1]
    if A == tmpA:
        if DEBUG:print('As[n-1:n]+As[0:n-1]')
        return 'No'
    return 'Yes'

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

if DEBUG:print(f'A:{A}') 
if DEBUG:print(f'B:{B}')

pair = list(zip(B,A))
pair.sort()#sortB

B, A = zip(*pair)#unzip

A = list(A)
B = list(B)
As = sorted(A)

if DEBUG:print(f'A:{A}')
if DEBUG:print(f'B:{B}')
if DEBUG:print(f'As:{As}')

print(calc(n,A,B,As))#for stdout

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())
A = np.array(list(map(int,input().split())))
B = np.array(list(map(int,input().split())))
Bsi = np.argsort(B)
A2 = A[Bsi]
Bs = B[Bsi]
A2si = np.argsort(A2)
As = A2[A2si]
C = Bs-As
if any(C[C<0]):
    ans = 'No'
else:
    D = A2-As
    if any(D == 0):
        ans = 'Yes'
    elif all(As[1::]-Bs[:-1:] > 0):
        psum = A2si[0]
        for i,pi in enumerate(A2si[1:-1],1):
            psum += pi
            if psum == int(i*(i+1)/2)
                ans = 'Yes'
                break
        else:
            ans = 'No'
    else:
        ans = 'Yes'
        
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
from operator import itemgetter


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

info = [(a[i], b[i]) for i in range(n)]
info = sorted(info, key = itemgetter(1))
for i in range(n):
    a[i], b[i] = info[i]

s = sorted(a)
t = sorted(b)
for i in range(n):
    if s[i] > t[i]:
        print("No")
        exit()

sorted_a = sorted(zip(a, range(len(a))))
graph = [[] for i in range(n)]
for i in range(n):
    u = i
    v = sorted_a[i][1]
    if u != v:
        graph[u].append(v)

used = [False]*n
pos = 0
used[pos] = True
q = deque([pos])
while q:
    pos = q.pop()
    for next_pos in graph[pos]:
        if used[next_pos]:
            continue
        q.append(next_pos)
        used[next_pos] = True

for i in range(n):
    if not used[i]:
        print("Yes")
        exit()

for i in range(n):
    if s[i+1] <= t[i]:
        print("Yes")
        exit()

print("No")

Submission status: Runtime Error

Solution code:
from operator import itemgetter

def calc(n, A, B):
    count = 0
    for i in range(n):#scanB
        if DEBUG:print('i=',i)#Debug
        if A[i] <= B[i]:
            continue
        elif i < n-1:
            for j in range(i+1,n):#scanA
                if DEBUG:print(f'i={i},j={j}')
                if A[j] <= B[i]:
                    A[j],A[i] = A[i],A[j]
                    count += 1
                    if count > n-2:
                        if DEBUG:print(f'count over:{count}')
                        return 'No'
                    if DEBUG:print(f'swap:{count}')
                    if DEBUG:print(f'A:{A}')
                    if DEBUG:print(f'B:{B}')
                    break
                elif j == n-1:
                    if DEBUG:print('j == n-1')
                    return 'No'
        else:
            if DEBUG:print('i == n-1')
            return 'No'
    
    return 'Yes'

                
#main
DEBUG = 0;

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

if DEBUG:print(f'A:{A}') 

if DEBUG:print(f'B:{B}')

pair = list(zip(B,A))
pair.sort()#sortB

B, A = zip(*pair)

if DEBUG:print(f'A:{A}')
if DEBUG:print(f'B:{B}')

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

if DEBUG:print(f'A:{ltA}')
if DEBUG:print(f'B:{ltB}')

if DEBUG:print(f'pair:{pair}')

print(calc(n,ltA,ltB))#for stdout

Submission status: Runtime Error

Solution code:
from operator import itemgetter

def calc(n, A, B):
    count = 0
    for i in range(n):#scanB
        if DEBUG:print('i=',i)#Debug
        if A[i] <= B[i]:
            continue
        elif i < n-1:
            for j in range(i+1,n):#scanA
                if DEBUG:print(f'i={i},j={j}')
                if A[j] <= B[i] and A[i] <= B[j]:
                    A[j],A[i] = A[i],A[j]
                    count += 1
                    if count > n-2:
                        if DEBUG:print('count over')
                        return 'No'
                    if DEBUG:print(f'swap:{count}')
                    if DEBUG:print(f'A:{A}')
                    if DEBUG:print(f'B:{B}')
                    break
                elif j == n-1:
                    if DEBUG:print('j == n-1')
                    return 'No'
        else:
            if DEBUG:print('i == n-1')
            return 'No'
    
    return 'Yes'

                
#main
DEBUG = 0;

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

if DEBUG:print(f'A:{A}') 

if DEBUG:print(f'B:{B}')

pair = list(zip(A,B))
pair.sort(key=itemgetter(1))#sortB

A, B = zip(*pair)

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

if DEBUG:print(f'A:{ltA}')
if DEBUG:print(f'B:{ltB}')

if DEBUG:print(f'pair:{pair}')

print(calc(n,ltA,ltB))#for stdout

Submission status: Runtime Error

Solution code:
n = input()
a = input().split(" ")
b = input().split(" ")

s_a = sorted(a)
s_b = sorted(b)

t_b = ["-1"]*len(s_b)

for i, v in enumerate(s_a):
    pos = a.index(v)
    if (pos == i):
        exit("Yes")
    else:
        t_b[pos] = s_b[i]

for i,v in enumerate(s_b):
    j = t_b.index(v)
    if (t_b[i] == s_b[j]):
        exit("Yes")

exit("No")

Submission status: Runtime Error

Solution code:
n = input()
a = input().split(" ")
b = input().split(" ")

s_a = a.sort()
s_b = b.sort()

t_b = [-1]*len(s_b)

pairs = 0
unchange = 0

for i, v in enumerate(s_a):
  pos = a.index(v)
  if (pos == i):
    return "Yes"
  else:
    t_b[pos] = s_b[i]
for i,v in enumerate(s_b):
  j = t_b.index(v)
  if (t_b[i] == s_b[j]):
    return "Yes"
return "No"

Submission status: Runtime Error

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

c=0
for i in range (n):
    x=i
    m=0
    for im in range(n):
        if (a[im]<a[m]):
            m=im
    while (x<n):
        if (a[x]<=b[i]) & (a[x]>a[m]):
            m=x
        x=x+1
    if (a[m]!=a[i]):
        a[m],a[i]=a[i],a[m]
        c=c+1
r=True
for i in range(n)
    if (a[i]>b[i]): r=False
if (c<=n-2) & r:
    print("Yes")
else:
    print("No")

Submission status: Runtime Error

Solution code:
from operator import itemgetter

def calc(n, A, B):
    count = 0
    for i in range(n):#scanB
        print('i=',i)#Debug
        if A[i] <= B[i]:
            continue
        elif i < n-1:
            for j in range(i+1,n):#scanA
                if A[j] <= B[i] and A[i] <= B[j]:
                    A[j],A[i] = A[i],A[j]
                    count += 1
                    if count > n-2:
                        return 'No'
                    print(f'swap:{count}')
                    print(f'A:{A}')
                    print(f'B:{B}')
                    break
                elif j == n-1:
                    return 'No'
        else:
            return 'No'
    
    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(1))#sortB

A, B = zip(*pair)

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

print(f'A:{ltA}')
print(f'B:{ltB}')

#print(f'pair:{pair}')

print(calc(n,ltA,ltB))

Submission status: Runtime Error

Solution code:
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)]

a2 = [[a[i], i] for i in range(n)]
a2.sort(key=lambda x:x[0])

flag1 = 0
for i in range(n):
    if a2[i][0] > b[i]:
        flag1 = 1

flag2 = 1
idx = a2[0][1]
cnt = 0
while idx != 0:
    idx = a2[idx][1]
    cnt += 1

if cnt == n-1:
    for i in range(n-1):
        if a2[i+1] <= b[i]:
            flag2=0
            break
else:
    flag2 = 0

if flag1 == 1 or flag2 == 1:
    print('No')
else:
    print('Yes')

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
l = sorted(list(zip(b, a)))
 
a.sort()
b.sort()
for i in range(n):
  if a[i] > b[i]:
    print('No')
    exit()
 
p = 0
for i, (x, y) in enumerate(l):
  if x >= y:
    if y >= a[i] or i == 0 or l[i - 1][0] >= a[i]
      print('Yes')
      exit()
 
print('No')

Submission status: Runtime Error

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

A_new = sorted(A)
B_new = sorted(B)
 
for i in range(N):
    if B_new[i] >= A_new[i]:
        count2 += 1
 
for i in range(N):
    for j in range(N):
        if A[i] == A_new[j] and B[i] == B_new[j] 
            count1 = 1
            break
        if A[i] == A_new[j] and B[i] < B_new[j]
            break
    if count1 == 1:
        break
 
if count1 == 1 and count2 == N:
    print("Yes")
else:
    print("No")

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 998244353
inf = float('INF')

#A
def A():
    n = II()
    n -= 1
    print(n // 2)
    return

#B
def combination_mod(n, k, mod=mod):
    """ power_funcを用いて(nCk) mod p を求める """ 
    """ nCk = n!/((n-k)!k!)を使用 """

    from math import factorial
    if n < 0 or k < 0 or n < k: return 0
    if n == 0 or k == 0: return 1
    a = factorial(n) % mod
    b = factorial(k) % mod
    c = factorial(n - k) % mod
    return (a * pow(b, mod - 2, mod) * pow(c, mod - 2, mod)) % mod
    

def B():
    n = II()
    d = LI()
    dic = defaultdict(int)
    if d[0] != 0:
        print(0)
        return
    for di in d[1:]:
        dic[di] += 1
    ans = 1
    di = list(dic.items())
    di.sort()
    if di[0][0] != 1:
        print(0)
        return
    for i in range(len(di) - 1):
        if di[i][0] != di[i + 1][0] - 1:
            print(0)
            return
    be = 1
    for _, b in di:
        ans *= pow(be, b, mod)
        be = b
        ans %= mod
    print(ans)
    return
class BinaryIndexedTree:
    # http://hos.ac/slides/20140319_bit.pdf
    def __init__(self, size):
        """
        :param int size:
        """
        self.bit = [0 for _ in range(size)]
        self.size = size

    def add(self, i, w):
        """
        i番目にwを加える
        :param int i:
        :param int w:
        :return:
        """
        x = i + 1
        while x <= self.size:
            self.bit[x - 1] += w
            x += x & -x
        return

    def sum(self, i):
        """
        [0,i]の合計
        :param int i:
        :return:
        """
        res = 0
        x = i + 1
        while x > 0:
            res += self.bit[x - 1]
            x -= x & -x
        return res

    def search(self, x):
        """
        二分探索。和がx以上となる最小のインデックス(>= 1)を返す
        :param int x:
        :return :
        """
        i = 1
        s = 0
        step = 1 << (self.size.bit_length() - 1)
        while step:
            if i + step <= self.size and s + self.bit[i + step - 1] < x:
                i += step
                s += self.bit[i - 1]
            step >>= 1
        return i

    def __len__(self):
        return self.size
#C
def C():
    n = II()
    a = LI()
    b = LI()
    res = None
    for i in range(n):
        if a[i] <= b[i]:
            res = True
    if res == None:
        print("No")
        return
    ca = a[::1]
    cb = b[::1]
    ca.sort()
    cb.sort()
    d = [0] * (n + 1)
    for i in range(n):
        if ca[i] > cb[i]:
            print("No")
            return
        if i == 0:
            continue
        d[i] = ca[i] <= cb[i - 1]
    d = list(itertools.accumulate(d))

    for i in range(n):
        if a[i] <= b[i]:
            first = bisect_left(ca, a[i])
            end = bisect_right(cb, b[i]) - 1
            first, end = end, first if end >= first else first, end
            f = d[end] - d[first]
            if f == end - first:
                print("Yes")
                return
    print("No")
    return


def Dijkstra(num, start, vedge):

    dist = [float("inf") for i in range(num)]
    dist[0] = 0
    for i in range(num):
        du = dist[i]
        l = i
        for k, j in vedge[i]:
            if dist[j] <= du + k:
                continue
            for j in range(j, i - 1, -1):
                if dist[j] > du + k:
                    dist[j] = du + k
                    continue
                break
    return dist


#D
def D():
    n, m = LI()
    dist = [[] for i in range(n)]
    for _ in range(m):
        l, r, c = LI_()
        c += 1
        dist[l].append([c, r])
    for i in range(n):
        dist[i].sort()
    dist = Dijkstra(n, 0, dist)
    print(dist[n - 1] if dist[n - 1] != inf else -1)
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    C()

Submission status: Runtime Error

Solution code:
def main():
  N=int(input())
  A=list(map(int,input().split()))
  B=list(map(int,input().split()))
  tmp={}
  for i in range(N):
    if not A[i] in tmp.keys():
      tmp[A[i]]=[]
    tmp[A[i]].append(B[i])
  A.sort()
  B.sort()
  for i in range(N):
    if A[i]>B[i]:
      print("No")
      return 0
  for i in range(N):
    if B[i] in tmp[A[i]]:
      print("Yes")
      return 0
    if B[i] >= A[i+1] and i<=N-2:
      for j in range(i+1,N-2):
        if B[i] in tmp[A[j]]:
          print("Yes")
          return 0
  print("No")
      
main()

Submission status: Runtime Error

Solution code:
import numpy as np
n = int(input())
a = np.array(list(map(int, input().split())))
b = np.array(list(map(int, input().split())))

a_sort = np.sort(a)
b_sort = np.sort(b)

# 1. 回数関係なくそもそも不可能なやつ → no
# 2-a. サイクルは1つだとしても，完璧じゃない組み合わせが1つ以上許容されるやつ → yes
# 2-b. サイクルが2以上にわけられるやつ → yes

# 1
if (a_sort > b_sort).any():
    print('No')
    exit()

# 2-a
if (a_sort[1:] <= b_sort[:-1]).any():
    print('Yes')
    exit()

# 2-b （startをとりあえずa[0]とかにする）
d = dict(zip(a.tolist(),b.tolist()))
from_ = a[0]
count = 0
while True:
    count += 1
    from_ = d[from_]
    if from_ == a[0]:
        break

if count != n:
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
import numpy as np
n = int(input())
a = np.array(list(map(int, input().split())))
b = np.array(list(map(int, input().split())))

a_sort = np.sort(a)
b_sort = np.sort(b)

# 1. 回数関係なくそもそも不可能なやつ → no
# 2-a. サイクルは1つだとしても，完璧じゃない組み合わせが1つ以上許容されるやつ → yes
# 2-b. サイクルが2以上にわけられるやつ → yes

# 1
if (a_sort > b_sort).any():
    print('No')
    exit()

# 2-a
if (a_sort[1:] <= b_sort[:-1]).any():
    print('Yes')
    exit()

# 2-b （startをとりあえずa[0]とかにする）
d = dict(zip(a,b))

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

if count != n:
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
import numpy as np
n = int(input())
a = np.array(list(map(int, input().split())))
b = np.array(list(map(int, input().split())))

a_sort = np.sort(a)
b_sort = np.sort(b)

# 1. 回数関係なくそもそも不可能なやつ → no
# 2-a. サイクルは1つだとしても，完璧じゃない組み合わせが1つ以上許容されるやつ → yes
# 2-b. サイクルが2以上にわけられるやつ → yes

# 1
if (a_sort > b_sort).any():
    print('No')
    exit()

# 2-a
if (a_sort[1:] <= b_sort[:-1]).any():
    print('Yes')
    exit()

# 2-b （startをとりあえずa[0]とかにする）
d = dict(zip(a,b))
for i in range(n):
    d[a[i]] = b[i]

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

if count != n:
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
import numpy as np

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

a_sort = np.sort(a)
b_sort = np.sort(b)

# 1. 回数関係なくそもそも不可能なやつ → no
# 2-a. サイクルは1つだとしても，完璧じゃない組み合わせが1つ以上許容されるやつ → yes
# 2-b. サイクルが2以上にわけられるやつ → yes

# 1
if (a_sort > b_sort).any():
    print('No')
    exit()

# 2-a
if (a_sort[1:] <= b_sort[:-1]).any():
    print('Yes')
    exit()

# 2-b （startをとりあえず1とかにする）
d = dict(zip(a,b))
from_ = 1
count = 0
while True:
    count += 1
    from_ = d[from_]
    if from_ == 1:
        break

if count != n:
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
import numpy as np
from collections import defaultdict
n = int(input())
a = np.array(list(map(int, input().split())))
b = np.array(list(map(int, input().split())))

a_sort = np.sort(a)
b_sort = np.sort(b)

# 1. 回数関係なくそもそも不可能なやつ → no
# 2-a. サイクルは1つだとしても，完璧じゃない組み合わせが1つ以上許容されるやつ → yes
# 2-b. サイクルが2以上にわけられるやつ → yes

# 1
if (a_sort > b_sort).any():
    print('No')
    exit()

# 2-a
if (a_sort[1:] <= b_sort[:-1]).any():
    print('Yes')
    exit()

# 2-b （startをとりあえず1とかにする）
d = defaultdict()
for i in range(n):
    d[a[i]] = b[i]

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

if count != n:
    print('Yes')
else:
    print('No')

Submission status: Runtime Error

Solution code:
class UnionFind:
    def __init__(self,N):
        self.parent = [i for i in range(N)]
        self.rank = [0] * N
        self.count = 0
    def root(self,a):
        if self.parent[a] == a:
            return a
        else:
            self.parent[a] = self.root(self.parent[a])
            return self.parent[a]
    def is_same(self,a,b):
        return self.root(a) == self.root(b)
    def unite(self,a,b):
        ra = self.root(a)
        rb = self.root(b)
        if ra == rb: return
        if self.rank[ra] < self.rank[rb]:
            self.parent[ra] = rb
        else:
            self.parent[rb] = ra
            if self.rank[ra] == self.rank[rb]: self.rank[ra] += 1
        self.count += 1

n = int(input())
a_arr = list(map(int,input().split()))
s_a = sorted(a_arr)
b_arr = list(map(int,input().split()))
s_b = sorted(b_arr)
swap = False
pb = 0
for a,b in zip(s_a,s_b):
    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.is_same(a,sa):
        uf.unite(a,sa)
print('Yes')