Submission #871447

# Submission time Handle Problem Language Result Execution time Memory
871447 2023-11-10T20:18:03 Z rainboy Secret Permutation (RMI19_permutation) C++17
100 / 100
459 ms 55024 KB
#include "permutation.h"
#include <cstring>
#include <vector>
 
using namespace std;
 
typedef vector<int> vi;
 
const int N = 256, M = 260000;
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
 
int abs_(int a) { return a > 0 ? a : -a; }
int min(int a, int b) { return a < b ? a : b; }
 
unsigned int X = 133333333;
 
int rand_() {
	return (X *= 3) >> 1;
}
 
char used[N]; int kk[N];
 
int check(short *aa, int n, int m) {
	int a = 0;
	for (int i = 0; i < m; i++)
		a = min(a, aa[i]);
	memset(used, 0, n * sizeof *used);
	for (int i = 0; i < m; i++) {
		if (aa[i] - a >= n || used[aa[i] - a])
			return 0;
		used[aa[i] - a] = 1;
	}
	return 1;
}
 
int rr[N], dd[N];
short aaa[M][N], aaa_[M][N];
 
void solve(int n) {
	if (n == 4) {
		vi aa(n);
		int i = 1, j = 2, k = 3, l = 4;
		int x = query({ i, j, k, l });
		int y = query({ i, k, j, l });
		int tmp;
		if (x == y)
			tmp = j, j = l, l = tmp;
		else if (x > y)
			tmp = j, j = k, k = tmp;
		int z = query({ i, j, k, l });
		if (z == 4) {
			tmp = i, i = j, j = tmp;
			z = query({ i, j, k, l });
		}
		if (z == 5)
			tmp = i, i = j, j = tmp, tmp = k, k = l, l = tmp;
		aa[i - 1] = 1, aa[j - 1] = 2, aa[k - 1] = 3, aa[l - 1] = 4;
		answer(aa);
		return;
	}
	for (int i = 0; i < n; i++)
		rr[i] = i;
	for (int i = 0; i < n; i++) {
		int j = rand_() % (i + 1), tmp;
		tmp = rr[i], rr[i] = rr[j], rr[j] = tmp;
	}
	vi ii(n);
	for (int i = 0; i < n; i++) {
		for (int h = 0; h < n; h++)
			ii[h] = rr[(i + h) % n] + 1;
		dd[i] = query(ii);
	}
	int sum = 0;
	for (int i = 0; i < n; i++)
		sum += dd[i];
	sum /= n - 1;
	for (int i = 0; i < n; i++)
		dd[i] = sum - dd[i];
	vi aa(n), aa_(n);
	int m, m_;
	m = 0;
	aaa[m][0] = 0, aaa[m][1] = dd[1], m++;
	int j = 2;
	while (1) {
		while (j < n) {
			m_ = 0;
			for (int h = 0; h < m; h++) {
				memcpy(aaa_[m_], aaa[h], j * sizeof *aaa[h]), aaa_[m_][j] = aaa[h][j - 1] + dd[j];
				if (check(aaa_[m_], n, j + 1))
					m_++;
				if (m_ >= M)
					goto out;
				memcpy(aaa_[m_], aaa[h], j * sizeof *aaa[h]), aaa_[m_][j] = aaa[h][j - 1] - dd[j];
				if (check(aaa_[m_], n, j + 1))
					m_++;
				if (m_ >= M)
					goto out;
			}
			m = m_;
			for (int h = 0; h < m_; h++)
				memcpy(aaa[h], aaa_[h], (j + 1) * sizeof *aaa_[h]);
			j++;
		}
out:
		if (j == n) {
			m_ = 0;
			for (int h = 0; h < m; h++)
				if (abs_(aaa[h][n - 1]) == dd[0])
					memcpy(aaa_[m_++], aaa[h], n * sizeof *aaa[h]);
			m = m_;
			for (int h = 0; h < m_; h++)
				memcpy(aaa[h], aaa_[h], n * sizeof *aaa_[h]);
			if (m == 1) {
				for (int i = 0; i < n; i++)
					aa[i] = aaa[0][i];
				break;
			}
		}
		long long z_ = INF; int i_ = -1;
		for (int i = 0; i < j; i++) {
			memset(kk, 0, n * sizeof *kk);
			for (int h = 0; h < m; h++)
				kk[abs_(aaa[h][i])]++;
			long long z = 0;
			for (int a = 0; a < n; a++)
				z += (long long) kk[a] * kk[a];
			if (z_ > z)
				z_ = z, i_ = i;
		}
		for (int h = 0; h < n; h++)
			ii[h] = rr[h < i_ ? i_ - 1 - h : h] + 1;
		int a = query(ii) - (sum - dd[0]) + dd[i_];
		m_ = 0;
		for (int h = 0; h < m; h++)
			if (abs_(aaa[h][i_]) == a)
				memcpy(aaa_[m_++], aaa[h], n * sizeof *aaa[h]);
		m = m_;
		for (int h = 0; h < m_; h++)
			memcpy(aaa[h], aaa_[h], n * sizeof *aaa_[h]);
	}
	int a = 0;
	for (int i = 0; i < n; i++)
		a = min(a, aa[i]);
	for (int i = 0; i < n; i++)
		aa[i] -= a - 1;
	for (int i = 0; i < n; i++)
		aa_[rr[i]] = aa[i];
	for (int i = 0; i < n; i++)
		aa[i] = aa_[i];
	answer(aa);
}

Compilation message

stub.cpp: In function 'int query(int*)':
stub.cpp:15:9: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 |   fscanf(stdin, "%d", &x);
      |   ~~~~~~^~~~~~~~~~~~~~~~~
stub.cpp: In function 'int main(int, char**)':
stub.cpp:48:9: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |   fscanf(stdin, "%d", &N);
      |   ~~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2732 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 2480 KB Output is correct
4 Correct 0 ms 2480 KB Output is correct
5 Correct 0 ms 2480 KB Output is correct
6 Correct 0 ms 2488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2732 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 2480 KB Output is correct
4 Correct 0 ms 2480 KB Output is correct
5 Correct 0 ms 2480 KB Output is correct
6 Correct 0 ms 2488 KB Output is correct
7 Correct 1 ms 2488 KB Output is correct
8 Correct 2 ms 4532 KB Output is correct
9 Correct 1 ms 2480 KB Output is correct
10 Correct 1 ms 2484 KB Output is correct
11 Correct 1 ms 2480 KB Output is correct
12 Correct 1 ms 2480 KB Output is correct
13 Correct 1 ms 4528 KB Output is correct
14 Correct 1 ms 2484 KB Output is correct
15 Correct 1 ms 2484 KB Output is correct
16 Correct 1 ms 2480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2732 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 2480 KB Output is correct
4 Correct 0 ms 2480 KB Output is correct
5 Correct 0 ms 2480 KB Output is correct
6 Correct 0 ms 2488 KB Output is correct
7 Correct 1 ms 2488 KB Output is correct
8 Correct 2 ms 4532 KB Output is correct
9 Correct 1 ms 2480 KB Output is correct
10 Correct 1 ms 2484 KB Output is correct
11 Correct 1 ms 2480 KB Output is correct
12 Correct 1 ms 2480 KB Output is correct
13 Correct 1 ms 4528 KB Output is correct
14 Correct 1 ms 2484 KB Output is correct
15 Correct 1 ms 2484 KB Output is correct
16 Correct 1 ms 2480 KB Output is correct
17 Correct 175 ms 54712 KB Output is correct
18 Correct 144 ms 38320 KB Output is correct
19 Correct 37 ms 19884 KB Output is correct
20 Correct 138 ms 30144 KB Output is correct
21 Correct 177 ms 55024 KB Output is correct
22 Correct 14 ms 9652 KB Output is correct
23 Correct 88 ms 30132 KB Output is correct
24 Correct 16 ms 9652 KB Output is correct
25 Correct 459 ms 50672 KB Output is correct
26 Correct 38 ms 13740 KB Output is correct