Submission #531355

# Submission time Handle Problem Language Result Execution time Memory
531355 2022-02-28T13:50:36 Z lunchbox Jakarta Skyscrapers (APIO15_skyscraper) C++17
36 / 100
338 ms 262148 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], *ej[N], *ew[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 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;
}

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 i, int d) {
	int lower = 0, upper = eo[i] - 1;

	while (lower < upper) {
		int t = (lower + upper) / 2;

		if (ed[i][t] < d)
			lower = t + 1;
		else if (ed[i][t] > d)
			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, 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;
		ej[i] = (int *) malloc(2 * sizeof *ej[i]), ew[i] = (int *) malloc(2 * sizeof *ew[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++)
		sort(ed[i], 0, eo[i]);
	for (i = 0; i < n; i++) {
		int o;

		for (o = eo[i]; o--; ) {
			d = ed[i][o];

			for (j = i + d; j < n; j += d) {
				append_(i, j, (j - i) / d);
				if (find(j, d))
					break;
			}
			for (j = i - d; j >= 0; j -= d) {
				append_(i, j, (i - j) / d);
				if (find(j, d))
					break;	
			}
		}
	}
	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--; ) {
			int j = ej[i][o], w = ew[i][o];

			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);
			}
		}
	}
	printf("%d\n", dd[t] == INF ? -1 : dd[t]);
	for (i = 0; i < n; i++) {
		free(ed[i]);
		free(ej[i]);
		free(ew[i]);
	}
}

Compilation message

skyscraper.cpp: In function 'void srand_()':
skyscraper.cpp:15:16: warning: suggest parentheses around arithmetic in operand of '|' [-Wparentheses]
   15 |  X = tv.tv_sec ^ tv.tv_usec | 1;
      |      ~~~~~~~~~~^~~~~~~~~~~~
skyscraper.cpp: In function 'void append(int, int)':
skyscraper.cpp:27:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   27 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
skyscraper.cpp: In function 'void append_(int, int, int)':
skyscraper.cpp:35:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   35 |  if (o >= 2 && (o & o - 1) == 0) {
      |                     ~~^~~
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:123:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  123 |  scanf("%d%d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~
skyscraper.cpp:130:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  130 |   scanf("%d%d", &j, &d);
      |   ~~~~~^~~~~~~~~~~~~~~~
# 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
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 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 1 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 0 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 3 ms 1888 KB Output is correct
13 Correct 3 ms 2016 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 1 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 0 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 4 ms 1888 KB Output is correct
13 Correct 3 ms 2016 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 588 KB Output is correct
18 Correct 1 ms 588 KB Output is correct
19 Correct 1 ms 588 KB Output is correct
20 Correct 49 ms 32592 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 588 KB Output is correct
23 Correct 2 ms 588 KB Output is correct
24 Correct 2 ms 588 KB Output is correct
25 Correct 2 ms 588 KB Output is correct
26 Correct 40 ms 28848 KB Output is correct
27 Correct 38 ms 27264 KB Output is correct
28 Correct 2 ms 716 KB Output is correct
29 Correct 2 ms 1100 KB Output is correct
30 Correct 1 ms 716 KB Output is correct
31 Correct 2 ms 844 KB Output is correct
32 Correct 2 ms 716 KB Output is correct
33 Correct 3 ms 1740 KB Output is correct
34 Correct 4 ms 1740 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 1 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 0 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 3 ms 1888 KB Output is correct
13 Correct 5 ms 2016 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 2 ms 588 KB Output is correct
18 Correct 1 ms 588 KB Output is correct
19 Correct 1 ms 588 KB Output is correct
20 Correct 50 ms 32620 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 588 KB Output is correct
23 Correct 1 ms 588 KB Output is correct
24 Correct 2 ms 588 KB Output is correct
25 Correct 2 ms 588 KB Output is correct
26 Correct 42 ms 28888 KB Output is correct
27 Correct 38 ms 27268 KB Output is correct
28 Correct 2 ms 716 KB Output is correct
29 Correct 2 ms 1100 KB Output is correct
30 Correct 1 ms 716 KB Output is correct
31 Correct 2 ms 844 KB Output is correct
32 Correct 2 ms 844 KB Output is correct
33 Correct 3 ms 1740 KB Output is correct
34 Correct 5 ms 1840 KB Output is correct
35 Correct 12 ms 1652 KB Output is correct
36 Correct 2 ms 588 KB Output is correct
37 Correct 14 ms 2872 KB Output is correct
38 Correct 16 ms 2676 KB Output is correct
39 Correct 14 ms 2636 KB Output is correct
40 Correct 14 ms 2716 KB Output is correct
41 Correct 18 ms 2640 KB Output is correct
42 Runtime error 314 ms 262148 KB Execution killed with signal 9
43 Halted 0 ms 0 KB -
# 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 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 320 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 0 ms 332 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 3 ms 2000 KB Output is correct
13 Correct 3 ms 2016 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 2 ms 588 KB Output is correct
18 Correct 1 ms 588 KB Output is correct
19 Correct 1 ms 588 KB Output is correct
20 Correct 51 ms 32688 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 588 KB Output is correct
23 Correct 1 ms 588 KB Output is correct
24 Correct 2 ms 588 KB Output is correct
25 Correct 1 ms 588 KB Output is correct
26 Correct 43 ms 28936 KB Output is correct
27 Correct 40 ms 27284 KB Output is correct
28 Correct 2 ms 716 KB Output is correct
29 Correct 3 ms 1100 KB Output is correct
30 Correct 1 ms 716 KB Output is correct
31 Correct 2 ms 844 KB Output is correct
32 Correct 2 ms 716 KB Output is correct
33 Correct 3 ms 1740 KB Output is correct
34 Correct 4 ms 1740 KB Output is correct
35 Correct 11 ms 1612 KB Output is correct
36 Correct 2 ms 588 KB Output is correct
37 Correct 13 ms 2908 KB Output is correct
38 Correct 14 ms 2644 KB Output is correct
39 Correct 15 ms 2680 KB Output is correct
40 Correct 15 ms 2764 KB Output is correct
41 Correct 15 ms 2636 KB Output is correct
42 Runtime error 338 ms 262148 KB Execution killed with signal 9
43 Halted 0 ms 0 KB -