Submission #733456

#TimeUsernameProblemLanguageResultExecution timeMemory
733456rainboyClimbers (RMI18_climbers)C11
55 / 100
429 ms524288 KiB
#include <stdio.h> #include <stdlib.h> #include <string.h> #define N 5000 #define N_ (N * N) #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; } int *ej[N_], *ew[N_], eo[N_]; 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; } long long dd[N_]; int iq[N_ + 1], pq[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, n_, i, i_, j, j_, o; scanf("%d", &n), n_ = n * n; for (i = 0; i < n; i++) scanf("%d", &aa[i]); for (i = 0; i < n_; i++) { ej[i] = (int *) malloc(2 * sizeof *ej[i]); ew[i] = (int *) malloc(2 * sizeof *ew[i]); } for (i = 0; i < n; i++) for (j = 0; j + 1 < n; j++) if (contains(aa[j], aa[j + 1], aa[i])) { if (i > 0) { if (contains(aa[j], aa[j + 1], aa[i - 1])) append(i * n + j, (i - 1) * n + j, abs_(aa[i - 1] - aa[i])); if (contains(aa[i], aa[i - 1], aa[j])) append(i * n + j, j * n + (i - 1), abs_(aa[j] - aa[i])); if (contains(aa[i], aa[i - 1], aa[j + 1])) append(i * n + j, (j + 1) * n + (i - 1), abs_(aa[j + 1] - aa[i])); } if (i + 1 < n) { if (contains(aa[j], aa[j + 1], aa[i + 1])) append(i * n + j, (i + 1) * n + j, abs_(aa[i + 1] - aa[i])); if (contains(aa[i], aa[i + 1], aa[j])) append(i * n + j, j * n + i, abs_(aa[j] - aa[i])); if (contains(aa[i], aa[i + 1], aa[j + 1])) append(i * n + j, (j + 1) * n + i, abs_(aa[j + 1] - aa[i])); } } memset(dd, 0x3f, n_ * sizeof *dd); dd[0 * n + (n - 2)] = 0, pq_add_last(0 * n + (n - 2)); while (cnt) { i_ = pq_remove_first(), i = i_ / n, j = i_ % n; if (i == j || i == j + 1) { printf("%lld\n", dd[i_]); return 0; } for (o = eo[i_]; o--; ) { int w; long long d; j_ = ej[i_][o], w = ew[i_][o], d = dd[i_] + w; if (dd[j_] > d) { if (dd[j_] == INF) pq_add_last(j_); dd[j_] = d, pq_up(j_); } } } printf("NO\n"); return 0; }

Compilation message (stderr)

climbers.c: In function 'append':
climbers.c:24:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   24 |  if (o >= 2 && (o & o - 1) == 0) {
      |                     ~~^~~
climbers.c: In function 'main':
climbers.c:72:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   72 |  scanf("%d", &n), n_ = n * n;
      |  ^~~~~~~~~~~~~~~
climbers.c:74:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |   scanf("%d", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...