Submission #741203

# Submission time Handle Problem Language Result Execution time Memory
741203 2023-05-13T20:09:06 Z rainboy Shopping Plans (CCO20_day2problem3) C
25 / 25
567 ms 38988 KB
#include <stdio.h>
#include <stdlib.h>

#define M	200000
#define K	200000
#define INF	0x3f3f3f3f3f3f3f3fLL

int min(int a, int b) { return a < b ? a : b; }

unsigned int X = 12345;

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

int *cc[M], nn[M], hh[M], ll[M], rr[M], m; long long dd[M], ss[K];

void append(int a, int c) {
	int i = nn[a]++;

	if (i >= 2 && (i & i - 1) == 0)
		cc[a] = (int *) realloc(cc[a], i * 2 * sizeof *cc[a]);
	cc[a][i] = c;
}

int compare_c(int a, int b) { return a - b; }
int compare_d(int h1, int h2) { return dd[h1] != dd[h2] ? (dd[h1] < dd[h2] ? -1 : 1) : 0; }
int compare_s(int h1, int h2) { return ss[h1] != ss[h2] ? (ss[h1] < ss[h2] ? -1 : 1) : 0; }

int (*compare)(int, int);

void sort(int *hh, int l, int r) {
	while (l < r) {
		int i = l, j = l, k = r, h = hh[l + rand_() % (r - l)], tmp;

		while (j < k) {
			int c = compare(hh[j], h);

			if (c == 0)
				j++;
			else if (c < 0) {
				tmp = hh[i], hh[i] = hh[j], hh[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = hh[j], hh[j] = hh[k], hh[k] = tmp;
			}
		}
		sort(hh, l, i);
		l = k;
	}
}

int k, k_;

int solve(long long s, long long s_, int h, int i, int j) {
	int h_, i_;
	long long s1;

	ss[k++] = s;
	if (k == k_)
		return 1;
	while (h < m) {
		h_ = hh[h];
		if (i == 0 && j == ll[h_] && s + dd[h_] > s_)
			break;
		if (i == 0 && j < rr[h_] && s + cc[h_][j] <= s_ && solve(s + cc[h_][j], s_, h, i, j + 1))
			return 1;
		if (j < nn[h_])
			for (i_ = j; i_ > i && (s1 = s - cc[h_][i_ - 1] + cc[h_][j]) <= s_; i_--)
				if (solve(s1, s_, h, i_, j + 1))
					return 1;
		h++, i = 0, j = h == m ? 0 : ll[hh[h]];
	}
	return 0;
}

int main() {
	int n, h, i, c;
	long long s, lower, upper;

	scanf("%d%d%d", &n, &m, &k_);
	for (h = 0; h < m; h++)
		cc[h] = (int *) malloc(2 * sizeof *cc[h]);
	while (n--) {
		scanf("%d%d", &h, &c), h--;
		append(h, c);
	}
	for (h = 0; h < m; h++)
		compare = compare_c, sort(cc[h], 0, nn[h]);
	s = 0;
	for (h = 0; h < m; h++) {
		scanf("%d%d", &ll[h], &rr[h]);
		rr[h] = min(rr[h], nn[h]);
		if (ll[h] > rr[h]) {
			while (k_--)
				printf("-1\n");
			return 0;
		}
		for (i = 0; i < ll[h]; i++)
			s += cc[h][i];
		if (ll[h] == 0)
			dd[h] = rr[h] == 0 ? INF : cc[h][0];
		else if (ll[h] < nn[h])
			dd[h] = cc[h][ll[h]] - cc[h][ll[h] - 1];
		else
			dd[h] = -1;
	}
	for (h = 0; h < m; h++)
		hh[h] = h;
	compare = compare_d, sort(hh, 0, m);
	solve(s, 7, 0, 0, ll[hh[0]]);
	lower = s - 1, upper = INF;
	while (upper - lower > 1) {
		long long s_ = (lower + upper) / 2;

		k = 0;
		if (solve(s, s_, 0, 0, ll[hh[0]]))
			upper = s_;
		else
			lower = s_;
	}
	k = 0;
	if (lower >= s)
		solve(s, lower, 0, 0, ll[hh[0]]);
	while (k < k_)
		ss[k++] = upper;
	for (h = 0; h < k; h++)
		hh[h] = h;
	compare = compare_s, sort(hh, 0, k);
	for (h = 0; h < k; h++)
		printf("%lld\n", ss[hh[h]] == INF ? -1 : ss[hh[h]]);
	return 0;
}

Compilation message

Main.c: In function 'append':
Main.c:21:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   21 |  if (i >= 2 && (i & i - 1) == 0)
      |                     ~~^~~
Main.c: In function 'main':
Main.c:82:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |  scanf("%d%d%d", &n, &m, &k_);
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:86:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |   scanf("%d%d", &h, &c), h--;
      |   ^~~~~~~~~~~~~~~~~~~~~
Main.c:93:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |   scanf("%d%d", &ll[h], &rr[h]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 724 KB Output is correct
2 Correct 7 ms 724 KB Output is correct
3 Correct 6 ms 728 KB Output is correct
4 Correct 8 ms 804 KB Output is correct
5 Correct 7 ms 664 KB Output is correct
6 Correct 6 ms 692 KB Output is correct
7 Correct 6 ms 852 KB Output is correct
8 Correct 5 ms 816 KB Output is correct
9 Correct 4 ms 724 KB Output is correct
10 Correct 7 ms 688 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 3 ms 340 KB Output is correct
13 Correct 8 ms 424 KB Output is correct
14 Correct 6 ms 724 KB Output is correct
15 Correct 4 ms 352 KB Output is correct
16 Correct 4 ms 428 KB Output is correct
17 Correct 6 ms 816 KB Output is correct
18 Correct 4 ms 340 KB Output is correct
19 Correct 5 ms 340 KB Output is correct
20 Correct 6 ms 724 KB Output is correct
21 Correct 3 ms 288 KB Output is correct
22 Correct 4 ms 424 KB Output is correct
23 Correct 5 ms 860 KB Output is correct
24 Correct 5 ms 724 KB Output is correct
25 Correct 4 ms 724 KB Output is correct
26 Correct 5 ms 724 KB Output is correct
27 Correct 5 ms 728 KB Output is correct
28 Correct 4 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 183 ms 4460 KB Output is correct
2 Correct 192 ms 4516 KB Output is correct
3 Correct 186 ms 4484 KB Output is correct
4 Correct 174 ms 4496 KB Output is correct
5 Correct 183 ms 4504 KB Output is correct
6 Correct 177 ms 4528 KB Output is correct
7 Correct 178 ms 4480 KB Output is correct
8 Correct 135 ms 3960 KB Output is correct
9 Correct 21 ms 3664 KB Output is correct
10 Correct 165 ms 4536 KB Output is correct
11 Correct 6 ms 980 KB Output is correct
12 Correct 85 ms 3552 KB Output is correct
13 Correct 193 ms 3788 KB Output is correct
14 Correct 170 ms 4556 KB Output is correct
15 Correct 31 ms 3164 KB Output is correct
16 Correct 165 ms 3880 KB Output is correct
17 Correct 172 ms 4556 KB Output is correct
18 Correct 58 ms 3408 KB Output is correct
19 Correct 197 ms 3932 KB Output is correct
20 Correct 172 ms 4504 KB Output is correct
21 Correct 23 ms 3156 KB Output is correct
22 Correct 152 ms 3540 KB Output is correct
23 Correct 129 ms 3848 KB Output is correct
24 Correct 6 ms 1108 KB Output is correct
25 Correct 7 ms 1076 KB Output is correct
26 Correct 156 ms 4672 KB Output is correct
27 Correct 160 ms 4484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 724 KB Output is correct
2 Correct 7 ms 724 KB Output is correct
3 Correct 6 ms 728 KB Output is correct
4 Correct 8 ms 804 KB Output is correct
5 Correct 7 ms 664 KB Output is correct
6 Correct 6 ms 692 KB Output is correct
7 Correct 6 ms 852 KB Output is correct
8 Correct 5 ms 816 KB Output is correct
9 Correct 4 ms 724 KB Output is correct
10 Correct 7 ms 688 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 3 ms 340 KB Output is correct
13 Correct 8 ms 424 KB Output is correct
14 Correct 6 ms 724 KB Output is correct
15 Correct 4 ms 352 KB Output is correct
16 Correct 4 ms 428 KB Output is correct
17 Correct 6 ms 816 KB Output is correct
18 Correct 4 ms 340 KB Output is correct
19 Correct 5 ms 340 KB Output is correct
20 Correct 6 ms 724 KB Output is correct
21 Correct 3 ms 288 KB Output is correct
22 Correct 4 ms 424 KB Output is correct
23 Correct 5 ms 860 KB Output is correct
24 Correct 5 ms 724 KB Output is correct
25 Correct 4 ms 724 KB Output is correct
26 Correct 5 ms 724 KB Output is correct
27 Correct 5 ms 728 KB Output is correct
28 Correct 4 ms 724 KB Output is correct
29 Correct 183 ms 4460 KB Output is correct
30 Correct 192 ms 4516 KB Output is correct
31 Correct 186 ms 4484 KB Output is correct
32 Correct 174 ms 4496 KB Output is correct
33 Correct 183 ms 4504 KB Output is correct
34 Correct 177 ms 4528 KB Output is correct
35 Correct 178 ms 4480 KB Output is correct
36 Correct 135 ms 3960 KB Output is correct
37 Correct 21 ms 3664 KB Output is correct
38 Correct 165 ms 4536 KB Output is correct
39 Correct 6 ms 980 KB Output is correct
40 Correct 85 ms 3552 KB Output is correct
41 Correct 193 ms 3788 KB Output is correct
42 Correct 170 ms 4556 KB Output is correct
43 Correct 31 ms 3164 KB Output is correct
44 Correct 165 ms 3880 KB Output is correct
45 Correct 172 ms 4556 KB Output is correct
46 Correct 58 ms 3408 KB Output is correct
47 Correct 197 ms 3932 KB Output is correct
48 Correct 172 ms 4504 KB Output is correct
49 Correct 23 ms 3156 KB Output is correct
50 Correct 152 ms 3540 KB Output is correct
51 Correct 129 ms 3848 KB Output is correct
52 Correct 6 ms 1108 KB Output is correct
53 Correct 7 ms 1076 KB Output is correct
54 Correct 156 ms 4672 KB Output is correct
55 Correct 160 ms 4484 KB Output is correct
56 Correct 427 ms 23680 KB Output is correct
57 Correct 402 ms 25480 KB Output is correct
58 Correct 416 ms 24856 KB Output is correct
59 Correct 388 ms 27160 KB Output is correct
60 Correct 437 ms 23432 KB Output is correct
61 Correct 425 ms 24804 KB Output is correct
62 Correct 383 ms 27280 KB Output is correct
63 Correct 346 ms 26228 KB Output is correct
64 Correct 417 ms 26568 KB Output is correct
65 Correct 408 ms 25596 KB Output is correct
66 Correct 65 ms 8108 KB Output is correct
67 Correct 207 ms 4804 KB Output is correct
68 Correct 216 ms 4960 KB Output is correct
69 Correct 442 ms 24568 KB Output is correct
70 Correct 28 ms 3276 KB Output is correct
71 Correct 216 ms 4964 KB Output is correct
72 Correct 379 ms 27004 KB Output is correct
73 Correct 22 ms 3272 KB Output is correct
74 Correct 217 ms 4960 KB Output is correct
75 Correct 458 ms 23428 KB Output is correct
76 Correct 20 ms 3268 KB Output is correct
77 Correct 184 ms 4584 KB Output is correct
78 Correct 340 ms 26056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 150 ms 3396 KB Output is correct
2 Correct 137 ms 3352 KB Output is correct
3 Correct 26 ms 3228 KB Output is correct
4 Correct 28 ms 3164 KB Output is correct
5 Correct 558 ms 31784 KB Output is correct
6 Correct 502 ms 34532 KB Output is correct
7 Correct 525 ms 33156 KB Output is correct
8 Correct 444 ms 37324 KB Output is correct
9 Correct 519 ms 31276 KB Output is correct
10 Correct 567 ms 32664 KB Output is correct
11 Correct 400 ms 38040 KB Output is correct
12 Correct 346 ms 38988 KB Output is correct
13 Correct 342 ms 38416 KB Output is correct
14 Correct 495 ms 34556 KB Output is correct
15 Correct 489 ms 34612 KB Output is correct
16 Correct 210 ms 4560 KB Output is correct
17 Correct 271 ms 4472 KB Output is correct
18 Correct 539 ms 36432 KB Output is correct
19 Correct 181 ms 4564 KB Output is correct
20 Correct 245 ms 4608 KB Output is correct
21 Correct 426 ms 38320 KB Output is correct
22 Correct 202 ms 4568 KB Output is correct
23 Correct 231 ms 4416 KB Output is correct
24 Correct 545 ms 35316 KB Output is correct
25 Correct 186 ms 4716 KB Output is correct
26 Correct 185 ms 4480 KB Output is correct
27 Correct 351 ms 38480 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 724 KB Output is correct
2 Correct 7 ms 724 KB Output is correct
3 Correct 6 ms 728 KB Output is correct
4 Correct 8 ms 804 KB Output is correct
5 Correct 7 ms 664 KB Output is correct
6 Correct 6 ms 692 KB Output is correct
7 Correct 6 ms 852 KB Output is correct
8 Correct 5 ms 816 KB Output is correct
9 Correct 4 ms 724 KB Output is correct
10 Correct 7 ms 688 KB Output is correct
11 Correct 2 ms 468 KB Output is correct
12 Correct 3 ms 340 KB Output is correct
13 Correct 8 ms 424 KB Output is correct
14 Correct 6 ms 724 KB Output is correct
15 Correct 4 ms 352 KB Output is correct
16 Correct 4 ms 428 KB Output is correct
17 Correct 6 ms 816 KB Output is correct
18 Correct 4 ms 340 KB Output is correct
19 Correct 5 ms 340 KB Output is correct
20 Correct 6 ms 724 KB Output is correct
21 Correct 3 ms 288 KB Output is correct
22 Correct 4 ms 424 KB Output is correct
23 Correct 5 ms 860 KB Output is correct
24 Correct 5 ms 724 KB Output is correct
25 Correct 4 ms 724 KB Output is correct
26 Correct 5 ms 724 KB Output is correct
27 Correct 5 ms 728 KB Output is correct
28 Correct 4 ms 724 KB Output is correct
29 Correct 183 ms 4460 KB Output is correct
30 Correct 192 ms 4516 KB Output is correct
31 Correct 186 ms 4484 KB Output is correct
32 Correct 174 ms 4496 KB Output is correct
33 Correct 183 ms 4504 KB Output is correct
34 Correct 177 ms 4528 KB Output is correct
35 Correct 178 ms 4480 KB Output is correct
36 Correct 135 ms 3960 KB Output is correct
37 Correct 21 ms 3664 KB Output is correct
38 Correct 165 ms 4536 KB Output is correct
39 Correct 6 ms 980 KB Output is correct
40 Correct 85 ms 3552 KB Output is correct
41 Correct 193 ms 3788 KB Output is correct
42 Correct 170 ms 4556 KB Output is correct
43 Correct 31 ms 3164 KB Output is correct
44 Correct 165 ms 3880 KB Output is correct
45 Correct 172 ms 4556 KB Output is correct
46 Correct 58 ms 3408 KB Output is correct
47 Correct 197 ms 3932 KB Output is correct
48 Correct 172 ms 4504 KB Output is correct
49 Correct 23 ms 3156 KB Output is correct
50 Correct 152 ms 3540 KB Output is correct
51 Correct 129 ms 3848 KB Output is correct
52 Correct 6 ms 1108 KB Output is correct
53 Correct 7 ms 1076 KB Output is correct
54 Correct 156 ms 4672 KB Output is correct
55 Correct 160 ms 4484 KB Output is correct
56 Correct 427 ms 23680 KB Output is correct
57 Correct 402 ms 25480 KB Output is correct
58 Correct 416 ms 24856 KB Output is correct
59 Correct 388 ms 27160 KB Output is correct
60 Correct 437 ms 23432 KB Output is correct
61 Correct 425 ms 24804 KB Output is correct
62 Correct 383 ms 27280 KB Output is correct
63 Correct 346 ms 26228 KB Output is correct
64 Correct 417 ms 26568 KB Output is correct
65 Correct 408 ms 25596 KB Output is correct
66 Correct 65 ms 8108 KB Output is correct
67 Correct 207 ms 4804 KB Output is correct
68 Correct 216 ms 4960 KB Output is correct
69 Correct 442 ms 24568 KB Output is correct
70 Correct 28 ms 3276 KB Output is correct
71 Correct 216 ms 4964 KB Output is correct
72 Correct 379 ms 27004 KB Output is correct
73 Correct 22 ms 3272 KB Output is correct
74 Correct 217 ms 4960 KB Output is correct
75 Correct 458 ms 23428 KB Output is correct
76 Correct 20 ms 3268 KB Output is correct
77 Correct 184 ms 4584 KB Output is correct
78 Correct 340 ms 26056 KB Output is correct
79 Correct 150 ms 3396 KB Output is correct
80 Correct 137 ms 3352 KB Output is correct
81 Correct 26 ms 3228 KB Output is correct
82 Correct 28 ms 3164 KB Output is correct
83 Correct 558 ms 31784 KB Output is correct
84 Correct 502 ms 34532 KB Output is correct
85 Correct 525 ms 33156 KB Output is correct
86 Correct 444 ms 37324 KB Output is correct
87 Correct 519 ms 31276 KB Output is correct
88 Correct 567 ms 32664 KB Output is correct
89 Correct 400 ms 38040 KB Output is correct
90 Correct 346 ms 38988 KB Output is correct
91 Correct 342 ms 38416 KB Output is correct
92 Correct 495 ms 34556 KB Output is correct
93 Correct 489 ms 34612 KB Output is correct
94 Correct 210 ms 4560 KB Output is correct
95 Correct 271 ms 4472 KB Output is correct
96 Correct 539 ms 36432 KB Output is correct
97 Correct 181 ms 4564 KB Output is correct
98 Correct 245 ms 4608 KB Output is correct
99 Correct 426 ms 38320 KB Output is correct
100 Correct 202 ms 4568 KB Output is correct
101 Correct 231 ms 4416 KB Output is correct
102 Correct 545 ms 35316 KB Output is correct
103 Correct 186 ms 4716 KB Output is correct
104 Correct 185 ms 4480 KB Output is correct
105 Correct 351 ms 38480 KB Output is correct
106 Correct 130 ms 3404 KB Output is correct
107 Correct 140 ms 3420 KB Output is correct
108 Correct 134 ms 3400 KB Output is correct
109 Correct 151 ms 3620 KB Output is correct
110 Correct 526 ms 32176 KB Output is correct
111 Correct 512 ms 33552 KB Output is correct
112 Correct 501 ms 32968 KB Output is correct
113 Correct 453 ms 34124 KB Output is correct
114 Correct 530 ms 32060 KB Output is correct
115 Correct 526 ms 33020 KB Output is correct
116 Correct 436 ms 34272 KB Output is correct
117 Correct 347 ms 33824 KB Output is correct
118 Correct 304 ms 30644 KB Output is correct
119 Correct 76 ms 13512 KB Output is correct
120 Correct 548 ms 33580 KB Output is correct
121 Correct 196 ms 4824 KB Output is correct
122 Correct 212 ms 5072 KB Output is correct
123 Correct 539 ms 29544 KB Output is correct
124 Correct 195 ms 4860 KB Output is correct
125 Correct 230 ms 4976 KB Output is correct
126 Correct 432 ms 30948 KB Output is correct
127 Correct 188 ms 4940 KB Output is correct
128 Correct 209 ms 4980 KB Output is correct
129 Correct 497 ms 29000 KB Output is correct
130 Correct 216 ms 4824 KB Output is correct
131 Correct 199 ms 4996 KB Output is correct
132 Correct 313 ms 25420 KB Output is correct