답안 #531350

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
531350 2022-02-28T13:42:58 Z lunchbox Jakarta Skyscrapers (APIO15_skyscraper) C
36 / 100
312 ms 262148 KB
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <string.h>

#define N	30000
#define INF	0x3f3f3f3f

unsigned int X;
 
void srand__() {
	struct timeval tv;
 
	gettimeofday(&tv, NULL);
	X = tv.tv_sec ^ tv.tv_usec | 1;
}
 
int rand_() {
	return (X *= 3) >> 1;
}

int *ed[N], eo[N], *ej[N], *ew[N], eo_[N];

void append(int i, int j) {
	int o = eo[i]++;
 
	if (o >= 2 && (o & o - 1) == 0)
		ed[i] = (int *) realloc(ed[i], o * 2 * sizeof *ed[i]);
	ed[i][o] = j;
}

void append_(int i, int j, int w) {
	int o = eo_[i]++;
 
	if (o >= 2 && (o & o - 1) == 0) {
		ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
		ew[i] = (int *) realloc(ew[i], o * 2 * sizeof *ew[i]);
	}
	ej[i][o] = j, ew[i][o] = w;
}

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 = 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 find(int i, int d) {
	int lower = 0, upper = eo[i] - 1;

	while (lower < upper) {
		int t = (lower + upper) / 2;

		if (ed[i][t] < d)
			lower = t + 1;
		else if (ed[i][t] > d)
			upper = t - 1;
		else
			return 1;
	}
	return 0;
}

int dd[N], pq[1 + N], iq[N], cnt;

#define LT(u, v)    (dd[u] < dd[v])

int i2(int i) {
	return (i *= 2) > cnt ? 0 : i < cnt && LT(pq[i + 1], pq[i]) ? i + 1 : i;
}

void pq_up(int u) {
	int i, j;
	
	for (i = iq[u]; (j = i / 2) && LT(u, pq[j]); i = j)
		pq[iq[pq[j]] = i] = pq[j];
	pq[iq[u] = i] = u;
}

void pq_dn(int u) {
	int i, j;
	
	for (i = iq[u]; (j = i2(i)) && LT(pq[j], u); i = j)
		pq[iq[pq[j]] = i] = pq[j];
	pq[iq[u] = i] = u;
}

void pq_add(int u) {
	iq[u] = ++cnt;
	pq_up(u);
}

int pq_remove_first() {
	int u = pq[1], v = pq[cnt--];
	
	iq[v] = 1;
	pq_dn(v);
	return u;
}

int main() {
	int n, m, i, j, d, s, t;

	srand__();
	scanf("%d%d", &n, &m);
	for (i = 0; i < n; i++) {
		ed[i] = (int *) malloc(2 * sizeof *ed[i]), eo[i] = 0;
		ej[i] = (int *) malloc(2 * sizeof *ej[i]), ew[i] = (int *) malloc(2 * sizeof *ew[i]), eo_[i] = 0;
	}
	s = t = -1;
	for (i = 0; i < m; i++) {
		scanf("%d%d", &j, &d);
		if (i == 0)
			s = j;
		else if (i == 1)
			t = j;
		append(j, d);
	}
	for (i = 0; i < n; i++)
		sort(ed[i], 0, eo[i]);
	for (i = 0; i < n; i++) {
		int o;

		for (o = eo[i]; o--; ) {
			d = ed[i][o];

			for (j = i + d; j < n; j += d) {
				append_(i, j, (j - i) / d);
				if (find(j, d))
					break;
			}
			for (j = i - d; j >= 0; j -= d) {
				append_(i, j, (i - j) / d);
				if (find(j, d))
					break;	
			}
		}
	}
	memset(dd, 0x3f, n * sizeof *dd);
	dd[s] = 0;
	pq_add(s);
	while (cnt) {
		int o;

		i = pq_remove_first();
		for (o = eo_[i]; o--; ) {
			int j = ej[i][o], w = ew[i][o];

			d = dd[i] + w;
			if (dd[j] == INF) {
				dd[j] = d;
				pq_add(j);
			} else if (d < dd[j]) {
				dd[j] = d;
				pq_up(j);
			}
		}
	}
	printf("%d\n", dd[t] == INF ? -1 : dd[t]);
	for (i = 0; i < n; i++) {
		free(ed[i]);
		free(ej[i]);
		free(ew[i]);
	}
}

Compilation message

skyscraper.c: In function 'srand__':
skyscraper.c:15:16: warning: suggest parentheses around arithmetic in operand of '|' [-Wparentheses]
   15 |  X = tv.tv_sec ^ tv.tv_usec | 1;
      |      ~~~~~~~~~~^~~~~~~~~~~~
skyscraper.c: In function 'append':
skyscraper.c:27:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   27 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
skyscraper.c: In function 'append_':
skyscraper.c:35:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   35 |  if (o >= 2 && (o & o - 1) == 0) {
      |                     ~~^~~
skyscraper.c: In function 'main':
skyscraper.c:121:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  121 |  scanf("%d%d", &n, &m);
      |  ^~~~~~~~~~~~~~~~~~~~~
skyscraper.c:128:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  128 |   scanf("%d%d", &j, &d);
      |   ^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 0 ms 272 KB Output is correct
9 Correct 0 ms 252 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 416 KB Output is correct
12 Correct 3 ms 1976 KB Output is correct
13 Correct 3 ms 1976 KB Output is correct
14 Correct 1 ms 292 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 416 KB Output is correct
12 Correct 3 ms 1968 KB Output is correct
13 Correct 3 ms 1976 KB Output is correct
14 Correct 1 ms 328 KB Output is correct
15 Correct 1 ms 288 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 2 ms 588 KB Output is correct
18 Correct 1 ms 588 KB Output is correct
19 Correct 1 ms 588 KB Output is correct
20 Correct 48 ms 32736 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 588 KB Output is correct
23 Correct 1 ms 544 KB Output is correct
24 Correct 2 ms 716 KB Output is correct
25 Correct 2 ms 572 KB Output is correct
26 Correct 39 ms 28928 KB Output is correct
27 Correct 37 ms 27220 KB Output is correct
28 Correct 2 ms 804 KB Output is correct
29 Correct 3 ms 1188 KB Output is correct
30 Correct 2 ms 844 KB Output is correct
31 Correct 2 ms 844 KB Output is correct
32 Correct 2 ms 844 KB Output is correct
33 Correct 3 ms 1868 KB Output is correct
34 Correct 3 ms 1824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 1 ms 284 KB Output is correct
12 Correct 4 ms 1968 KB Output is correct
13 Correct 3 ms 2016 KB Output is correct
14 Correct 2 ms 300 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 552 KB Output is correct
18 Correct 1 ms 588 KB Output is correct
19 Correct 1 ms 540 KB Output is correct
20 Correct 53 ms 32704 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 588 KB Output is correct
23 Correct 1 ms 588 KB Output is correct
24 Correct 2 ms 716 KB Output is correct
25 Correct 2 ms 588 KB Output is correct
26 Correct 42 ms 28916 KB Output is correct
27 Correct 39 ms 27328 KB Output is correct
28 Correct 2 ms 824 KB Output is correct
29 Correct 2 ms 1184 KB Output is correct
30 Correct 1 ms 716 KB Output is correct
31 Correct 1 ms 928 KB Output is correct
32 Correct 2 ms 844 KB Output is correct
33 Correct 3 ms 1788 KB Output is correct
34 Correct 4 ms 1824 KB Output is correct
35 Correct 11 ms 1956 KB Output is correct
36 Correct 2 ms 588 KB Output is correct
37 Correct 18 ms 3036 KB Output is correct
38 Correct 19 ms 2940 KB Output is correct
39 Correct 19 ms 2988 KB Output is correct
40 Correct 15 ms 2916 KB Output is correct
41 Correct 14 ms 2776 KB Output is correct
42 Runtime error 312 ms 262148 KB Execution killed with signal 9
43 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 2 ms 332 KB Output is correct
12 Correct 3 ms 2016 KB Output is correct
13 Correct 3 ms 2016 KB Output is correct
14 Correct 1 ms 296 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 2 ms 552 KB Output is correct
18 Correct 1 ms 552 KB Output is correct
19 Correct 1 ms 588 KB Output is correct
20 Correct 59 ms 32756 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 588 KB Output is correct
23 Correct 1 ms 596 KB Output is correct
24 Correct 2 ms 684 KB Output is correct
25 Correct 2 ms 588 KB Output is correct
26 Correct 44 ms 28832 KB Output is correct
27 Correct 44 ms 27352 KB Output is correct
28 Correct 2 ms 844 KB Output is correct
29 Correct 2 ms 1100 KB Output is correct
30 Correct 2 ms 716 KB Output is correct
31 Correct 2 ms 844 KB Output is correct
32 Correct 2 ms 716 KB Output is correct
33 Correct 3 ms 1868 KB Output is correct
34 Correct 3 ms 1740 KB Output is correct
35 Correct 12 ms 1860 KB Output is correct
36 Correct 3 ms 716 KB Output is correct
37 Correct 13 ms 3024 KB Output is correct
38 Correct 15 ms 2972 KB Output is correct
39 Correct 14 ms 2996 KB Output is correct
40 Correct 16 ms 3016 KB Output is correct
41 Correct 15 ms 2844 KB Output is correct
42 Runtime error 305 ms 262148 KB Execution killed with signal 9
43 Halted 0 ms 0 KB -