Submission #503412

# Submission time Handle Problem Language Result Execution time Memory
503412 2022-01-07T21:20:24 Z rainboy Akcija (COCI21_akcija) C
110 / 110
2546 ms 47472 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];
				long long 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:84:43: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   84 |     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 Correct 23 ms 45516 KB Output is correct
2 Correct 23 ms 47356 KB Output is correct
3 Correct 22 ms 43596 KB Output is correct
4 Correct 21 ms 43044 KB Output is correct
5 Correct 23 ms 47028 KB Output is correct
6 Correct 19 ms 42896 KB Output is correct
7 Correct 21 ms 47176 KB Output is correct
8 Correct 21 ms 43800 KB Output is correct
9 Correct 19 ms 42796 KB Output is correct
10 Correct 21 ms 44444 KB Output is correct
11 Correct 0 ms 284 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 45516 KB Output is correct
2 Correct 23 ms 47356 KB Output is correct
3 Correct 22 ms 43596 KB Output is correct
4 Correct 21 ms 43044 KB Output is correct
5 Correct 23 ms 47028 KB Output is correct
6 Correct 19 ms 42896 KB Output is correct
7 Correct 21 ms 47176 KB Output is correct
8 Correct 21 ms 43800 KB Output is correct
9 Correct 19 ms 42796 KB Output is correct
10 Correct 21 ms 44444 KB Output is correct
11 Correct 0 ms 284 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 716 KB Output is correct
14 Correct 22 ms 45512 KB Output is correct
15 Correct 24 ms 47348 KB Output is correct
16 Correct 21 ms 43568 KB Output is correct
17 Correct 21 ms 43084 KB Output is correct
18 Correct 24 ms 46992 KB Output is correct
19 Correct 19 ms 42956 KB Output is correct
20 Correct 22 ms 47240 KB Output is correct
21 Correct 20 ms 43760 KB Output is correct
22 Correct 20 ms 42728 KB Output is correct
23 Correct 20 ms 44444 KB Output is correct
24 Correct 0 ms 204 KB Output is correct
25 Correct 0 ms 204 KB Output is correct
26 Correct 0 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 45552 KB Output is correct
2 Correct 26 ms 47392 KB Output is correct
3 Correct 22 ms 43544 KB Output is correct
4 Correct 23 ms 43028 KB Output is correct
5 Correct 25 ms 47028 KB Output is correct
6 Correct 20 ms 42956 KB Output is correct
7 Correct 22 ms 47136 KB Output is correct
8 Correct 20 ms 43724 KB Output is correct
9 Correct 21 ms 42808 KB Output is correct
10 Correct 22 ms 44476 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 284 KB Output is correct
13 Correct 1 ms 716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 460 KB Output is correct
2 Correct 5 ms 492 KB Output is correct
3 Correct 5 ms 460 KB Output is correct
4 Correct 3 ms 464 KB Output is correct
5 Correct 6 ms 500 KB Output is correct
6 Correct 0 ms 464 KB Output is correct
7 Correct 4 ms 460 KB Output is correct
8 Correct 2 ms 464 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 1 ms 460 KB Output is correct
11 Correct 0 ms 208 KB Output is correct
12 Correct 0 ms 336 KB Output is correct
13 Correct 3 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1192 KB Output is correct
2 Correct 4 ms 1228 KB Output is correct
3 Correct 2 ms 1052 KB Output is correct
4 Correct 2 ms 1100 KB Output is correct
5 Correct 3 ms 1180 KB Output is correct
6 Correct 2 ms 1100 KB Output is correct
7 Correct 2 ms 1180 KB Output is correct
8 Correct 2 ms 1052 KB Output is correct
9 Correct 2 ms 1228 KB Output is correct
10 Correct 2 ms 1100 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 1 ms 588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 45516 KB Output is correct
2 Correct 23 ms 47356 KB Output is correct
3 Correct 22 ms 43596 KB Output is correct
4 Correct 21 ms 43044 KB Output is correct
5 Correct 23 ms 47028 KB Output is correct
6 Correct 19 ms 42896 KB Output is correct
7 Correct 21 ms 47176 KB Output is correct
8 Correct 21 ms 43800 KB Output is correct
9 Correct 19 ms 42796 KB Output is correct
10 Correct 21 ms 44444 KB Output is correct
11 Correct 0 ms 284 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 716 KB Output is correct
14 Correct 22 ms 45512 KB Output is correct
15 Correct 24 ms 47348 KB Output is correct
16 Correct 21 ms 43568 KB Output is correct
17 Correct 21 ms 43084 KB Output is correct
18 Correct 24 ms 46992 KB Output is correct
19 Correct 19 ms 42956 KB Output is correct
20 Correct 22 ms 47240 KB Output is correct
21 Correct 20 ms 43760 KB Output is correct
22 Correct 20 ms 42728 KB Output is correct
23 Correct 20 ms 44444 KB Output is correct
24 Correct 0 ms 204 KB Output is correct
25 Correct 0 ms 204 KB Output is correct
26 Correct 0 ms 716 KB Output is correct
27 Correct 26 ms 45552 KB Output is correct
28 Correct 26 ms 47392 KB Output is correct
29 Correct 22 ms 43544 KB Output is correct
30 Correct 23 ms 43028 KB Output is correct
31 Correct 25 ms 47028 KB Output is correct
32 Correct 20 ms 42956 KB Output is correct
33 Correct 22 ms 47136 KB Output is correct
34 Correct 20 ms 43724 KB Output is correct
35 Correct 21 ms 42808 KB Output is correct
36 Correct 22 ms 44476 KB Output is correct
37 Correct 0 ms 204 KB Output is correct
38 Correct 0 ms 284 KB Output is correct
39 Correct 1 ms 716 KB Output is correct
40 Correct 4 ms 460 KB Output is correct
41 Correct 5 ms 492 KB Output is correct
42 Correct 5 ms 460 KB Output is correct
43 Correct 3 ms 464 KB Output is correct
44 Correct 6 ms 500 KB Output is correct
45 Correct 0 ms 464 KB Output is correct
46 Correct 4 ms 460 KB Output is correct
47 Correct 2 ms 464 KB Output is correct
48 Correct 2 ms 468 KB Output is correct
49 Correct 1 ms 460 KB Output is correct
50 Correct 0 ms 208 KB Output is correct
51 Correct 0 ms 336 KB Output is correct
52 Correct 3 ms 460 KB Output is correct
53 Correct 2 ms 1192 KB Output is correct
54 Correct 4 ms 1228 KB Output is correct
55 Correct 2 ms 1052 KB Output is correct
56 Correct 2 ms 1100 KB Output is correct
57 Correct 3 ms 1180 KB Output is correct
58 Correct 2 ms 1100 KB Output is correct
59 Correct 2 ms 1180 KB Output is correct
60 Correct 2 ms 1052 KB Output is correct
61 Correct 2 ms 1228 KB Output is correct
62 Correct 2 ms 1100 KB Output is correct
63 Correct 0 ms 204 KB Output is correct
64 Correct 0 ms 332 KB Output is correct
65 Correct 1 ms 588 KB Output is correct
66 Correct 1271 ms 45564 KB Output is correct
67 Correct 1625 ms 47472 KB Output is correct
68 Correct 1533 ms 43624 KB Output is correct
69 Correct 1260 ms 43092 KB Output is correct
70 Correct 2546 ms 47020 KB Output is correct
71 Correct 886 ms 43020 KB Output is correct
72 Correct 761 ms 47228 KB Output is correct
73 Correct 305 ms 43816 KB Output is correct
74 Correct 483 ms 42800 KB Output is correct
75 Correct 1315 ms 44484 KB Output is correct
76 Correct 0 ms 204 KB Output is correct
77 Correct 0 ms 204 KB Output is correct
78 Correct 12 ms 728 KB Output is correct