Problem:
Score : 600 points

Problem Statement

Given are two integer sequences of N elements each: A_1,...,A_N and B_1,...,B_N.
Determine if it is possible to do the following operation at most N-2 times (possibly zero) so that, for every integer i from 1 to N, A_i \leq B_i holds:

Choose two distinct integers x and y between 1 and N (inclusive), and swap the values of A_x and A_y.

Constraints

2 \leq N \leq 10^5

1 \leq A_i,B_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N
B_1 B_2 ... B_N

Output

If the objective is achievable, print Yes; if it is not, print No.

Sample Input 1

3
1 3 2
1 2 3

Sample Output 1

Yes

We should swap the values of A_2 and A_3.

Sample Input 2

3
1 2 3
2 2 2

Sample Output 2

No

Sample Input 3

6
3 1 2 6 3 4
2 2 8 3 4 3

Sample Output 3

Yes

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
	t=0; char ch=getchar(); int f=1;
	while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
	do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
const int maxn=(1e5)+10;
int n,a[maxn],b[maxn],pos[maxn],c[maxn],C[maxn],cnt;
struct node { int x,y; } d[maxn],e[maxn];
bool cmp(node a,node b) { return a.y<b.y; }
void WA() { printf("No\n"); exit(0); }
void Swap(int x,int y) {
	//printf("%d %d\n",x,y);
	swap(c[x],c[y]); swap(C[x],C[y]);
	pos[c[x]]=x; pos[c[y]]=y;
}
int main() {
	//freopen("1.txt","r",stdin);
	read(n);
	for (int i=1;i<=n;i++) read(d[i].x);
	for (int i=1;i<=n;i++) read(d[i].y);
	sort(d+1,d+n+1,cmp);
	for (int i=1;i<=n;i++) {
		a[i]=d[i].x,b[i]=d[i].y;
		e[i]=(node){i,d[i].x};
		C[i]=a[i];
	}
	sort(e+1,e+n+1,cmp);
	for (int i=1;i<=n;i++) {
		c[e[i].x]=i,pos[i]=e[i].x;
		if (e[i].y>b[i]) WA();
	}
	//for (int i=1;i<=n;i++) printf("%d ",c[i]); printf("\n");
	//for (int i=1;i<=n;i++) printf("%d ",a[i]); printf("\n");
	for (int i=1;i<n;i++) {
		if (C[i]==e[i].y) continue;
		Swap(pos[i],i); cnt++;
	}
	if (cnt==n-1) {
		for (int i=1;i<n;i++)
			if (e[i+1].y<=b[i]&&e[i].y<=b[i+1]) { printf("Yes\n"); return 0; }
		WA();
	}
	printf("Yes\n");
	return 0;
}
/*
  REMEMBER:
  1. Think TWICE, Code ONCE!
  Are there any counterexamples to your algo?
    
  2. Be careful about the BOUNDARIES!
  N=1? P=1? Something about 0?
    
  3. Do not make STUPID MISTAKES!
  Array size? Integer overflow? Time complexity? Memory usage? Precision error?
*/