Submission #599607

#TimeUsernameProblemLanguageResultExecution timeMemory
599607beaconmcSorting (IOI15_sorting)C++14
100 / 100
486 ms46556 KiB
//#include "sorting.h"
#include <bits/stdc++.h>
#pragma GCC optimize ("Ofast")
#pragma GCC target ("avx2")

typedef long long ll;
using namespace std;

#define ll int
#define FOR(i, x, y) for(ll i=x; i<y; i++)
#define FORNEG(i, x, y) for(ll i=x; i>y; i--)
#define fast() ios_base::sync_with_stdio(false);cin.tie(NULL)

ll n,s[600001],m,x[600001],y[600001],p[600001],q[600001], realsus[600001];
vector<vector<ll>> temp;

int check(ll k){
	vector<ll> sus;

	temp.clear();
	FOR(i,0,n){
		sus.push_back(s[i]);
		realsus[s[i]] = i;
	}

	FOR(i,0,k){
		swap(realsus[sus[x[i]]], realsus[sus[y[i]]]);
		swap(sus[x[i]], sus[y[i]]);
	}

	cout << endl;

	ll ans = 0;
	FOR(i,0,n){
		if (realsus[i]==i) continue;
		ans++;
		ll amogus = sus[i];
		temp.push_back({i, sus[i]});

		swap(sus[realsus[i]], sus[i]);
		swap(realsus[i], realsus[amogus]);
	}
	if (ans<k){
		FOR(i,0,k-ans){
			temp.push_back({0,0});
		}
	}
	if (ans <= k) return 1;
	else return 0;
}

void genans(ll k){
	vector<ll> sus;
	unordered_map<ll, ll> realsus;

	FOR(i,0,n){
		sus.push_back(s[i]);
		realsus[s[i]] = i;
	}


	FOR(i,0,k){
		swap(realsus[sus[x[i]]], realsus[sus[y[i]]]);
		swap(sus[x[i]], sus[y[i]]);

		p[i] = realsus[temp[i][0]];
		q[i] = realsus[temp[i][1]];

		swap(sus[realsus[temp[i][0]]], sus[realsus[temp[i][1]]]);
		swap(realsus[temp[i][0]], realsus[temp[i][1]]);
	}
}

int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
	n = N;
	FOR(i,0,n){
		s[i] = S[i]; 
	}
	m = M;
	FOR(i,0,m){
		x[i] = X[i];
		y[i] = Y[i];
	}
	ll lo = 0;
	ll hi = n+1;

	while (lo<hi){
		int mid = lo + (hi - lo) / 2;
		if (check(mid)) {
			hi = mid;
		} else {
			lo = mid + 1;
		}
	}
	check(lo);
	genans(lo);
	FOR(i,0,lo){
		P[i] = p[i];
		Q[i] = q[i];
	}

	return lo;
}


Compilation message (stderr)

sorting.cpp: In function 'void genans(int)':
sorting.cpp:54:24: warning: declaration of 'realsus' shadows a global declaration [-Wshadow]
   54 |  unordered_map<ll, ll> realsus;
      |                        ^~~~~~~
sorting.cpp:14:59: note: shadowed declaration is here
   14 | ll n,s[600001],m,x[600001],y[600001],p[600001],q[600001], realsus[600001];
      |                                                           ^~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...