#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
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]);
| ^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1236 KB |
Output is correct |
2 |
Correct |
8 ms |
8820 KB |
Output is correct |
3 |
Correct |
78 ms |
94328 KB |
Output is correct |
4 |
Runtime error |
429 ms |
524288 KB |
Execution killed with signal 9 |
5 |
Runtime error |
367 ms |
524288 KB |
Execution killed with signal 9 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
4 ms |
2516 KB |
Output is correct |
4 |
Correct |
2 ms |
1236 KB |
Output is correct |
5 |
Correct |
19 ms |
15728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1448 KB |
Output is correct |
2 |
Correct |
14 ms |
9944 KB |
Output is correct |
3 |
Correct |
302 ms |
118824 KB |
Output is correct |
4 |
Runtime error |
373 ms |
524288 KB |
Execution killed with signal 9 |
5 |
Runtime error |
368 ms |
524288 KB |
Execution killed with signal 9 |
6 |
Runtime error |
372 ms |
524288 KB |
Execution killed with signal 9 |
7 |
Runtime error |
361 ms |
524288 KB |
Execution killed with signal 9 |
8 |
Runtime error |
366 ms |
524288 KB |
Execution killed with signal 9 |
9 |
Runtime error |
387 ms |
524288 KB |
Execution killed with signal 9 |
10 |
Runtime error |
426 ms |
524288 KB |
Execution killed with signal 9 |