Submission #472986

# Submission time Handle Problem Language Result Execution time Memory
472986 2021-09-14T17:31:12 Z rainboy Karte (COCI18_karte) C
120 / 120
225 ms 7108 KB
#include <stdio.h>

#define N	500000

unsigned int X = 12345;

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

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

		while (j < k)
			if (aa[j] == a)
				j++;
			else if (aa[j] < a) {
				tmp = aa[i], aa[i] = aa[j], aa[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = aa[j], aa[j] = aa[k], aa[k] = tmp;
			}
		sort(aa, l, i);
		l = k;
	}
}

int eval(int *aa, int *bb, int n, long long c) {
	int i, j, k;

	i = 0;
	while (c > n - 1 - i)
		c -= n - 1 - i++;
	for (j = 0; j < i; j++)
		bb[n - 1 - j] = aa[j];
	for (j = 0; j < c; j++)
		bb[j] = aa[i + 1 + j];
	bb[c] = aa[i];
	for (j = c + 1; j < n - i; j++)
		bb[j] = aa[i + j];
	k = 0;
	for (i = n - 1; i >= 0; i--)
		if (k < bb[i])
			k++;
	return k;
}

int main() {
	static int aa[N], bb[N];
	int n, k, i;
	long long lower, upper;

	scanf("%d%d", &n, &k);
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	sort(aa, 0, n);
	lower = 0, upper = (long long) n * (n - 1) / 2 + 1;
	while (upper - lower > 1) {
		long long c = (lower + upper) / 2;

		if (eval(aa, bb, n, c) <= k)
			lower = c;
		else
			upper = c;
	}
	if (eval(aa, bb, n, lower) != k)
		printf("-1\n");
	else {
		for (i = 0; i < n; i++)
			printf("%d ", bb[i]);
		printf("\n");
	}
	return 0;
}

Compilation message

karte.c: In function 'main':
karte.c:55:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |  scanf("%d%d", &n, &k);
      |  ^~~~~~~~~~~~~~~~~~~~~
karte.c:57:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   57 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 1476 KB Output is correct
2 Correct 34 ms 1656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 75 ms 2756 KB Output is correct
2 Correct 60 ms 3032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 225 ms 6668 KB Output is correct
2 Correct 156 ms 7108 KB Output is correct