Submission #531407

# Submission time Handle Problem Language Result Execution time Memory
531407 2022-02-28T15:43:19 Z lunchbox Jakarta Skyscrapers (APIO15_skyscraper) C
57 / 100
1000 ms 2256 KB
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <string.h>

#define N	30000
#define INF	0x3f3f3f3f
 
unsigned int X;
 
void srand_() {
	struct timeval tv;
 
	gettimeofday(&tv, NULL);
	X = tv.tv_sec ^ tv.tv_usec | 1;
}
 
int rand_() {
	return (X *= 3) >> 1;
}
 
int *ed[N], eo[N];
 
void append(int i, int j) {
	int o = eo[i]++;
 
	if (o >= 2 && (o & o - 1) == 0)
		ed[i] = (int *) realloc(ed[i], o * 2 * sizeof *ed[i]);
	ed[i][o] = j;
}
 
void sort(int *aa, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, a_ = aa[l + rand_() % (r - l)], tmp;
 
		while (j < k) {
			int c = aa[j] - a_;
 
			if (c == 0)
				j++;
			else if (c < 0) {
				tmp = aa[i], aa[i] = aa[j], aa[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = aa[j], aa[j] = aa[k], aa[k] = tmp;
			}
		}
		sort(aa, l, i);
		l = k;
	}
}
 
int find(int *aa, int n, int a) {
	int lower = 0, upper = n - 1;
 
	while (lower < upper) {
		int t = (lower + upper) / 2;
 
		if (aa[t] < a)
			lower = t + 1;
		else if (aa[t] > a)
			upper = t - 1;
		else
			return 1;
	}
	return 0;
}
 
int dd[N], pq[1 + N], iq[N], cnt;
 
int lt(int u, int v) {
	return dd[u] < dd[v];
}
 
int i2(int i) {
	return (i *= 2) > cnt ? 0 : i < cnt && lt(pq[i + 1], pq[i]) ? i + 1 : i;
}
 
void pq_up(int u) {
	int i, j;
	
	for (i = iq[u]; (j = i / 2) && lt(u, pq[j]); i = j)
		pq[iq[pq[j]] = i] = pq[j];
	pq[iq[u] = i] = u;
}
 
void pq_dn(int u) {
	int i, j;
	
	for (i = iq[u]; (j = i2(i)) && lt(pq[j], u); i = j)
		pq[iq[pq[j]] = i] = pq[j];
	pq[iq[u] = i] = u;
}
 
void pq_add(int u) {
	iq[u] = ++cnt;
	pq_up(u);
}
 
int pq_remove_first() {
	int u = pq[1], v = pq[cnt--];
	
	iq[v] = 1;
	pq_dn(v);
	return u;
}
 
int main() {
	int n, m, i, j, w, d, d_, s, t;
 
	srand_();
	scanf("%d%d", &n, &m);
	for (i = 0; i < n; i++)
		ed[i] = (int *) malloc(2 * sizeof *ed[i]), eo[i] = 0;
	s = t = -1;
	for (i = 0; i < m; i++) {
		scanf("%d%d", &j, &d);
		if (i == 0)
			s = j;
		else if (i == 1)
			t = j;
		append(j, d);
	}
	for (i = 0; i < n; i++) {
		int o = 0;

		sort(ed[i], 0, eo[i]);
		for (j = 0; j < eo[i]; j++)
			if (j == 0 || ed[i][j] != ed[i][j - 1])
				ed[i][o++] = ed[i][j];
		eo[i] = o;
	}
	memset(dd, 0x3f, n * sizeof *dd);
	dd[s] = 0;
	pq_add(s);
	while (cnt) {
		int o;
 
		i = pq_remove_first();
		for (o = eo[i]; o--; ) {
			d = ed[i][o];
			for (w = 0, j = i + d; j < n; j += d) {
				w++;
				d_ = dd[i] + w;
				if (dd[j] == INF) {
					dd[j] = d_;
					pq_add(j);
				} else if (d_ < dd[j]) {
					dd[j] = d_;
					pq_up(j);
				}
				if (find(ed[j], eo[j], d))
					break;
			}
			for (w = 0, j = i - d; j >= 0; j -= d) {
				w++;
				d_ = dd[i] + w;
				if (dd[j] == INF) {
					dd[j] = d_;
					pq_add(j);
				} else if (d_ < dd[j]) {
					dd[j] = d_;
					pq_up(j);
				}
				if (find(ed[j], eo[j], d))
					break;	
			}
		}
	}
	printf("%d\n", dd[t] == INF ? -1 : dd[t]);
}

Compilation message

skyscraper.c: In function 'srand_':
skyscraper.c:15:16: warning: suggest parentheses around arithmetic in operand of '|' [-Wparentheses]
   15 |  X = tv.tv_sec ^ tv.tv_usec | 1;
      |      ~~~~~~~~~~^~~~~~~~~~~~
skyscraper.c: In function 'append':
skyscraper.c:27:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   27 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
skyscraper.c: In function 'main':
skyscraper.c:113:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  113 |  scanf("%d%d", &n, &m);
      |  ^~~~~~~~~~~~~~~~~~~~~
skyscraper.c:118:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  118 |   scanf("%d%d", &j, &d);
      |   ^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 284 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 288 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 284 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 17 ms 424 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
25 Correct 2 ms 332 KB Output is correct
26 Correct 2 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 2 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 2 ms 332 KB Output is correct
34 Correct 2 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 284 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 312 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 448 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 14 ms 332 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 2 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 424 KB Output is correct
29 Correct 1 ms 416 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 2 ms 332 KB Output is correct
34 Correct 2 ms 420 KB Output is correct
35 Correct 9 ms 716 KB Output is correct
36 Correct 2 ms 332 KB Output is correct
37 Correct 11 ms 684 KB Output is correct
38 Correct 12 ms 844 KB Output is correct
39 Correct 12 ms 860 KB Output is correct
40 Correct 12 ms 872 KB Output is correct
41 Correct 11 ms 836 KB Output is correct
42 Correct 6 ms 716 KB Output is correct
43 Correct 6 ms 716 KB Output is correct
44 Correct 19 ms 764 KB Output is correct
45 Correct 10 ms 812 KB Output is correct
46 Correct 10 ms 816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 1 ms 280 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 14 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 1 ms 332 KB Output is correct
25 Correct 1 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 1 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Correct 1 ms 332 KB Output is correct
31 Correct 1 ms 332 KB Output is correct
32 Correct 1 ms 332 KB Output is correct
33 Correct 2 ms 332 KB Output is correct
34 Correct 2 ms 332 KB Output is correct
35 Correct 9 ms 776 KB Output is correct
36 Correct 2 ms 332 KB Output is correct
37 Correct 9 ms 680 KB Output is correct
38 Correct 11 ms 864 KB Output is correct
39 Correct 12 ms 788 KB Output is correct
40 Correct 14 ms 828 KB Output is correct
41 Correct 11 ms 780 KB Output is correct
42 Correct 6 ms 716 KB Output is correct
43 Correct 5 ms 716 KB Output is correct
44 Correct 19 ms 684 KB Output is correct
45 Correct 12 ms 716 KB Output is correct
46 Correct 10 ms 820 KB Output is correct
47 Correct 20 ms 1184 KB Output is correct
48 Correct 7 ms 1576 KB Output is correct
49 Correct 6 ms 1704 KB Output is correct
50 Correct 5 ms 1704 KB Output is correct
51 Correct 19 ms 2252 KB Output is correct
52 Correct 18 ms 2124 KB Output is correct
53 Correct 16 ms 2256 KB Output is correct
54 Correct 4 ms 1868 KB Output is correct
55 Correct 4 ms 1868 KB Output is correct
56 Execution timed out 1077 ms 2124 KB Time limit exceeded
57 Halted 0 ms 0 KB -