Submission #831491

# Submission time Handle Problem Language Result Execution time Memory
831491 2023-08-20T09:47:41 Z rainboy Watermelon (INOI20_watermelon) C
100 / 100
53 ms 29728 KB
#include <stdio.h>
#include <stdlib.h>

#define N	100000
#define K	10
#define N_	(N * K * 2 + 1)

unsigned int Z = 12345;

int rand_() {
	return (Z *= 3) >> 1;
}

int n, k;

int *ej[N], eo[N], dd[N];

void append(int i, int j) {
	int o = eo[i]++;

	if (o >= 2 && (o & o - 1) == 0)
		ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
	ej[i][o] = j, dd[j]++;
}

int zz[N_], ll[N_], rr[N_], ii[N_], u_, l_, r_;

int node(int i) {
	static int _ = 1;

	zz[_] = rand_();
	ii[_] = i;
	return _++;
}

void split(int u, int i) {
	if (u == 0) {
		u_ = l_ = r_ = 0;
		return;
	}
	if (ii[u] < i) {
		split(rr[u], i);
		rr[u] = l_, l_ = u;
	} else if (ii[u] > i) {
		split(ll[u], i);
		ll[u] = r_, r_ = u;
	} else {
		u_ = u, l_ = ll[u], r_ = rr[u];
		ll[u] = rr[u] = 0;
	}
}

int merge(int u, int v) {
	if (u == 0)
		return v;
	if (v == 0)
		return u;
	if (zz[u] < zz[v]) {
		rr[u] = merge(rr[u], v);
		return u;
	} else {
		ll[v] = merge(u, ll[v]);
		return v;
	}
}

void tr_add(int i) {
	split(u_, i);
	u_ = merge(merge(l_, node(i)), r_);
}

void tr_remove(int i) {
	split(u_, i);
	u_ = merge(l_, r_);
}

int tr_higher(int i) {
	int u = u_, i_ = -1;

	while (u)
		if (ii[u] > i)
			i_ = ii[u], u = ll[u];
		else
			u = rr[u];
	return i_;
}

int aa[N];

int branch(int a) {
	int i, o;

	if (a == n) {
		if (--k == 0) {
			for (i = 0; i < n; i++)
				printf("%d ", aa[i] + 1);
			printf("\n");
			return 1;
		}
		return 0;
	}
	if (u_ == 0)
		return 0;
	i = -1;
	while ((i = tr_higher(i)) != -1) {
		tr_remove(i);
		aa[i] = a;
		for (o = eo[i]; o--; ) {
			int j = ej[i][o];

			if (--dd[j] == 0)
				tr_add(j);
		}
		if (branch(a + 1))
			return 1;
		for (o = eo[i]; o--; ) {
			int j = ej[i][o];

			if (dd[j]++ == 0)
				tr_remove(j);
		}
		tr_add(i);
	}
	return 0;
}

int main() {
	static int tt[N], qu[N];
	int cnt, h, i;

	scanf("%d%d", &n, &k);
	for (i = 0; i < n; i++)
		scanf("%d", &tt[i]);
	if (tt[n - 1] != -1) {
		printf("-1\n");
		return 0;
	}
	for (i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
	cnt = 0;
	for (i = n - 1; i >= 0; i--)
		if (tt[i] == -1) {
			while (cnt)
				append(qu[--cnt], i);
			qu[cnt++] = i;
		} else {
			h = cnt;
			while (h >= 2 && tt[qu[h - 1]] < tt[i])
				append(qu[--h], i);
			if ((h == cnt ? 0 : tt[qu[h]]) != tt[i] - 1) {
				printf("-1\n");
				return 0;
			}
			append(i, qu[h - 1]);
			if (h >= 2 && tt[qu[h - 1]] == tt[i])
				h--;
			qu[h++] = i;
			cnt = h;
		}
	for (i = 0; i < n; i++)
		if (dd[i] == 0)
			tr_add(i);
	if (!branch(0))
		printf("-1\n");
	return 0;
}

Compilation message

Main.c: In function 'append':
Main.c:21:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   21 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
Main.c: In function 'main':
Main.c:131:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  131 |  scanf("%d%d", &n, &k);
      |  ^~~~~~~~~~~~~~~~~~~~~
Main.c:133:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  133 |   scanf("%d", &tt[i]);
      |   ^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 284 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 0 ms 340 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 284 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 0 ms 340 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 1 ms 596 KB Output is correct
35 Correct 1 ms 596 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 1 ms 596 KB Output is correct
39 Correct 1 ms 596 KB Output is correct
40 Correct 1 ms 596 KB Output is correct
41 Correct 1 ms 596 KB Output is correct
42 Correct 1 ms 596 KB Output is correct
43 Correct 1 ms 596 KB Output is correct
44 Correct 1 ms 852 KB Output is correct
45 Correct 1 ms 596 KB Output is correct
46 Correct 1 ms 724 KB Output is correct
47 Correct 0 ms 340 KB Output is correct
48 Correct 0 ms 340 KB Output is correct
49 Correct 1 ms 340 KB Output is correct
50 Correct 0 ms 340 KB Output is correct
51 Correct 0 ms 340 KB Output is correct
52 Correct 0 ms 356 KB Output is correct
53 Correct 1 ms 596 KB Output is correct
54 Correct 1 ms 596 KB Output is correct
55 Correct 0 ms 340 KB Output is correct
56 Correct 0 ms 340 KB Output is correct
57 Correct 1 ms 596 KB Output is correct
58 Correct 1 ms 596 KB Output is correct
59 Correct 1 ms 596 KB Output is correct
60 Correct 1 ms 596 KB Output is correct
61 Correct 1 ms 596 KB Output is correct
62 Correct 1 ms 596 KB Output is correct
63 Correct 1 ms 596 KB Output is correct
64 Correct 0 ms 468 KB Output is correct
65 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 8020 KB Output is correct
2 Correct 5 ms 2368 KB Output is correct
3 Correct 18 ms 7960 KB Output is correct
4 Correct 5 ms 2388 KB Output is correct
5 Correct 18 ms 7968 KB Output is correct
6 Correct 18 ms 8068 KB Output is correct
7 Correct 18 ms 7960 KB Output is correct
8 Correct 18 ms 7964 KB Output is correct
9 Correct 18 ms 8064 KB Output is correct
10 Correct 17 ms 8064 KB Output is correct
11 Correct 45 ms 15760 KB Output is correct
12 Correct 38 ms 15732 KB Output is correct
13 Correct 46 ms 15644 KB Output is correct
14 Correct 37 ms 15740 KB Output is correct
15 Correct 10 ms 4820 KB Output is correct
16 Correct 10 ms 4836 KB Output is correct
17 Correct 9 ms 4812 KB Output is correct
18 Correct 10 ms 4812 KB Output is correct
19 Correct 9 ms 4844 KB Output is correct
20 Correct 9 ms 4844 KB Output is correct
21 Correct 9 ms 4844 KB Output is correct
22 Correct 9 ms 4820 KB Output is correct
23 Correct 9 ms 4836 KB Output is correct
24 Correct 25 ms 15952 KB Output is correct
25 Correct 26 ms 15948 KB Output is correct
26 Correct 26 ms 16224 KB Output is correct
27 Correct 25 ms 16228 KB Output is correct
28 Correct 25 ms 16204 KB Output is correct
29 Correct 25 ms 16200 KB Output is correct
30 Correct 8 ms 5036 KB Output is correct
31 Correct 14 ms 8148 KB Output is correct
32 Correct 25 ms 14504 KB Output is correct
33 Correct 28 ms 16032 KB Output is correct
34 Correct 3 ms 1876 KB Output is correct
35 Correct 5 ms 3376 KB Output is correct
36 Correct 13 ms 8148 KB Output is correct
37 Correct 33 ms 16172 KB Output is correct
38 Correct 31 ms 15956 KB Output is correct
39 Correct 28 ms 16120 KB Output is correct
40 Correct 32 ms 15900 KB Output is correct
41 Correct 26 ms 16332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 8020 KB Output is correct
2 Correct 5 ms 2368 KB Output is correct
3 Correct 18 ms 7960 KB Output is correct
4 Correct 5 ms 2388 KB Output is correct
5 Correct 18 ms 7968 KB Output is correct
6 Correct 18 ms 8068 KB Output is correct
7 Correct 18 ms 7960 KB Output is correct
8 Correct 18 ms 7964 KB Output is correct
9 Correct 18 ms 8064 KB Output is correct
10 Correct 17 ms 8064 KB Output is correct
11 Correct 45 ms 15760 KB Output is correct
12 Correct 38 ms 15732 KB Output is correct
13 Correct 46 ms 15644 KB Output is correct
14 Correct 37 ms 15740 KB Output is correct
15 Correct 10 ms 4820 KB Output is correct
16 Correct 10 ms 4836 KB Output is correct
17 Correct 9 ms 4812 KB Output is correct
18 Correct 10 ms 4812 KB Output is correct
19 Correct 9 ms 4844 KB Output is correct
20 Correct 9 ms 4844 KB Output is correct
21 Correct 9 ms 4844 KB Output is correct
22 Correct 9 ms 4820 KB Output is correct
23 Correct 9 ms 4836 KB Output is correct
24 Correct 25 ms 15952 KB Output is correct
25 Correct 26 ms 15948 KB Output is correct
26 Correct 26 ms 16224 KB Output is correct
27 Correct 25 ms 16228 KB Output is correct
28 Correct 25 ms 16204 KB Output is correct
29 Correct 25 ms 16200 KB Output is correct
30 Correct 8 ms 5036 KB Output is correct
31 Correct 14 ms 8148 KB Output is correct
32 Correct 25 ms 14504 KB Output is correct
33 Correct 28 ms 16032 KB Output is correct
34 Correct 3 ms 1876 KB Output is correct
35 Correct 5 ms 3376 KB Output is correct
36 Correct 13 ms 8148 KB Output is correct
37 Correct 33 ms 16172 KB Output is correct
38 Correct 31 ms 15956 KB Output is correct
39 Correct 28 ms 16120 KB Output is correct
40 Correct 32 ms 15900 KB Output is correct
41 Correct 26 ms 16332 KB Output is correct
42 Correct 18 ms 8140 KB Output is correct
43 Correct 5 ms 2644 KB Output is correct
44 Correct 18 ms 8088 KB Output is correct
45 Correct 5 ms 2736 KB Output is correct
46 Correct 5 ms 2772 KB Output is correct
47 Correct 5 ms 2644 KB Output is correct
48 Correct 5 ms 2620 KB Output is correct
49 Correct 18 ms 8168 KB Output is correct
50 Correct 25 ms 8044 KB Output is correct
51 Correct 22 ms 8136 KB Output is correct
52 Correct 38 ms 15892 KB Output is correct
53 Correct 37 ms 15908 KB Output is correct
54 Correct 9 ms 5076 KB Output is correct
55 Correct 9 ms 5028 KB Output is correct
56 Correct 25 ms 14516 KB Output is correct
57 Correct 30 ms 16204 KB Output is correct
58 Correct 30 ms 16084 KB Output is correct
59 Correct 31 ms 16212 KB Output is correct
60 Correct 30 ms 16156 KB Output is correct
61 Correct 34 ms 17484 KB Output is correct
62 Correct 20 ms 17236 KB Output is correct
63 Correct 26 ms 16332 KB Output is correct
64 Correct 26 ms 16332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 0 ms 284 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 0 ms 340 KB Output is correct
17 Correct 0 ms 340 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 0 ms 340 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 0 ms 340 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 0 ms 340 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 0 ms 340 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 1 ms 596 KB Output is correct
35 Correct 1 ms 596 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 1 ms 596 KB Output is correct
39 Correct 1 ms 596 KB Output is correct
40 Correct 1 ms 596 KB Output is correct
41 Correct 1 ms 596 KB Output is correct
42 Correct 1 ms 596 KB Output is correct
43 Correct 1 ms 596 KB Output is correct
44 Correct 1 ms 852 KB Output is correct
45 Correct 1 ms 596 KB Output is correct
46 Correct 1 ms 724 KB Output is correct
47 Correct 0 ms 340 KB Output is correct
48 Correct 0 ms 340 KB Output is correct
49 Correct 1 ms 340 KB Output is correct
50 Correct 0 ms 340 KB Output is correct
51 Correct 0 ms 340 KB Output is correct
52 Correct 0 ms 356 KB Output is correct
53 Correct 1 ms 596 KB Output is correct
54 Correct 1 ms 596 KB Output is correct
55 Correct 0 ms 340 KB Output is correct
56 Correct 0 ms 340 KB Output is correct
57 Correct 1 ms 596 KB Output is correct
58 Correct 1 ms 596 KB Output is correct
59 Correct 1 ms 596 KB Output is correct
60 Correct 1 ms 596 KB Output is correct
61 Correct 1 ms 596 KB Output is correct
62 Correct 1 ms 596 KB Output is correct
63 Correct 1 ms 596 KB Output is correct
64 Correct 0 ms 468 KB Output is correct
65 Correct 1 ms 468 KB Output is correct
66 Correct 18 ms 8020 KB Output is correct
67 Correct 5 ms 2368 KB Output is correct
68 Correct 18 ms 7960 KB Output is correct
69 Correct 5 ms 2388 KB Output is correct
70 Correct 18 ms 7968 KB Output is correct
71 Correct 18 ms 8068 KB Output is correct
72 Correct 18 ms 7960 KB Output is correct
73 Correct 18 ms 7964 KB Output is correct
74 Correct 18 ms 8064 KB Output is correct
75 Correct 17 ms 8064 KB Output is correct
76 Correct 45 ms 15760 KB Output is correct
77 Correct 38 ms 15732 KB Output is correct
78 Correct 46 ms 15644 KB Output is correct
79 Correct 37 ms 15740 KB Output is correct
80 Correct 10 ms 4820 KB Output is correct
81 Correct 10 ms 4836 KB Output is correct
82 Correct 9 ms 4812 KB Output is correct
83 Correct 10 ms 4812 KB Output is correct
84 Correct 9 ms 4844 KB Output is correct
85 Correct 9 ms 4844 KB Output is correct
86 Correct 9 ms 4844 KB Output is correct
87 Correct 9 ms 4820 KB Output is correct
88 Correct 9 ms 4836 KB Output is correct
89 Correct 25 ms 15952 KB Output is correct
90 Correct 26 ms 15948 KB Output is correct
91 Correct 26 ms 16224 KB Output is correct
92 Correct 25 ms 16228 KB Output is correct
93 Correct 25 ms 16204 KB Output is correct
94 Correct 25 ms 16200 KB Output is correct
95 Correct 8 ms 5036 KB Output is correct
96 Correct 14 ms 8148 KB Output is correct
97 Correct 25 ms 14504 KB Output is correct
98 Correct 28 ms 16032 KB Output is correct
99 Correct 3 ms 1876 KB Output is correct
100 Correct 5 ms 3376 KB Output is correct
101 Correct 13 ms 8148 KB Output is correct
102 Correct 33 ms 16172 KB Output is correct
103 Correct 31 ms 15956 KB Output is correct
104 Correct 28 ms 16120 KB Output is correct
105 Correct 32 ms 15900 KB Output is correct
106 Correct 26 ms 16332 KB Output is correct
107 Correct 18 ms 8140 KB Output is correct
108 Correct 5 ms 2644 KB Output is correct
109 Correct 18 ms 8088 KB Output is correct
110 Correct 5 ms 2736 KB Output is correct
111 Correct 5 ms 2772 KB Output is correct
112 Correct 5 ms 2644 KB Output is correct
113 Correct 5 ms 2620 KB Output is correct
114 Correct 18 ms 8168 KB Output is correct
115 Correct 25 ms 8044 KB Output is correct
116 Correct 22 ms 8136 KB Output is correct
117 Correct 38 ms 15892 KB Output is correct
118 Correct 37 ms 15908 KB Output is correct
119 Correct 9 ms 5076 KB Output is correct
120 Correct 9 ms 5028 KB Output is correct
121 Correct 25 ms 14516 KB Output is correct
122 Correct 30 ms 16204 KB Output is correct
123 Correct 30 ms 16084 KB Output is correct
124 Correct 31 ms 16212 KB Output is correct
125 Correct 30 ms 16156 KB Output is correct
126 Correct 34 ms 17484 KB Output is correct
127 Correct 20 ms 17236 KB Output is correct
128 Correct 26 ms 16332 KB Output is correct
129 Correct 26 ms 16332 KB Output is correct
130 Correct 1 ms 340 KB Output is correct
131 Correct 0 ms 340 KB Output is correct
132 Correct 0 ms 340 KB Output is correct
133 Correct 39 ms 15820 KB Output is correct
134 Correct 37 ms 15752 KB Output is correct
135 Correct 36 ms 15764 KB Output is correct
136 Correct 9 ms 4564 KB Output is correct
137 Correct 9 ms 4500 KB Output is correct
138 Correct 9 ms 4520 KB Output is correct
139 Correct 10 ms 4588 KB Output is correct
140 Correct 9 ms 4564 KB Output is correct
141 Correct 12 ms 4532 KB Output is correct
142 Correct 38 ms 16072 KB Output is correct
143 Correct 36 ms 15692 KB Output is correct
144 Correct 28 ms 15768 KB Output is correct
145 Correct 25 ms 14144 KB Output is correct
146 Correct 31 ms 15992 KB Output is correct
147 Correct 36 ms 18488 KB Output is correct
148 Correct 48 ms 26688 KB Output is correct
149 Correct 52 ms 29728 KB Output is correct
150 Correct 53 ms 22200 KB Output is correct
151 Correct 29 ms 17840 KB Output is correct
152 Correct 32 ms 19496 KB Output is correct
153 Correct 26 ms 16204 KB Output is correct
154 Correct 26 ms 16168 KB Output is correct
155 Correct 28 ms 16156 KB Output is correct
156 Correct 25 ms 16204 KB Output is correct
157 Correct 28 ms 16156 KB Output is correct
158 Correct 31 ms 16204 KB Output is correct
159 Correct 28 ms 16292 KB Output is correct
160 Correct 32 ms 16212 KB Output is correct