Submission #503410

# Submission time Handle Problem Language Result Execution time Memory
503410 2022-01-07T21:15:45 Z rainboy Akcija (COCI21_akcija) C
10 / 110
5000 ms 45516 KB
/* https://codeforces.com/blog/entry/97754#comment-866029 (errorgorn) */
#include <stdio.h>

#define N	2000
#define K	2000
#define S	2000000000002LL

unsigned int X = 12345;

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

int ww[N], tt[N], ii[N];
int cc[K], hh[K], k, k_; long long ss[K];

int compare_t(int i, int j) {
	return tt[i] - tt[j];
}

int compare_cs(int h1, int h2) {
	return cc[h1] != cc[h2] ? cc[h2] - cc[h1] : (ss[h1] == ss[h2] ? 0 : (ss[h1] < ss[h2] ? -1 : 1));
}

int (*compare)(int, int);

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

		while (j < k) {
			int c = compare(ii[j], i_);

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

int cc_[N + 1][N + 1]; long long ss_[N + 1][N + 1];

int branch(int n, int i, int c, long long s, int c_, long long s_) {
	if (cc_[i][c] < c_ || cc_[i][c] == c_ && s + ss_[i][c] > s_)
		return 0;
	if (i == n) {
		cc[k] = c, ss[k] = s, k++;
		return k == k_;
	}
	if (branch(n, i + 1, c, s, c_, s_))
		return 1;
	if (tt[ii[i]] > c && branch(n, i + 1, c + 1, s + ww[ii[i]], c_, s_))
		return 1;
	return 0;
}

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

	scanf("%d%d", &n, &k_);
	for (i = 0; i < n; i++) {
		scanf("%d%d", &ww[i], &tt[i]);
		ii[i] = i;
	}
	compare = compare_t, sort(ii, 0, n);
	for (c = 0; c <= n; c++)
		cc_[n][c] = c;
	for (i = n - 1; i >= 0; i--)
		for (c = 0; c <= n; c++) {
			cc_[i][c] = cc_[i + 1][c], ss_[i][c] = ss_[i + 1][c];
			if (tt[ii[i]] > c) {
				int c_ = cc_[i + 1][c + 1], s_ = ss_[i + 1][c + 1] + ww[ii[i]];

				if (cc_[i][c] < c_ || cc_[i][c] == c_ && ss_[i][c] > s_)
					cc_[i][c] = c_, ss_[i][c] = s_;
			}
		}
	lower = -1, upper = (n + 2) * S;
	while (upper - lower > 1) {
		cs = (lower + upper) / 2, c = n - cs / S, s = cs % S;
		k = 0;
		if (branch(n, 0, 0, 0, c, s))
			upper = cs;
		else
			lower = cs;
	}
	if (lower >= 0) {
		cs = lower, c = n - cs / S, s = cs % S;
		k = 0, branch(n, 0, 0, 0, c, s);
	}
	cs = upper, c = n - cs / S, s = cs % S;
	while (k < k_)
		cc[k] = c, ss[k] = s, k++;
	for (h = 0; h < k; h++)
		hh[h] = h;
	compare = compare_cs, sort(hh, 0, k);
	for (h = 0; h < k; h++)
		printf("%d %lld\n", cc[hh[h]], ss[hh[h]]);
	return 0;
}

Compilation message

Main.c: In function 'branch':
Main.c:52:40: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   52 |  if (cc_[i][c] < c_ || cc_[i][c] == c_ && s + ss_[i][c] > s_)
      |                        ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
Main.c: In function 'main':
Main.c:83:43: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   83 |     if (cc_[i][c] < c_ || cc_[i][c] == c_ && ss_[i][c] > s_)
      |                           ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
Main.c:69:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |  scanf("%d%d", &n, &k_);
      |  ^~~~~~~~~~~~~~~~~~~~~~
Main.c:71:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |   scanf("%d%d", &ww[i], &tt[i]);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Execution timed out 5046 ms 45516 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5046 ms 45516 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5070 ms 45516 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 460 KB Output is correct
2 Correct 7 ms 404 KB Output is correct
3 Correct 4 ms 460 KB Output is correct
4 Correct 3 ms 460 KB Output is correct
5 Correct 5 ms 460 KB Output is correct
6 Correct 0 ms 460 KB Output is correct
7 Correct 4 ms 460 KB Output is correct
8 Correct 3 ms 460 KB Output is correct
9 Correct 3 ms 460 KB Output is correct
10 Correct 0 ms 460 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 7 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 36 ms 1100 KB Output is correct
2 Execution timed out 5090 ms 1228 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 5046 ms 45516 KB Time limit exceeded
2 Halted 0 ms 0 KB -