답안 #873246

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
873246 2023-11-14T17:00:25 Z rainboy Shuffle (NOI19_shuffle) C++17
95.7704 / 100
2 ms 2652 KB
#include "shuffle.h"
#include <cstring>
#include <vector>

using namespace std;

const int N = 1000, L = 10;	/* L = ceil(log2(N)) */

typedef vector<int> vi;
typedef vector<vi> vvi;

int n, m, k;
int pp[L], xx[N], idx[N * N], xx_[N], ii[N];
int uu[N], vv[N];
int dd[N];

void shuffle_(vvi iii, vvi &aaa) {
	aaa = shuffle(iii);
	for (int d = 0; d < m; d++)
		for (int g = 0; g < k; g++)
			aaa[d][g]--;
}

vi solve(int n_, int m_, int k_, int q, int s) {
	n = n_, m = m_, k = k_;
	vvi iii(m), aaa, aaa_;
	vi aa(n);
	if (s == 3) {
		int l = 0, p = 1;
		while (p < n)
			pp[l++] = p, p *= m;
		for (int i = 0; i < n; i++) {
			int d = i % m, x = d;
			for (int h = 1; h < l; h++) {
				int d_ = (i / pp[h] + d) % m;
				x += d_ * pp[h];
			}
			idx[xx[i] = x] = i;
		}
		memset(xx_, 0, n * sizeof *xx_);
		for (int h = 0; h < l; h++) {
			for (int d = 0; d < m; d++)
				iii[d].clear();
			for (int i = 0; i < n; i++)
				iii[xx[i] / pp[h] % m].push_back(i + 1);
			shuffle_(iii, aaa);
			for (int d = 0; d < m; d++)
				for (int g = 0; g < k; g++)
					xx_[aaa[d][g]] += d * pp[h];
		}
		for (int a = 0; a < n; a++)
			aa[idx[xx_[a]]] = a + 1;
	} else if (s == 2 || s == 4) {
		for (int d = 0; d < n / 2; d++)
			iii[d].clear();
		for (int d = 0; d < n / 2; d++)
			iii[d] = { d * 2 + 1, d * 2 + 2 };
		shuffle_(iii, aaa);
		for (int d = 0; d < n / 2; d++) {
			int a = aaa[d][0], b = aaa[d][1];
			uu[a] = b, uu[b] = a;
		}
		for (int d = 0; d + 1 < n / 2; d++)
			iii[d] = { d * 2 + 2, d * 2 + 3 };
		iii[n / 2 - 1] = { n, 1 };
		shuffle_(iii, aaa);
		for (int d = 0; d < n / 2; d++) {
			int a = aaa[d][0], b = aaa[d][1];
			vv[a] = b, vv[b] = a;
		}
		for (int i = 2, j = n - 1; j - i > 3; i++, j--)
			iii[i - 2] = { i + 1, j + 1 };
		iii[n / 2 - 3] = { n / 2, n / 2 + 2 }, iii[n / 2 - 2] = { n / 2 + 1, n / 2 + 3 };
		iii[n / 2 - 1] = { 1, 2 };
		shuffle_(iii, aaa);
		int a_ = -1, b_ = -1;
		for (int d = 0; d < n / 2; d++) {
			int a = aaa[d][0], b = aaa[d][1];
			if (uu[a] == b) {
				a_ = a, b_ = b;
				break;
			}
		}
		for (int i = 1, j = n - 2; j - i > 3; i++, j--)
			iii[i - 1] = { i + 1, j + 1 };
		iii[n / 2 - 3] = { n / 2 - 1, n / 2 + 1 }, iii[n / 2 - 2] = { n / 2, n / 2 + 2 };
		iii[n / 2 - 1] = { n, 1 };
		shuffle_(iii, aaa);
		for (int d = 0; d < n / 2; d++) {
			int a = aaa[d][0], b = aaa[d][1];
			if (vv[a] == b) {
				if (b_ == a || b_ == b)
					a_ = b_;
				break;
			}
		}
		for (int i = 0, a = a_; i < n; i++) {
			aa[i] = a + 1;
			a = i % 2 == 0 ? uu[a] : vv[a];
		}
	} else if (s == 1 || s == 5) {
		int l = 0;
		while (1 << l < n)
			l++;
		l++;
		for (int i = 0; i < n; i++) {
			int x = (i < n - 2 ? i : 1 << l - 1 ^ i & 1) ^ ((i & 1) == 0 ? 0 : (1 << l) - 2);
			idx[xx[i] = x] = i;
		}
		memset(xx_, 0, n * sizeof *xx_);
		for (int h = 0; h < l; h++) {
			for (int d = 0; d < 2; d++)
				iii[d].clear();
			for (int i = 0; i < n; i++)
				iii[xx[i] >> h & 1].push_back(i + 1);
			shuffle_(iii, aaa);
			for (int g = 0; g < n / 2; g++)
				xx_[aaa[1][g]] ^= 1 << h;
		}
		for (int h = 1; h < l; h++) {
			int d = 0;
			for (int a = 0; a < n; a++)
				d += (xx_[a] >> h & 1) == (xx_[a] & 1) ? 1 : -1;
			if (d < 0)
				for (int a = 0; a < n; a++)
					xx_[a] ^= 1 << h;
		}
		for (int d = 0; d < 2; d++)
			iii[d].clear();
		iii[0].push_back(1);
		for (int i = 2; i <= n / 2; i++)
			iii[0].push_back(i + 1);
		iii[1].push_back(2);
		for (int i = n / 2 + 1; i < n; i++)
			iii[1].push_back(i + 1);
		shuffle_(iii, aaa);
		for (int d = 0; d < 2; d++)
			for (int g = 0; g < n / 2; g++)
				if (xx_[aaa[d][g]] == 0) {
					int flip = 1;
					for (g = 0; g < n / 2; g++)
						if (xx_[aaa[d][g]] == 2) {
							flip = 0;
							break;
						}
					if (flip)
						for (int a = 0; a < n; a++)
							xx_[a] ^= (1 << l) - 1;
					goto out;
				}
out:
		for (int a = 0; a < n; a++)
			aa[idx[xx_[a]]] = a + 1;
	} else {
		for (int d = 0; d < m; d++)
			iii[d].clear();
		for (int i = 0; i < n; i++)
			iii[i / k].push_back(i + 1);
		shuffle_(iii, aaa_);
		for (int d = 0; d < m; d++)
			for (int g = 0; g < k; g++)
				dd[aaa_[d][g]] = d;
		int l = 0;
		while (1 << l < k)
			l++;
		l++;
		for (int g = 0; g < k; g++) {
			int x = g + 1 < k ? g : 1 << l - 1;
			idx[xx[g] = x] = g;
		}
		memset(xx_, 0, n * sizeof *xx_);
		for (int h = 0; h < l; h++) {
			for (int d = 0; d < m; d++)
				iii[d].clear();
			for (int i = 0; i < n; i++)
				iii[(i / k + (xx[i % k] >> h & 1)) % m].push_back(i + 1);
			shuffle_(iii, aaa);
			for (int d = 0; d < m; d++) {
				int u = -1, ku = 0, v = -1, kv = 0;
				for (int g = 0; g < k; g++) {
					int w = dd[aaa[d][g]];
					if (u == -1 || w == u)
						u = w, ku++;
					else
						v = w, kv++;
				}
				if (ku > kv) {
					int tmp;
					tmp = u, u = v, v = tmp, tmp = ku, ku = kv, kv = tmp;
				}
				vv[u] = v;
				for (int g = 0; g < k; g++) {
					int a = aaa[d][g], w = dd[a];
					if (w == u)
						xx_[a] |= 1 << h;
				}
			}
		}
		for (int d = 0; d < m; d++)
			iii[d].clear();
		for (int i = 0; i < k; i++)
			iii[0].push_back(i + 1);
		for (int i = k; i < n; i++)
			iii[i / k].push_back((i - k + 1) % (n - k) + k + 1);
		shuffle_(iii, aaa);
		for (int d = 0; d < m; d++) {
			int u = -1;
			for (int g = 0; g < k; g++) {
				int w = dd[aaa[d][g]];
				if (u == -1 || w == u)
					u = w;
				else {
					u = -1;
					break;
				}
			}
			if (u != -1) {
				for (int d = 0; d < m; d++, u = vv[u])
					for (int g = 0; g < k; g++) {
						int a = aaa_[u][g];
						aa[d * k + idx[xx_[a]]] = a + 1;
					}
				break;
			}
		}
	}
	return aa;
}

Compilation message

shuffle.cpp: In function 'vi solve(int, int, int, int, int)':
shuffle.cpp:107:36: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
  107 |    int x = (i < n - 2 ? i : 1 << l - 1 ^ i & 1) ^ ((i & 1) == 0 ? 0 : (1 << l) - 2);
      |                                  ~~^~~
shuffle.cpp:107:44: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
  107 |    int x = (i < n - 2 ? i : 1 << l - 1 ^ i & 1) ^ ((i & 1) == 0 ? 0 : (1 << l) - 2);
      |                                          ~~^~~
shuffle.cpp:168:35: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
  168 |    int x = g + 1 < k ? g : 1 << l - 1;
      |                                 ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2648 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 516 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Partially correct 1 ms 348 KB Partially correct
4 Partially correct 1 ms 348 KB Partially correct
5 Correct 1 ms 348 KB Output is correct
6 Partially correct 1 ms 348 KB Partially correct
7 Correct 1 ms 348 KB Output is correct
8 Partially correct 1 ms 348 KB Partially correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Partially correct 1 ms 348 KB Partially correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Partially correct 1 ms 348 KB Partially correct
16 Partially correct 1 ms 348 KB Partially correct
17 Correct 0 ms 348 KB Output is correct
18 Partially correct 1 ms 600 KB Partially correct
19 Partially correct 1 ms 348 KB Partially correct
20 Correct 1 ms 348 KB Output is correct