Submission #871382

# Submission time Handle Problem Language Result Execution time Memory
871382 2023-11-10T16:47:05 Z rainboy Secret Permutation (RMI19_permutation) C++17
66.0408 / 100
24 ms 592 KB
#include "permutation.h"
#include <cstring>
#include <vector>

using namespace std;

typedef vector<int> vi;

const int N = 256;

unsigned int X = 12345;

int rand_() {
	return (X *= 3) >> 1;
}

int abs_(int a) { return a > 0 ? a : -a; }
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }

int rr[N], dd[N], ss[N];
char usedl[N * 2 + 1], usedr[N * 2 + 1];

void solve(int n) {
	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];
	memset(ss, 0, n * sizeof *ss);
	for (int t = 0; t < n; t++)
		for (int i = 1; i + 1 < n; i++)
			if (ss[i + 1] == 0) {
				memset(usedl, 0, (n * 2 + 1) * sizeof *usedl);
				ss[i + 1] = 1;
				int lmn = 0, lmx = 0;
				for (int h = i, s = -1, l = 0; h > 0 && ss[h + 1]; h--) {
					s *= ss[h + 1], l += s == 1 ? dd[h] : -dd[h], lmn = min(lmn, l), lmx = max(lmx, l);
					usedl[n + l] = 1;
				}
				memset(usedr, 0, (n * 2 + 1) * sizeof *usedr);
				int rmn = 0, rmx = 0;
				for (int j = i + 1, s = 1, r = 0; j < n && ss[j]; j++) {
					s *= ss[j], r += s == 1 ? dd[j] : -dd[j], rmn = min(rmn, r), rmx = max(rmx, r);
					usedr[n + r] = 1;
				}
				ss[i + 1] = 0;
				if (lmx - rmn >= n || rmx - lmn >= n)
					ss[i + 1] = -1;
				else if (lmx - -rmx >= n || -rmn - lmn >= n)
					ss[i + 1] = 1;
				else
					for (int x = -n; x <= n; x++) {
						if (usedl[n + x] && usedr[n + x]) {
							ss[i + 1] = -1;
							break;
						}
						if (usedl[n + x] && usedr[n - x]) {
							ss[i + 1] = 1;
							break;
						}
					}
			}
	vi pp(n), qq(n);
	pp[0] = 0, pp[1] = dd[1];
	for (int i = 2; i < n; i++)
		if (ss[i])
			pp[i] = pp[i - 1] + ((pp[i - 2] < pp[i - 1]) == (ss[i] == 1) ? dd[i] : -dd[i]);
		else {
			for (int h = 0; h < n; h++)
				ii[h] = rr[h < i ? i - 1 - h : h] + 1;
			pp[i] = query(ii) - (sum - dd[0]) + dd[i];
			if (abs_(pp[i] - pp[i - 1]) != dd[i])
				pp[i] = -pp[i];
		}
	int p = 0;
	for (int i = 0; i < n; i++)
		p = min(p, pp[i]);
	for (int i = 0; i < n; i++)
		pp[i] -= p - 1;
	for (int i = 0; i < n; i++)
		qq[rr[i]] = pp[i];
	for (int i = 0; i < n; i++)
		pp[i] = qq[i];
	answer(pp);
}

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 Partially correct 0 ms 344 KB Partially correct
2 Correct 0 ms 344 KB Output is correct
3 Partially correct 0 ms 344 KB Partially correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Partially correct 0 ms 344 KB Partially correct
2 Correct 0 ms 344 KB Output is correct
3 Partially correct 0 ms 344 KB Partially correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Partially correct 2 ms 344 KB Partially correct
8 Partially correct 1 ms 344 KB Partially correct
9 Partially correct 1 ms 344 KB Partially correct
10 Partially correct 1 ms 344 KB Partially correct
11 Partially correct 1 ms 344 KB Partially correct
12 Partially correct 1 ms 344 KB Partially correct
13 Partially correct 1 ms 344 KB Partially correct
14 Partially correct 1 ms 344 KB Partially correct
15 Partially correct 1 ms 344 KB Partially correct
16 Partially correct 1 ms 344 KB Partially correct
# Verdict Execution time Memory Grader output
1 Partially correct 0 ms 344 KB Partially correct
2 Correct 0 ms 344 KB Output is correct
3 Partially correct 0 ms 344 KB Partially correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Partially correct 2 ms 344 KB Partially correct
8 Partially correct 1 ms 344 KB Partially correct
9 Partially correct 1 ms 344 KB Partially correct
10 Partially correct 1 ms 344 KB Partially correct
11 Partially correct 1 ms 344 KB Partially correct
12 Partially correct 1 ms 344 KB Partially correct
13 Partially correct 1 ms 344 KB Partially correct
14 Partially correct 1 ms 344 KB Partially correct
15 Partially correct 1 ms 344 KB Partially correct
16 Partially correct 1 ms 344 KB Partially correct
17 Partially correct 21 ms 424 KB Partially correct
18 Partially correct 23 ms 424 KB Partially correct
19 Partially correct 18 ms 424 KB Partially correct
20 Partially correct 24 ms 416 KB Partially correct
21 Partially correct 20 ms 404 KB Partially correct
22 Partially correct 18 ms 592 KB Partially correct
23 Partially correct 18 ms 424 KB Partially correct
24 Partially correct 21 ms 420 KB Partially correct
25 Partially correct 20 ms 428 KB Partially correct
26 Partially correct 23 ms 404 KB Partially correct