Submission #741200

# Submission time Handle Problem Language Result Execution time Memory
741200 2023-05-13T19:59:09 Z rainboy Shopping Plans (CCO20_day2problem3) C
0 / 25
175 ms 4576 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(int h, long long s, long long s_, int i, int j) {
	int h_, i_;
	long long s1;

	ss[k++] = s;
	if (k == k_)
		return 1;
	while (h < m && s + dd[h_ = hh[h]] <= s_) {
		if (i == 0 && j < rr[h_] && s + cc[h_][j] <= s_ && solve(h, s + cc[h_][j], s_, 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(h, s1, s_, 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);
	lower = s - 1, upper = INF;
	while (upper - lower > 1) {
		long long s_ = (lower + upper) / 2;

		k = 0;
		if (solve(0, s, s_, 0, ll[hh[0]]))
			upper = s_;
		else
			lower = s_;
	}
	k = 0;
	if (lower >= s)
		solve(0, s, lower, 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:79:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |  scanf("%d%d%d", &n, &m, &k_);
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:83:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   83 |   scanf("%d%d", &h, &c), h--;
      |   ^~~~~~~~~~~~~~~~~~~~~
Main.c:90:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   90 |   scanf("%d%d", &ll[h], &rr[h]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 696 KB Output is correct
2 Incorrect 6 ms 724 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 175 ms 4556 KB Output is correct
2 Correct 167 ms 4556 KB Output is correct
3 Correct 162 ms 4528 KB Output is correct
4 Incorrect 157 ms 4576 KB Output isn't correct
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 696 KB Output is correct
2 Incorrect 6 ms 724 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 154 ms 3404 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 696 KB Output is correct
2 Incorrect 6 ms 724 KB Output isn't correct
3 Halted 0 ms 0 KB -