Submission #441883

# Submission time Handle Problem Language Result Execution time Memory
441883 2021-07-06T13:30:44 Z rainboy Teleporters (IOI08_teleporters) C
90 / 100
1000 ms 27620 KB
#include <stdio.h>

#define N	1000000

unsigned int X = 12345;

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

int xx[N * 2];

int compare_x(int i, int j) { return xx[i] - xx[j]; }
int compare_(int a, int b) { return b - a; }

int (*compare)(int, int);

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) {
			int c = compare(aa[j], a);

			if (c == 0)
				j++;
			else if (c < 0) {
				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 main() {
	static int ii[N * 2], pp[N * 2], cc[N * 2];
	static char visited[N * 2];
	int n, n_, m, h, i, ans;

	scanf("%d%d", &n, &m);
	for (i = 0; i < n * 2; i++) {
		scanf("%d", &xx[i]);
		ii[i] = i;
	}
	compare = compare_x, sort(ii, 0, n * 2);
	for (h = 0; h < n * 2; h++)
		pp[ii[h]] = h + 1 == n * 2 ? -1 : ii[h + 1] ^ 1;
	ans = 0;
	for (i = ii[0] ^ 1; i != -1; i = pp[i])
		visited[i] = 1, ans++;
	n_ = 0;
	for (i = 0; i < n * 2; i++)
		if (!visited[i]) {
			int c;

			c = 0;
			while (!visited[i]) {
				visited[i] = 1, c++;
				i = pp[i];
			}
			cc[n_++] = c;
			while (visited[i] == 1) {
				visited[i] = 2;
				i = pp[i];
			}
		}
	compare = compare_, sort(cc, 0, n_);
	for (i = 0; i < n_ && i < m; i++)
		ans += cc[i];
	ans += m * 2 - (m > n_ ? (m - n_) % 2 : 0);
	printf("%d\n", ans);
	return 0;
}

Compilation message

teleporters.c: In function 'main':
teleporters.c:45:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   45 |  scanf("%d%d", &n, &m);
      |  ^~~~~~~~~~~~~~~~~~~~~
teleporters.c:47:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |   scanf("%d", &xx[i]);
      |   ^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 7 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 332 KB Output is correct
2 Correct 8 ms 628 KB Output is correct
3 Correct 19 ms 972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 113 ms 3700 KB Output is correct
2 Correct 269 ms 8284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 193 ms 6140 KB Output is correct
2 Correct 398 ms 12168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 555 ms 17860 KB Output is correct
2 Correct 743 ms 21496 KB Output is correct
3 Correct 767 ms 24240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1057 ms 24092 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1088 ms 27620 KB Time limit exceeded
2 Halted 0 ms 0 KB -