답안 #733464

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
733464 2023-04-30T20:34:00 Z rainboy Climbers (RMI18_climbers) C
100 / 100
235 ms 254796 KB
#include <stdio.h>
#include <string.h>

#define N	5000
#define INF	0x3f3f3f3f3f3f3f3fLL

int abs_(int a) { return a > 0 ? a : -a; }

int contains(int a, int b, int c) {
	int tmp;

	if (a > b)
		tmp = a, a = b, b = tmp;
	return a <= c && c <= b;
}

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

int lt(int i, int j) { return dd[i] < dd[j]; }

int p2(int p) {
	return (p *= 2) > cnt ? 0 : (p < cnt && lt(iq[p + 1], iq[p]) ? p + 1 : p);
}

void pq_up(int i) {
	int p, q, j;

	for (p = pq[i]; (q = p / 2) && lt(i, j = iq[q]); p = q)
		iq[pq[j] = p] = j;
	iq[pq[i] = p] = i;
}

void pq_dn(int i) {
	int p, q, j;

	for (p = pq[i]; (q = p2(p)) && lt(j = iq[q], i); p = q)
		iq[pq[j] = p] = j;
	iq[pq[i] = p] = i;
}

void pq_add_last(int i) {
	iq[pq[i] = ++cnt] = i;
}

int pq_remove_first() {
	int i = iq[1], j = iq[cnt--];

	if (j != i)
		pq[j] = 1, pq_dn(j);
	pq[i] = 0;
	return i;
}

int main() {
	static int aa[N];
	int n, i, j, u, v;
	long long d;

	scanf("%d", &n);
	for (i = 0; i < n; i++)
		scanf("%d", &aa[i]);
	memset(dd, 0x3f, n * n * sizeof *dd);
	dd[0 * n + (n - 2)] = 0, pq_add_last(0 * n + (n - 2));
	while (cnt) {
		u = pq_remove_first(), i = u / n, j = u % n;
		if (i == j || i == j + 1) {
			printf("%lld\n", dd[u]);
			return 0;
		}
		if (i > 0) {
			if (contains(aa[j], aa[j + 1], aa[i - 1])) {
				v = (i - 1) * n + j, d = dd[u] + abs_(aa[i - 1] - aa[i]);
				if (dd[v] > d) {
					if (dd[v] == INF)
						pq_add_last(v);
					dd[v] = d, pq_up(v);
				}
			}
			if (contains(aa[i], aa[i - 1], aa[j])) {
				v = j * n + (i - 1), d = dd[u] + abs_(aa[j] - aa[i]);
				if (dd[v] > d) {
					if (dd[v] == INF)
						pq_add_last(v);
					dd[v] = d, pq_up(v);
				}
			}
			if (contains(aa[i], aa[i - 1], aa[j + 1])) {
				v = (j + 1) * n + (i - 1), d = dd[u] + abs_(aa[j + 1] - aa[i]);
				if (dd[v] > d) {
					if (dd[v] == INF)
						pq_add_last(v);
					dd[v] = d, pq_up(v);
				}
			}
		}
		if (i + 1 < n) {
			if (contains(aa[j], aa[j + 1], aa[i + 1])) {
				v = (i + 1) * n + j, d = dd[u] + abs_(aa[i + 1] - aa[i]);
				if (dd[v] > d) {
					if (dd[v] == INF)
						pq_add_last(v);
					dd[v] = d, pq_up(v);
				}
			}
			if (contains(aa[i], aa[i + 1], aa[j])) {
				v = j * n + i, d = dd[u] + abs_(aa[j] - aa[i]);
				if (dd[v] > d) {
					if (dd[v] == INF)
						pq_add_last(v);
					dd[v] = d, pq_up(v);
				}
			}
			if (contains(aa[i], aa[i + 1], aa[j + 1])) {
				v = (j + 1) * n + i, d = dd[u] + abs_(aa[j + 1] - aa[i]);
				if (dd[v] > d) {
					if (dd[v] == INF)
						pq_add_last(v);
					dd[v] = d, pq_up(v);
				}
			}
		}
	}
	printf("NO\n");
	return 0;
}

Compilation message

climbers.c: In function 'main':
climbers.c:59:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   59 |  scanf("%d", &n);
      |  ^~~~~~~~~~~~~~~
climbers.c:61:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 1364 KB Output is correct
3 Correct 7 ms 11988 KB Output is correct
4 Correct 41 ms 92232 KB Output is correct
5 Correct 109 ms 254796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 468 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 2132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 1364 KB Output is correct
3 Correct 70 ms 12032 KB Output is correct
4 Correct 137 ms 89620 KB Output is correct
5 Correct 165 ms 92328 KB Output is correct
6 Correct 172 ms 156012 KB Output is correct
7 Correct 190 ms 151608 KB Output is correct
8 Correct 188 ms 251852 KB Output is correct
9 Correct 214 ms 240624 KB Output is correct
10 Correct 235 ms 240708 KB Output is correct