#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 30000
#define M 30000
#define M_ 11111111
int min(int a, int b) { return a < b ? a : b; }
void append(int **eh, int *eo, int i, int h) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
eh[i] = (int *) realloc(eh[i], o * 2 * sizeof *eh[i]);
eh[i][o] = h;
}
int main() {
static int *eh1[N + 1], eo1[N + 1], *eh2[N + 1], eo2[N + 1];
static int ii[M_], hh[N], nxt[M_ * 2], dd[M_], qu[M + M_];
int n, m, m_, h, i, j, o, head, cnt;
scanf("%d%d", &n, &m);
for (i = 0; i <= n; i++) {
eh1[i] = (int *) malloc(2 * sizeof *eh1[i]);
eh2[i] = (int *) malloc(2 * sizeof *eh2[i]);
}
for (h = 0; h < m; h++) {
scanf("%d%d", &i, &j), j = min(j, n);
ii[h] = i;
append(eh1, eo1, i, h);
append(eh2, eo2, j, h);
}
if (ii[0] == ii[1]) {
printf("0\n");
return 0;
}
m_ = m;
memset(hh, -1, n * sizeof *hh);
memset(nxt, -1, sizeof nxt);
for (j = 1; j < n; j++) {
for (o = eo2[j]; o--; ) {
h = eh2[j][o];
hh[ii[h]] = h;
}
for (o = eo2[j]; o--; ) {
int h_, h1;
h = eh2[j][o];
if (hh[ii[h]] == -1)
continue;
h_ = -1;
for (i = ii[h] % j; i < n; i += j) {
if (hh[i] == -1)
ii[h1 = m_++] = i;
else
h1 = hh[i], hh[i] = -1;
if (h_ != -1)
nxt[h_ << 1 | 1] = h1, nxt[h1 << 1 | 0] = h_;
h_ = h1;
}
}
}
for (h = 0; h < m_; h++)
dd[h] = m_;
head = m, cnt = 0;
dd[0] = 0, qu[head + cnt++] = 0;
while (cnt) {
int d, o, h_;
h = qu[cnt--, head++], i = ii[h], d = dd[h];
if (h == 1) {
printf("%d\n", d);
return 0;
}
if (eo1[i]) {
for (o = eo1[i]; o--; ) {
h_ = eh1[i][o];
if (dd[h_] > d)
dd[h_] = d, qu[cnt++, --head] = h_;
}
eo1[i] = 0;
}
d++;
h_ = nxt[h << 1 | 0];
if (h_ != -1 && dd[h_] > d)
dd[h_] = d, qu[head + cnt++] = h_;
h_ = nxt[h << 1 | 1];
if (h_ != -1 && dd[h_] > d)
dd[h_] = d, qu[head + cnt++] = h_;
}
printf("-1\n");
return 0;
}
Compilation message
skyscraper.c: In function 'append':
skyscraper.c:14:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
14 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
skyscraper.c: In function 'main':
skyscraper.c:24:2: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
24 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
skyscraper.c:30:3: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
30 | scanf("%d%d", &i, &j), j = min(j, n);
| ^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
87276 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
46 ms |
87276 KB |
Output is correct |
5 |
Correct |
46 ms |
87276 KB |
Output is correct |
6 |
Correct |
50 ms |
87276 KB |
Output is correct |
7 |
Correct |
46 ms |
87276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
87276 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
47 ms |
87276 KB |
Output is correct |
5 |
Correct |
47 ms |
87276 KB |
Output is correct |
6 |
Correct |
51 ms |
87276 KB |
Output is correct |
7 |
Correct |
48 ms |
87276 KB |
Output is correct |
8 |
Correct |
46 ms |
87276 KB |
Output is correct |
9 |
Correct |
46 ms |
87276 KB |
Output is correct |
10 |
Correct |
46 ms |
87404 KB |
Output is correct |
11 |
Correct |
46 ms |
87404 KB |
Output is correct |
12 |
Correct |
47 ms |
87404 KB |
Output is correct |
13 |
Correct |
46 ms |
87404 KB |
Output is correct |
14 |
Correct |
46 ms |
87404 KB |
Output is correct |
15 |
Correct |
47 ms |
87404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
87276 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
47 ms |
87276 KB |
Output is correct |
5 |
Correct |
46 ms |
87276 KB |
Output is correct |
6 |
Correct |
45 ms |
87276 KB |
Output is correct |
7 |
Correct |
47 ms |
87276 KB |
Output is correct |
8 |
Correct |
45 ms |
87276 KB |
Output is correct |
9 |
Correct |
46 ms |
87276 KB |
Output is correct |
10 |
Correct |
46 ms |
87404 KB |
Output is correct |
11 |
Correct |
46 ms |
87404 KB |
Output is correct |
12 |
Correct |
46 ms |
87404 KB |
Output is correct |
13 |
Correct |
46 ms |
87404 KB |
Output is correct |
14 |
Correct |
46 ms |
87404 KB |
Output is correct |
15 |
Correct |
46 ms |
87404 KB |
Output is correct |
16 |
Correct |
46 ms |
87424 KB |
Output is correct |
17 |
Correct |
46 ms |
87532 KB |
Output is correct |
18 |
Correct |
46 ms |
87660 KB |
Output is correct |
19 |
Correct |
46 ms |
87532 KB |
Output is correct |
20 |
Correct |
47 ms |
87532 KB |
Output is correct |
21 |
Correct |
46 ms |
87404 KB |
Output is correct |
22 |
Correct |
46 ms |
87532 KB |
Output is correct |
23 |
Correct |
47 ms |
87532 KB |
Output is correct |
24 |
Correct |
47 ms |
87660 KB |
Output is correct |
25 |
Correct |
47 ms |
87552 KB |
Output is correct |
26 |
Correct |
46 ms |
87660 KB |
Output is correct |
27 |
Correct |
47 ms |
87660 KB |
Output is correct |
28 |
Correct |
51 ms |
87788 KB |
Output is correct |
29 |
Correct |
48 ms |
88172 KB |
Output is correct |
30 |
Correct |
47 ms |
87788 KB |
Output is correct |
31 |
Correct |
48 ms |
87928 KB |
Output is correct |
32 |
Correct |
47 ms |
87788 KB |
Output is correct |
33 |
Correct |
49 ms |
88940 KB |
Output is correct |
34 |
Correct |
48 ms |
88684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
87276 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
46 ms |
87412 KB |
Output is correct |
5 |
Correct |
45 ms |
87276 KB |
Output is correct |
6 |
Correct |
47 ms |
87276 KB |
Output is correct |
7 |
Correct |
47 ms |
87532 KB |
Output is correct |
8 |
Correct |
51 ms |
87276 KB |
Output is correct |
9 |
Correct |
52 ms |
87276 KB |
Output is correct |
10 |
Correct |
46 ms |
87552 KB |
Output is correct |
11 |
Correct |
48 ms |
87404 KB |
Output is correct |
12 |
Correct |
47 ms |
87404 KB |
Output is correct |
13 |
Correct |
46 ms |
87404 KB |
Output is correct |
14 |
Correct |
48 ms |
87440 KB |
Output is correct |
15 |
Correct |
46 ms |
87404 KB |
Output is correct |
16 |
Correct |
46 ms |
87404 KB |
Output is correct |
17 |
Correct |
46 ms |
87532 KB |
Output is correct |
18 |
Correct |
46 ms |
87532 KB |
Output is correct |
19 |
Correct |
46 ms |
87532 KB |
Output is correct |
20 |
Correct |
46 ms |
87532 KB |
Output is correct |
21 |
Correct |
48 ms |
87404 KB |
Output is correct |
22 |
Correct |
46 ms |
87532 KB |
Output is correct |
23 |
Correct |
47 ms |
87532 KB |
Output is correct |
24 |
Correct |
47 ms |
87660 KB |
Output is correct |
25 |
Correct |
47 ms |
87660 KB |
Output is correct |
26 |
Correct |
47 ms |
87624 KB |
Output is correct |
27 |
Correct |
48 ms |
87660 KB |
Output is correct |
28 |
Correct |
47 ms |
87788 KB |
Output is correct |
29 |
Correct |
47 ms |
88172 KB |
Output is correct |
30 |
Correct |
47 ms |
87660 KB |
Output is correct |
31 |
Correct |
48 ms |
88044 KB |
Output is correct |
32 |
Correct |
57 ms |
87788 KB |
Output is correct |
33 |
Correct |
49 ms |
88940 KB |
Output is correct |
34 |
Correct |
49 ms |
88684 KB |
Output is correct |
35 |
Correct |
55 ms |
88940 KB |
Output is correct |
36 |
Correct |
48 ms |
87660 KB |
Output is correct |
37 |
Correct |
54 ms |
89324 KB |
Output is correct |
38 |
Correct |
60 ms |
89580 KB |
Output is correct |
39 |
Correct |
57 ms |
89452 KB |
Output is correct |
40 |
Correct |
57 ms |
89452 KB |
Output is correct |
41 |
Correct |
56 ms |
89452 KB |
Output is correct |
42 |
Correct |
53 ms |
88300 KB |
Output is correct |
43 |
Correct |
54 ms |
88556 KB |
Output is correct |
44 |
Correct |
53 ms |
88428 KB |
Output is correct |
45 |
Correct |
84 ms |
93676 KB |
Output is correct |
46 |
Correct |
70 ms |
92780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
87276 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
46 ms |
87276 KB |
Output is correct |
5 |
Correct |
46 ms |
87276 KB |
Output is correct |
6 |
Correct |
45 ms |
87276 KB |
Output is correct |
7 |
Correct |
46 ms |
87276 KB |
Output is correct |
8 |
Correct |
48 ms |
87276 KB |
Output is correct |
9 |
Correct |
46 ms |
87276 KB |
Output is correct |
10 |
Correct |
46 ms |
87404 KB |
Output is correct |
11 |
Correct |
48 ms |
87404 KB |
Output is correct |
12 |
Correct |
46 ms |
87404 KB |
Output is correct |
13 |
Correct |
46 ms |
87404 KB |
Output is correct |
14 |
Correct |
46 ms |
87404 KB |
Output is correct |
15 |
Correct |
47 ms |
87404 KB |
Output is correct |
16 |
Correct |
49 ms |
87552 KB |
Output is correct |
17 |
Correct |
46 ms |
87532 KB |
Output is correct |
18 |
Correct |
46 ms |
87492 KB |
Output is correct |
19 |
Correct |
47 ms |
87532 KB |
Output is correct |
20 |
Correct |
46 ms |
87532 KB |
Output is correct |
21 |
Correct |
46 ms |
87404 KB |
Output is correct |
22 |
Correct |
48 ms |
87532 KB |
Output is correct |
23 |
Correct |
49 ms |
87660 KB |
Output is correct |
24 |
Correct |
47 ms |
87660 KB |
Output is correct |
25 |
Correct |
48 ms |
87532 KB |
Output is correct |
26 |
Correct |
46 ms |
87660 KB |
Output is correct |
27 |
Correct |
47 ms |
87660 KB |
Output is correct |
28 |
Correct |
48 ms |
87788 KB |
Output is correct |
29 |
Correct |
47 ms |
88316 KB |
Output is correct |
30 |
Correct |
46 ms |
87660 KB |
Output is correct |
31 |
Correct |
47 ms |
87916 KB |
Output is correct |
32 |
Correct |
46 ms |
87788 KB |
Output is correct |
33 |
Correct |
50 ms |
88940 KB |
Output is correct |
34 |
Correct |
48 ms |
88684 KB |
Output is correct |
35 |
Correct |
56 ms |
88940 KB |
Output is correct |
36 |
Correct |
48 ms |
87660 KB |
Output is correct |
37 |
Correct |
54 ms |
89324 KB |
Output is correct |
38 |
Correct |
57 ms |
89580 KB |
Output is correct |
39 |
Correct |
58 ms |
89452 KB |
Output is correct |
40 |
Correct |
56 ms |
89452 KB |
Output is correct |
41 |
Correct |
57 ms |
89452 KB |
Output is correct |
42 |
Correct |
54 ms |
88300 KB |
Output is correct |
43 |
Correct |
53 ms |
88428 KB |
Output is correct |
44 |
Correct |
53 ms |
88428 KB |
Output is correct |
45 |
Correct |
81 ms |
93676 KB |
Output is correct |
46 |
Correct |
77 ms |
93036 KB |
Output is correct |
47 |
Correct |
60 ms |
94700 KB |
Output is correct |
48 |
Correct |
58 ms |
91500 KB |
Output is correct |
49 |
Correct |
56 ms |
91116 KB |
Output is correct |
50 |
Correct |
55 ms |
91116 KB |
Output is correct |
51 |
Correct |
63 ms |
93036 KB |
Output is correct |
52 |
Correct |
67 ms |
93164 KB |
Output is correct |
53 |
Correct |
61 ms |
91116 KB |
Output is correct |
54 |
Correct |
48 ms |
90092 KB |
Output is correct |
55 |
Correct |
48 ms |
90348 KB |
Output is correct |
56 |
Correct |
57 ms |
90604 KB |
Output is correct |
57 |
Correct |
54 ms |
93292 KB |
Output is correct |
58 |
Correct |
55 ms |
91244 KB |
Output is correct |
59 |
Correct |
57 ms |
91628 KB |
Output is correct |
60 |
Correct |
55 ms |
91884 KB |
Output is correct |
61 |
Correct |
56 ms |
91884 KB |
Output is correct |
62 |
Correct |
68 ms |
95852 KB |
Output is correct |
63 |
Correct |
235 ms |
117100 KB |
Output is correct |
64 |
Correct |
270 ms |
123116 KB |
Output is correct |
65 |
Correct |
544 ms |
137580 KB |
Output is correct |
66 |
Correct |
953 ms |
175084 KB |
Output is correct |
67 |
Correct |
678 ms |
161260 KB |
Output is correct |