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: Wrong Answer)

#include<bits/stdc++.h>

using namespace std;
#define INFS (1LL<<28)
#define INF (1LL<<60)
#define DEKAI 1000000007
#define MOD 998244353
#define lp(i,n) for(int i=0;i<n;i++)
#define double long double
#define all(c) begin(c), end(c)

#define int long long 

namespace {
#define __DECLARE__(C)    \
    template <typename T> \
    std::ostream &operator<<(std::ostream &, const C<T> &);

#define __DECLAREM__(C)               \
    template <typename T, typename U> \
    std::ostream &operator<<(std::ostream &, const C<T, U> &);

__DECLARE__(std::vector)
__DECLARE__(std::deque)
__DECLARE__(std::set)
__DECLARE__(std::stack)
__DECLARE__(std::queue)
__DECLARE__(std::priority_queue)
__DECLARE__(std::unordered_set)
__DECLAREM__(std::map)
__DECLAREM__(std::unordered_map)

template <typename T, typename U>
std::ostream &operator<<(std::ostream &, const std::pair<T, U> &);
template <typename... T>
std::ostream &operator<<(std::ostream &, const std::tuple<T...> &);
template <typename T, std::size_t N>
std::ostream &operator<<(std::ostream &, const std::array<T, N> &);

template <typename Tuple, std::size_t N>
struct __TuplePrinter__ {
    static void print(std::ostream &os, const Tuple &t) {
        __TuplePrinter__<Tuple, N - 1>::print(os, t);
        os << ", " << std::get<N - 1>(t);
    }
};

template <typename Tuple>
struct __TuplePrinter__<Tuple, 1> {
    static void print(std::ostream &os, const Tuple &t) { os << std::get<0>(t); }
};

template <typename... T>
std::ostream &operator<<(std::ostream &os, const std::tuple<T...> &t) {
    os << '(';
    __TuplePrinter__<decltype(t), sizeof...(T)>::print(os, t);
    os << ')';
    return os;
}

template <typename T, typename U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &v) {
    return os << '(' << v.first << ", " << v.second << ')';
}

#define __INNER__                             \
    os << '[';                                \
    for (auto it = begin(c); it != end(c);) { \
        os << *it;                            \
        os << (++it != end(c) ? ", " : "");   \
    }                                         \
    return os << ']';

template <typename T, std::size_t N>
std::ostream &operator<<(std::ostream &os, const std::array<T, N> &c) {
    __INNER__
}

#define __DEFINE__(C)                                           \
    template <typename T>                                       \
    std::ostream &operator<<(std::ostream &os, const C<T> &c) { \
        __INNER__                                               \
    }

#define __DEFINEM__(C)                                             \
    template <typename T, typename U>                              \
    std::ostream &operator<<(std::ostream &os, const C<T, U> &c) { \
        __INNER__                                                  \
    }

#define __DEFINEW__(C, M1, M2)                                  \
    template <typename T>                                       \
    std::ostream &operator<<(std::ostream &os, const C<T> &c) { \
        std::deque<T> v;                                        \
        for (auto d = c; !d.empty(); d.pop()) v.M1(d.M2());     \
        return os << v;                                         \
    }

__DEFINE__(std::vector)
__DEFINE__(std::deque)
__DEFINE__(std::set)
__DEFINEW__(std::stack, push_front, top)
__DEFINEW__(std::queue, push_back, front)
__DEFINEW__(std::priority_queue, push_front, top)
__DEFINE__(std::unordered_set)
__DEFINEM__(std::map)
__DEFINEM__(std::unordered_map)
}

using Weight = int;
using Flow = int;
struct Edge {
    int src, dst;
    Weight weight;
    Flow cap;
    Edge() : src(0), dst(0), weight(0) {}
    Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) {}
};
using Edges = std::vector<Edge>;
using Graph = std::vector<Edges>;
using Array = std::vector<Weight>;
using Matrix = std::vector<Array>;

void add_edge(Graph &g, int a, int b, Weight w = 1) {
    g[a].emplace_back(a, b, w);
    g[b].emplace_back(b, a, w);
}
void add_arc(Graph &g, int a, int b, Weight w = 1) { g[a].emplace_back(a, b, w); }
vector<pair<int,int>> v;
int good[2][100001];
set<pair<int,int>> m;
map<int,int> place[2];
signed main(){
	int n;
	cin>>n;
	vector<int> a(n),b(n);
	lp(i,n){
		cin>>a[i];
	}
	lp(i,n){
		cin>>b[i];
	}
	lp(i,n){
		v.push_back({a[i],b[i]});
		m.insert({a[i],b[i]});
	}
	sort(all(a));
	sort(all(b));
	lp(i,n){
		place[0][a[i]]=i;
		place[1][b[i]]=i;
	}
	a.push_back(INF);
	b.push_back(INF);
	bool ans=true;
	lp(i,n){
		if(a[i]>b[i]){
			cout<<"No"<<endl;
			return 0;
		}
		if(a[i]<=b[i+1])good[0][i]=1;
		if(b[i]>=a[i+1])good[1][i]=1;
	}
	lp(i,n){
		if(m.find({a[i],b[i]})!=m.end()){
			cout<<"Yes"<<endl;
			return 0;
		}
	}
	for(int i=100000;i>=0;i--){
		int befa=-1,befb=-1;
		if(good[0][i]==0){
			good[0][i]=-1;
			befa=-1;
		}
		else{
			if(befa==-1) befa=i;
			good[0][i]=befa;
		}
		if(good[1][i]==0){
			good[1][i]=-1;
			befb=-1;
		}
		else{
			if(befb==-1) befb=i;
			good[1][i]=befb;
		}
	}
	vector<int> g[100001];
	lp(i,n){
		int ax=v[i].first;
		int bx=v[i].second;
		int pa=place[0][ax];
		int pb=place[1][bx];
		g[pa].push_back(pb);
		if(!ax<=bx)continue;
		if(pb<pa){
			if(good[0][pb]>=pa-1){
				/*cout<<"Yes"<<endl;
				return 0;*/
			}
		}
		else{
			if(good[1][pa]>=pb-1){
				/*cout<<"Yes"<<endl;
				return 0;*/
			}
		}
	}

	int done[100001];
	lp(i,100001)done[i]=0;
	int cnt=0;
	lp(i,100001){
		if(i==0)continue;
		if(done[i]!=0)continue;
		queue<int> q;
		q.push(i);
		done[i]=i;
		while(!q.empty()){
			int now=q.front();
			q.pop();
			lp(j,g[now].size()){
				int next=g[now][j];
				if(done[next]==i){
					cnt++;	
				}
				if(done[next]!=0)continue;
				done[next]=i;
				q.push(next);
			}
		}
	}
	if(cnt>1){
		cout<<"Yes"<<endl;
		return 0;
	}
	cout<<"No"<<endl;
	return 0;
}