Submission #733464

#TimeUsernameProblemLanguageResultExecution timeMemory
733464rainboyClimbers (RMI18_climbers)C11
100 / 100
235 ms254796 KiB
#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 (stderr)

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]);
      |   ^~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...