답안 #730891

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
730891 2023-04-26T15:01:51 Z rainboy 바이오칩 (IZhO12_biochips) C
100 / 100
593 ms 15328 KB
#include <stdio.h>
#include <stdlib.h>

#define N	200000
#define INF	0x3f3f3f3f

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

unsigned int X = 12345;

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

int *ej[N], eo[N];

void append(int i, int j) {
	int o = eo[i]++;

	if (o >= 2 && (o & o - 1) == 0)
		ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
	ej[i][o] = j;
}

int ta[N], tb[N];

void dfs(int i) {
	static int time;
	int o;

	ta[i] = time++;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		dfs(j);
	}
	tb[i] = time;
}

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)
			if (tb[ii[j]] == tb[i_])
				j++;
			else if (tb[ii[j]] < tb[i_]) {
				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 main() {
	static int ww[N], ii[N], hh[N], dp[N];
	int n, k, h, h_, i, p, r, lower, upper;

	scanf("%d%d", &n, &k);
	for (i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
	r = -1;
	for (i = 0; i < n; i++) {
		scanf("%d%d", &p, &ww[i]), p--;
		if (p >= 0)
			append(p, i);
		else
			r = i;
	}
	dfs(r);
	for (i = 0; i < n; i++)
		ii[i] = i;
	sort(ii, 0, n);
	for (h = 0; h < n; h++) {
		lower = -1, upper = i;
		while (upper - lower > 1) {
			h_ = (lower + upper) / 2;
			if (tb[ii[h_]] <= ta[ii[h]])
				lower = h_;
			else
				upper = h_;
		}
		hh[h] = lower;
	}
	for (h = 0; h < n; h++)
		dp[h] = ww[ii[h]];
	for (h = 1; h < n; h++)
		dp[h] = max(dp[h], dp[h - 1]);
	while (--k) {
		for (h = n - 1; h >= 0; h--)
			dp[h] = hh[h] == -1 || dp[hh[h]] == -INF ? -INF : dp[hh[h]] + ww[ii[h]];
		for (h = 1; h < n; h++)
			dp[h] = max(dp[h], dp[h - 1]);
	}
	printf("%d\n", dp[n - 1]);
	return 0;
}

Compilation message

biochips.c: In function 'append':
biochips.c:20:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   20 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
biochips.c: In function 'main':
biochips.c:63:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   63 |  scanf("%d%d", &n, &k);
      |  ^~~~~~~~~~~~~~~~~~~~~
biochips.c:68:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   68 |   scanf("%d%d", &p, &ww[i]), p--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 7 ms 944 KB Output is correct
5 Correct 10 ms 1076 KB Output is correct
6 Correct 11 ms 1072 KB Output is correct
7 Correct 319 ms 12700 KB Output is correct
8 Correct 318 ms 12620 KB Output is correct
9 Correct 443 ms 13980 KB Output is correct
10 Correct 593 ms 15328 KB Output is correct