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>
#define rep(i,N) for(int i=0;i<N;i++)
using namespace std;
long long  INF=1000000000000000000;
long long mod=1000000007;
using namespace std;

struct UnionFind {
    vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

    UnionFind(int N) : par(N) { //最初は全てが根であるとして初期化
        for(int i = 0; i < N; i++) par[i] = i;
    }

    int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    void unite(int x, int y) { // xとyの木を併合
        int rx = root(x); //xの根をrx
        int ry = root(y); //yの根をry
        if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
        par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
    }

    bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
};

int main(){
int N;
cin>>N;
multimap<long long,int>A;
long long a[N];//並び替える前
multimap<long long,int >B;
long long b[N];//並び替える前

rep(i,N){
cin>>a[i];
A.insert(make_pair(a[i],i));
}

rep(i,N){
cin>>b[i];
B.insert(make_pair(b[i],i));
}
auto it=A.begin();
auto bit=B.begin();
int correct[N];
int Bv[N];
rep(i,N){

    if(it->first>bit->first){
        cout<<"No"<<endl;
        return 0;
    }
    else{
        correct[it->second]=bit->second;//A[i]にとっての理想の相手はb[correct[i]];
        Bv[bit->second]=it->second;//B[p]にとっての理想の相手はBv[p];
    }
    it++;
    bit++;
}
UnionFind tree(N);
int good=0;

rep(i,N){
    if(i==Bv[i]){
        good++;
        continue;
    }/*
    if(!tree.same(i,Bv[i]) ){
        tree.unite(i,Bv[i]);
    }
    else if(tree.same(i,Bv[i])){
        good++;
    }
    */
}
/*
it=A.begin();
bit=B.begin();
rep(i,N){
int t=it->second;//並び替える前の番号
//auto l=B.lower_bound(it->first);
    if(t==correct[t]){
    good++;
    }
it++;
bit++;
}
*/

if(good>=1){
    cout<<"Yes"<<endl;
}
else{
    cout<<"No"<<endl;
}

 return 0;
}
/*

*/