Submission #8387063


Source Code Expand

Copy
#include <bits/stdc++.h>
    
using namespace std;
 
#define f first
#define s second
#define pb push_back
#define sz(v) ((int)v.size())   
#define rad(a) a * PI / 180  
 
typedef long long ll;
typedef vector<int> vi;	
typedef pair<int,int> ii;
typedef pair<int,char> ic;
typedef pair<ll, bool> llb;
 
const int SIGMA = 20;
const int MAX = 100010;
const int MAXT = 362145;
const ll INF = 1000000001;
const ll P = 998244353LL; //1000000007;
const double EPS = 1e-6;
const double PI = 3.14159265358979;

int N;
int A[MAX], B[MAX];
ii elem[MAX];
int tree[MAXT];
vector<bool> swapped(MAX, false);
int prepare(int b, int e, int node){
	if(b == e){
		return tree[node] = e;
	}

	int m = (b+e)>>1, node2 = node << 1;
	int l = prepare(b,m,node2+1);
	int r = prepare(m+1,e,node2+2);
	if(elem[l].f <= elem[r].f){
		return tree[node] = l;
	}
	return tree[node] = r;
}

int update(int b, int e, int node, int p){
	if(p < b || p > e) return tree[node];
	if(b == e) return tree[node] = e;
	int m = (b+e)>>1, node2 = node << 1;
	int l = update(b,m,node2+1,p);
	int r = update(m+1,e,node2+2,p);
	if(elem[l].f <= elem[r].f){
		return tree[node] = l;
	}
	return tree[node] = r;
}

int query(int bq, int eq, int node, int bt, int et){
	if(bt > eq || et < bq) return -1;
 
	if(bq <= bt && et <= eq) return tree[node];
 
	int mid = (bt + et) >> 1;
	int leftMin = query(bq, eq, (node<<1) + 1, bt, mid);
	int rightMin = query(bq, eq, (node<<1) + 2, mid+1, et);
 
	if(leftMin == -1) return rightMin;
	if(rightMin == -1) return leftMin;
	
	return elem[leftMin].f <= elem[rightMin].f ? leftMin : rightMin;
}

int bs_lower(int b, int Ai){
	int e = N-1, f = -1;
	while(b <= e){
		int m = (b+e)>>1;
		if(elem[m].s >= Ai){
			e = m-1;
			f = m;
		}
		else{
			b = m+1;
		}
	}
	return f;
}

bool cmp(const ii &a, const ii &b){
	if(a.s != b.s) return a.s < b.s;
	return a.f > a.f;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	cin >> N;
	for(int i = 0; i < N; i++){
		cin >> elem[i].f;
		A[i] = elem[i].f;
	}

	for(int i = 0; i < N; i++){
		cin >> elem[i].s;
		B[i] = elem[i].s;
	}

	sort(A,A+N);
	sort(B,B+N);
	for(int i = 0; i < N; i++){
		if(A[i] > B[i]){
			cout << "No" << endl;
			return 0;
		}			
	}

	sort(elem, elem+N, cmp);

	prepare(0, N-1, 0);

	int swaps = 0;
	for(int i = 0; i < N; i++){
		if(elem[i].f <= elem[i].s){
			if(!swapped[i]){
				cout << "Yes" << endl;
				return 0;
			}
			continue;
		}

		swaps++;

		int j = bs_lower(i+1,elem[i].f);
		if(j != -1){
			int l = query(j,N-1,0,0,N-1);
			if(elem[l].f <= elem[i].s){
//				cout << "swap " << i << " " << l << endl;
				int s = elem[l].f;
				elem[l].f = elem[i].f;
				update(0,N-1,0,l);
				elem[i].f = s;
				update(0,N-1,0,i);
				swapped[l] = swapped[i] = true;
				continue ;
			}
		}
		
		int l = query(i+1,N-1,0,0,N-1);
		if(elem[l].f > elem[i].s){
			cout << "No" << endl;
			return 0;
		}
//		cout << "swap " << i << " " << l << endl;
		int s = elem[l].f;
		elem[l].f = elem[i].f;
		update(0,N-1,0,l);
		elem[i].f = s;
		update(0,N-1,0,i);
		swapped[l] = swapped[i] = true;
	}

	cout << ((swaps <= N-2) ? "Yes" : "No") << endl;
	return 0;
}

Submission Info

Submission Time
Task C - Swaps
User mdallrosa
Language C++14 (GCC 5.4.1)
Score 0
Code Size 3268 Byte
Status WA
Exec Time 114 ms
Memory 2816 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 600
Status
AC × 3
AC × 37
WA × 2
Set Name Test Cases
Sample sample01.txt, sample02.txt, sample03.txt
All sample01.txt, sample02.txt, sample03.txt, in01.txt, in02.txt, in03.txt, in04.txt, in05.txt, in06.txt, in07.txt, in08.txt, in09.txt, in10.txt, in11.txt, in12.txt, in13.txt, in14.txt, in15.txt, in16.txt, in17.txt, in18.txt, in19.txt, in20.txt, in21.txt, in22.txt, in23.txt, in24.txt, in25.txt, in26.txt, in27.txt, in28.txt, in29.txt, in30.txt, in31.txt, in32.txt, in33.txt, sample01.txt, sample02.txt, sample03.txt
Case Name Status Exec Time Memory
in01.txt AC 112 ms 2816 KB
in02.txt AC 105 ms 2688 KB
in03.txt AC 104 ms 2688 KB
in04.txt AC 111 ms 2816 KB
in05.txt AC 112 ms 2816 KB
in06.txt AC 78 ms 2816 KB
in07.txt AC 79 ms 2816 KB
in08.txt AC 76 ms 2816 KB
in09.txt AC 74 ms 2816 KB
in10.txt AC 76 ms 2816 KB
in11.txt AC 78 ms 2688 KB
in12.txt AC 104 ms 2688 KB
in13.txt AC 105 ms 2688 KB
in14.txt AC 78 ms 2816 KB
in15.txt AC 64 ms 2688 KB
in16.txt AC 110 ms 2816 KB
in17.txt AC 106 ms 2816 KB
in18.txt AC 111 ms 2816 KB
in19.txt AC 106 ms 2816 KB
in20.txt AC 114 ms 2816 KB
in21.txt AC 92 ms 2816 KB
in22.txt WA 93 ms 2816 KB
in23.txt AC 89 ms 2816 KB
in24.txt AC 84 ms 2816 KB
in25.txt WA 89 ms 2816 KB
in26.txt AC 27 ms 1024 KB
in27.txt AC 29 ms 1024 KB
in28.txt AC 30 ms 1408 KB
in29.txt AC 2 ms 384 KB
in30.txt AC 5 ms 384 KB
in31.txt AC 60 ms 1664 KB
in32.txt AC 27 ms 1024 KB
in33.txt AC 37 ms 1536 KB
sample01.txt AC 1 ms 256 KB
sample02.txt AC 1 ms 256 KB
sample03.txt AC 1 ms 256 KB