Submission #1071661

#TimeUsernameProblemLanguageResultExecution timeMemory
1071661rainboyPizza Party (CCO24_day1problem2)C11
0 / 12
569 ms46160 KiB
#include <stdio.h>

#define N	1000000

int max(int a, int b) { return a > b ? a : b; }

unsigned int Z = 12345;

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

int *xx;

void sort(int *ii, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;

		while (j < k) {
			int c = xx[ii[j]] != xx[i_] ? xx[ii[j]] - xx[i_] : ii[j] - i_;

			if (c == 0)
				j++;
			else if (c < 0) {
				tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
			}
		}
		sort(ii, l, i);
		l = k;
	}
}

int ft[N];

void update(int i, int n, int x) {
	while (i < n) {
		ft[i] = max(ft[i], x);
		i |= i + 1;
	}
}

int query(int i) {
	int x = 0;

	while (i >= 0) {
		x = max(x, ft[i]);
		i &= i + 1, i--;
	}
	return x;
}

int main() {
	static int aa[N], bb[N], ii[N], jj[N], jj_[N];
	int n, h, i, j;

	scanf("%d", &n);
	for (i = 0; i < n; i++)
		scanf("%d", &aa[n - 1 - i]);
	for (j = 0; j < n; j++)
		scanf("%d", &bb[j]);
	for (i = 0; i < n; i++)
		ii[i] = i;
	xx = aa, sort(ii, 0, n);
	for (j = 0; j < n; j++)
		jj[j] = j;
	xx = bb, sort(jj, 0, n);
	for (h = 0; h < n; h++) {
		if (aa[ii[h]] != bb[jj[h]]) {
			printf("-1\n");
			return 0;
		}
		jj_[ii[h]] = jj[h];
	}
	for (i = n - 1; i >= 0; i--) {
		j = jj_[i];
		update(j, n, aa[i] = bb[j] = query(j) + 1);
	}
	printf("%d\n", query(n - 1));
	for (i = n - 1; i >= 0; i--)
		printf("%d ", aa[i]);
	printf("\n");
	for (j = 0; j < n; j++)
		printf("%d ", bb[j]);
	printf("\n");
	return 0;
}

Compilation message (stderr)

Main.c: In function 'main':
Main.c:60:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
Main.c:62:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |   scanf("%d", &aa[n - 1 - i]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:64:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |   scanf("%d", &bb[j]);
      |   ^~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...