답안 #88784

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
88784 2018-12-08T16:56:42 Z qkxwsm 정렬하기 (IOI15_sorting) C++14
100 / 100
693 ms 21088 KB
#include "sorting.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

random_device(rd);
mt19937 rng(rd());
const long long FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();

struct custom_hash
{
	template<class T>
	unsigned long long operator()(T v) const
	{
		unsigned long long x = v;
		x += FIXED_RANDOM; x += 11400714819323198485ull;
		x = (x ^ (x >> 30)) * 13787848793156543929ull;
		x = (x ^ (x >> 27)) * 10723151780598845931ull;
		return x ^ (x >> 31);
	}
};

template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T, class U> using hash_table = gp_hash_table<T, U, custom_hash>;

template<class T>
void ckmin(T &a, T b)
{
	a = min(a, b);
}
template<class T>
void ckmax(T &a, T b)
{
	a = max(a, b);
}
long long expo(long long a, long long e, long long mod)
{
	return ((e == 0) ? 1 : ((expo(a * a % mod, e >> 1, mod)) * ((e & 1) ? a : 1) % mod));
}
template<class T, class U>
T nmod(T &x, U mod)
{
	if (x >= mod) x -= mod;
}
template<class T>
T gcd(T a, T b)
{
	return (b ? gcd(b, a % b) : a);
}
template<class T>
T randomize(T mod)
{
	return (uniform_int_distribution<T>(0, mod - 1))(rng);
}

#define y0 ___y0
#define y1 ___y1
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define PF push_front
#define fi first
#define se second
#define DBG(x) cerr << #x << " = " << x << endl;
#define SZ(x) ((int) (x.size()))
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define ALL(x) x.begin(), x.end()

const long double PI = 4.0 * atan(1.0);
const long double EPS = 1e-9;

#define MAGIC 347
#define SINF 10007
#define CO 1000007
#define INF 1000000007
#define BIG 1000000931
#define LARGE 1696969696967ll
#define GIANT 2564008813937411ll
#define LLINF 2696969696969696969ll
#define MAXN 600013

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<pdd> vpd;


int N, M;
int arr[MAXN];
pii swp[MAXN];
vpi ans;
int ord[MAXN], rord[MAXN], val[MAXN], pos[MAXN];

void sw(int p, int q, bool flag)
{
	//POSITION p, q!
	swap(val[p], val[q]);
	swap(pos[val[p]], pos[val[q]]);
	if (!flag)
	{
		swap(rord[p], rord[q]);
		swap(ord[rord[p]], ord[rord[q]]);
	}
	else
	{
		ans.PB({p, q});
	}
	// cerr << "state:\n";
	// FOR(i, 0, N)
	// {
	// 	cerr << rord[i] << ' ';
	// }
	// cerr << endl;
	// FOR(i, 0, N)
	// {
	// 	cerr << val[i] << ' ';
	// }
	// cerr << endl;
	// cerr << p << ' ' << q << endl;
}
bool check(int x)
{
	FOR(i, 0, N)
	{
		ord[i] = i;
		rord[i] = i;
		val[i] = arr[i];
		pos[arr[i]] = i;
	}
	FORD(i, x, 0)
	{
		//ord: a[i] = x => the one that will end up at position i is currently at x
		//rord: a[i] = x => the one that is currently at i will end up at position x
		pii p = swp[i];
		swap(rord[p.fi], rord[p.se]);
		swap(ord[rord[p.fi]], ord[rord[p.se]]);
	}
	// cerr << "state:\n";
	// FOR(i, 0, N)
	// {
	// 	cerr << rord[i] << ' ';
	// }
	// cerr << endl;
	// FOR(i, 0, N)
	// {
	// 	cerr << arr[i] << ' ';
	// }
	// cerr << endl;
	// FOR(i, 0, N)
	// {
	// 	cerr << ord[i] << ' ';
	// }
	// cerr << endl;
	ans.clear();
	int iter = 0;
	FOR(i, 0, x)
	{
		pii p = swp[i];
		sw(p.fi, p.se, 0);
		while (iter < N && rord[pos[iter]] == iter)
		{
			iter++;
		}
		// DBG(iter);
		if (iter == N)
		{
			int x = randomize(N);
			ans.PB({x, x});
			continue;
		}
		sw(pos[iter], ord[iter], 1);
	}
	while (iter < N && rord[pos[iter]] == iter)
	{
		iter++;
	}
	return (iter == N);
}
int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[])
{
	N = n;
	FOR(i, 0, N)
	{
		arr[i] = s[i];
	}
	M = m;
	FOR(i, 0, M)
	{
		swp[i] = {x[i], y[i]};
	}
	int lo = 0, hi = M;
	while(hi > lo)
	{
		int mid = (hi + lo) >> 1;
		if (check(mid))
		{
			hi = mid;
		}
		else
		{
			lo = mid + 1;
		}
	}
	// DBG(lo);
	check(lo);
	FOR(i, 0, SZ(ans))
	{
		p[i] = ans[i].fi;
		q[i] = ans[i].se;
	}
	return SZ(ans);
}

Compilation message

sorting.cpp: In function 'bool check(int)':
sorting.cpp:180:8: warning: declaration of 'int x' shadows a parameter [-Wshadow]
    int x = randomize(N);
        ^
sorting.cpp:134:16: note: shadowed declaration is here
 bool check(int x)
                ^
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 3 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 3 ms 512 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 3 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 3 ms 512 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 3 ms 512 KB Output is correct
16 Correct 3 ms 512 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 4 ms 384 KB Output is correct
21 Correct 4 ms 768 KB Output is correct
22 Correct 4 ms 768 KB Output is correct
23 Correct 5 ms 768 KB Output is correct
24 Correct 4 ms 768 KB Output is correct
25 Correct 5 ms 768 KB Output is correct
26 Correct 5 ms 768 KB Output is correct
27 Correct 4 ms 768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 640 KB Output is correct
2 Correct 4 ms 640 KB Output is correct
3 Correct 4 ms 640 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
5 Correct 4 ms 512 KB Output is correct
6 Correct 4 ms 640 KB Output is correct
7 Correct 4 ms 640 KB Output is correct
8 Correct 4 ms 640 KB Output is correct
9 Correct 4 ms 640 KB Output is correct
10 Correct 4 ms 640 KB Output is correct
11 Correct 4 ms 640 KB Output is correct
12 Correct 3 ms 640 KB Output is correct
13 Correct 3 ms 640 KB Output is correct
14 Correct 4 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 640 KB Output is correct
2 Correct 4 ms 640 KB Output is correct
3 Correct 4 ms 640 KB Output is correct
4 Correct 3 ms 512 KB Output is correct
5 Correct 4 ms 512 KB Output is correct
6 Correct 4 ms 640 KB Output is correct
7 Correct 4 ms 640 KB Output is correct
8 Correct 4 ms 640 KB Output is correct
9 Correct 4 ms 640 KB Output is correct
10 Correct 4 ms 640 KB Output is correct
11 Correct 4 ms 640 KB Output is correct
12 Correct 3 ms 640 KB Output is correct
13 Correct 3 ms 640 KB Output is correct
14 Correct 4 ms 640 KB Output is correct
15 Correct 423 ms 18784 KB Output is correct
16 Correct 382 ms 19168 KB Output is correct
17 Correct 589 ms 20192 KB Output is correct
18 Correct 136 ms 17652 KB Output is correct
19 Correct 323 ms 18528 KB Output is correct
20 Correct 341 ms 19172 KB Output is correct
21 Correct 376 ms 19172 KB Output is correct
22 Correct 464 ms 20448 KB Output is correct
23 Correct 454 ms 20960 KB Output is correct
24 Correct 693 ms 20668 KB Output is correct
25 Correct 606 ms 20448 KB Output is correct
26 Correct 474 ms 19168 KB Output is correct
27 Correct 598 ms 18520 KB Output is correct
28 Correct 496 ms 20292 KB Output is correct
29 Correct 543 ms 19808 KB Output is correct
30 Correct 420 ms 18536 KB Output is correct
31 Correct 600 ms 20172 KB Output is correct
32 Correct 402 ms 20164 KB Output is correct
33 Correct 578 ms 20452 KB Output is correct
34 Correct 589 ms 20548 KB Output is correct
35 Correct 326 ms 18260 KB Output is correct
36 Correct 97 ms 18536 KB Output is correct
37 Correct 591 ms 21088 KB Output is correct
38 Correct 430 ms 20304 KB Output is correct
39 Correct 463 ms 20436 KB Output is correct