제출 #1237441

#제출 시각아이디문제언어결과실행 시간메모리
1237441Zbyszek99정렬하기 (IOI15_sorting)C++20
100 / 100
104 ms16544 KiB
#include "sorting.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//mt19937 mt;void random_start(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll los(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;

int S[200001];
int X[600001];
int Y[600001];
bool odw[200001];
int cur_poz[200001];
int n,m;

bool check(int x)
{
	rep(i,x)
	{
		swap(S[X[i]],S[Y[i]]);
	}
	rep(i,n) odw[i] = 0;
	int cycles = 0;
	rep(i,n)
	{
		if(!odw[i])
		{
			cycles++;
			int v = i;
			while(odw[v] == 0)
			{
				odw[v] = 1;
				v = S[v];
			}
		}
	}
	for(int i = x-1; i >= 0; i--)
	{
		swap(S[X[i]],S[Y[i]]);
	}
	return (n-cycles <= x);
}

int findSwapPairs(int N, int S2[], int M, int X2[], int Y2[], int P[], int Q[]) 
{
	n = N;
	m = M;
	rep(i,n)
	{
		S[i] = S2[i];
	}
	rep(i,m)
	{
		X[i] = X2[i];
		Y[i] = Y2[i];
	}
    int l = 0;
	int r = M;
	int R = 0;
	while(l <= r)
	{
		int mid = (l+r)/2;
		if(check(mid))
		{
			R = mid;
			r = mid-1;
		}
		else
		{
			l = mid+1;
		}
	}
	rep(i,n) cur_poz[i] = i;
	rep(i,R)
	{
		swap(S[X[i]],S[Y[i]]);
	}
	vector<pii> opers;
	rep(i,n) odw[i] = 0;
	rep(i,n)
	{
		if(odw[i] == 0)
		{
			int cycle_siz = -1;
			int v = i;
			while(odw[v] == 0)
			{
				odw[v] = 1;
				cycle_siz++;
				v = S[v];
			}
			vector<pii> St;
			rep(j,cycle_siz)
			{
				opers.pb({S[i],S[S[i]]});
				St.pb({S[i],i});
				swap(S[S[i]],S[i]);
			}
			reverse(all(St));
			forall(it,St)
			{
				swap(S[it.ff],S[it.ss]);
			}
		}
	}
	for(int i = R-1; i >= 0; i--)
	{
		swap(S[X[i]],S[Y[i]]);
	}
	rep(i,n)
	{
		cur_poz[S[i]] = i;
	}
	rep(i,R)
	{
		swap(cur_poz[S[X[i]]],cur_poz[S[Y[i]]]);
		swap(S[X[i]],S[Y[i]]);
		P[i] = cur_poz[opers[i].ff];
		Q[i] = cur_poz[opers[i].ss];
		swap(cur_poz[S[P[i]]],cur_poz[S[Q[i]]]);
		swap(S[P[i]],S[Q[i]]);
	}
	return R;
}


#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...