답안 #503387

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
503387 2022-01-07T19:12:55 Z rainboy Akcija (COCI21_akcija) C
90 / 110
5000 ms 9924 KB
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

unsigned int X = 12345;

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

int ww[N], *ei[N], eo[N], n; int tt[N];

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

	if (o >= 2 && (o & o - 1) == 0)
		ei[t] = (int *) realloc(ei[t], o * 2 * sizeof *ei[t]);
	ei[t][o] = i;
}

int cc[K], k_, k; long long ss[K];

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)
			if (cc[hh[j]] == cc[h] && ss[hh[j]] == ss[h])
				j++;
			else if (cc[hh[j]] > cc[h] || cc[hh[j]] == cc[h] && ss[hh[j]] < ss[h]) {
				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 iq[1 + N], pq[N], cnt;

int lt(int i, int j) { return ww[i] < ww[j]; }

int p2(int p) {
	return (p *= 2) > cnt ? 0 : (p < cnt && lt(iq[p + 1], iq[p]) ? p + 1 : p);
}

void pq_up(int i) {
	int p, q, j;

	for (p = pq[i]; (q = p / 2) && lt(i, j = iq[q]); p = q)
		iq[pq[j] = p] = j;
	iq[pq[i] = p] = i;
}

void pq_dn(int i) {
	int p, q, j;

	for (p = pq[i]; (q = p2(p)) && lt(j = iq[q], i); p = q)
		iq[pq[j] = p] = j;
	iq[pq[i] = p] = i;
}

void pq_add(int i) {
	pq[i] = ++cnt, pq_up(i);
}

int pq_first() { return iq[1]; }

int pq_remove_first() {
	int i = iq[1], j = iq[cnt--];

	if (j != i)
		pq[j] = 1, pq_dn(j);
	pq[i] = 0;
	return i;
}

void greedy(char *used, int *dd, int *c__, long long *s__) {
	static int ii[N], jj[N];
	int i, t, o, c_;
	long long s_;

	memset(pq, 0, n * sizeof *pq), cnt = 0;
	memset(used, 0, n * sizeof *used);
	c_ = s_ = 0;
	for (t = n - 1; t >= 0; t--) {
		for (o = eo[t]; o--; ) {
			i = ei[t][o];
			if (tt[i] != -1)
				pq_add(i);
		}
		if (cnt) {
			i = pq_remove_first();
			ii[t] = i, jj[t] = cnt == 0 ? -1 : pq_first();
			c_++, s_ += ww[i], used[i] = 1;
		} else
			ii[t] = -1, jj[t] = -1;
	}
	for (t = 0; t < n; t++)
		if (ii[t] != -1)
			dd[ii[t]] = (jj[t] != -1 && used[jj[t]] ? dd[jj[t]] : 0)
				+ (jj[t] == -1 ? 0 : ww[jj[t]]) - ww[ii[t]];
	*c__ = c_, *s__ = s_;
}

char used[N][N]; int dd[N][N], n_;

int branch(int n, int c_, long long s_) {
	int c, i;
	long long s;

	greedy(used[n_], dd[n_], &c, &s), n_++;
	if (c < c_ || c == c_ && s > s_) {
		n_--;
		assert(n_ == 0);
		return 0;
	}
	cc[k_] = c, ss[k_] = s;
	if (++k_ == k) {
		n_--;
		return 1;
	}
	for (i = 0; i < n; i++)
		if (used[n_ - 1][i] && tt[i] == 0) {
			int c2 = dd[n_ - 1][i] < 0 ? c - 1 : c;
			long long s2 = s + dd[n_ - 1][i];

			if (c2 > c_ || c2 == c_ && s2 <= s_) {
				int done;

				tt[i] = -1, done = branch(n, c_, s_), tt[i] = n_;
				if (done) {
					for (i = n - 1; i >= 0; i--)
						if (tt[i] == n_)
							tt[i] = 0;
					n_--;
					return 1;
				}
			}
		}
	for (i = n - 1; i >= 0; i--)
		if (tt[i] == n_)
			tt[i] = 0;
	n_--;
	return 0;
}

int main() {
	static int hh[K];
	int h, i, t, c;
	long long lower, upper, cs, s;

	scanf("%d%d", &n, &k);
	for (t = 0; t < n; t++)
		ei[t] = (int *) malloc(2 * sizeof *ei[t]);
	for (i = 0; i < n; i++) {
		scanf("%d%d", &ww[i], &t), t--;
		append(t, i);
	}
	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, c, s))
			upper = cs;
		else
			lower = cs;
	}
	if (lower >= 0) {
		cs = lower, c = n - cs / S, s = cs % S;
		k_ = 0, branch(n, 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;
	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 '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 'sort':
Main.c:35:53: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   35 |    else if (cc[hh[j]] > cc[h] || cc[hh[j]] == cc[h] && ss[hh[j]] < ss[h]) {
      |                                  ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
Main.c: In function 'branch':
Main.c:121:24: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  121 |  if (c < c_ || c == c_ && s > s_) {
      |                ~~~~~~~~^~~~~~~~~
Main.c:136:28: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
  136 |    if (c2 > c_ || c2 == c_ && s2 <= s_) {
      |                   ~~~~~~~~~^~~~~~~~~~~
Main.c: In function 'main':
Main.c:161:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  161 |  scanf("%d%d", &n, &k);
      |  ^~~~~~~~~~~~~~~~~~~~~
Main.c:165:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  165 |   scanf("%d%d", &ww[i], &t), t--;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 332 KB Output is correct
2 Correct 5 ms 332 KB Output is correct
3 Correct 4 ms 332 KB Output is correct
4 Correct 4 ms 332 KB Output is correct
5 Correct 4 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 3 ms 444 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 332 KB Output is correct
2 Correct 5 ms 332 KB Output is correct
3 Correct 4 ms 332 KB Output is correct
4 Correct 4 ms 332 KB Output is correct
5 Correct 4 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 3 ms 444 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 9 ms 428 KB Output is correct
15 Correct 8 ms 332 KB Output is correct
16 Correct 8 ms 332 KB Output is correct
17 Correct 10 ms 332 KB Output is correct
18 Correct 8 ms 332 KB Output is correct
19 Correct 3 ms 332 KB Output is correct
20 Correct 3 ms 332 KB Output is correct
21 Correct 3 ms 332 KB Output is correct
22 Correct 4 ms 332 KB Output is correct
23 Correct 7 ms 440 KB Output is correct
24 Correct 0 ms 332 KB Output is correct
25 Correct 0 ms 332 KB Output is correct
26 Correct 0 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 332 KB Output is correct
2 Correct 12 ms 460 KB Output is correct
3 Correct 13 ms 448 KB Output is correct
4 Correct 11 ms 448 KB Output is correct
5 Correct 13 ms 452 KB Output is correct
6 Correct 4 ms 332 KB Output is correct
7 Correct 5 ms 460 KB Output is correct
8 Correct 5 ms 460 KB Output is correct
9 Correct 5 ms 460 KB Output is correct
10 Correct 10 ms 460 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 412 KB Output is correct
2 Correct 48 ms 392 KB Output is correct
3 Correct 54 ms 396 KB Output is correct
4 Correct 43 ms 384 KB Output is correct
5 Correct 60 ms 388 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 41 ms 440 KB Output is correct
8 Correct 33 ms 432 KB Output is correct
9 Correct 37 ms 448 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 41 ms 404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 640 KB Output is correct
2 Correct 14 ms 656 KB Output is correct
3 Correct 16 ms 628 KB Output is correct
4 Correct 14 ms 620 KB Output is correct
5 Correct 17 ms 588 KB Output is correct
6 Correct 6 ms 844 KB Output is correct
7 Correct 9 ms 844 KB Output is correct
8 Correct 9 ms 844 KB Output is correct
9 Correct 12 ms 844 KB Output is correct
10 Correct 17 ms 644 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 7 ms 488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 332 KB Output is correct
2 Correct 5 ms 332 KB Output is correct
3 Correct 4 ms 332 KB Output is correct
4 Correct 4 ms 332 KB Output is correct
5 Correct 4 ms 332 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 2 ms 332 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 3 ms 444 KB Output is correct
11 Correct 1 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 9 ms 428 KB Output is correct
15 Correct 8 ms 332 KB Output is correct
16 Correct 8 ms 332 KB Output is correct
17 Correct 10 ms 332 KB Output is correct
18 Correct 8 ms 332 KB Output is correct
19 Correct 3 ms 332 KB Output is correct
20 Correct 3 ms 332 KB Output is correct
21 Correct 3 ms 332 KB Output is correct
22 Correct 4 ms 332 KB Output is correct
23 Correct 7 ms 440 KB Output is correct
24 Correct 0 ms 332 KB Output is correct
25 Correct 0 ms 332 KB Output is correct
26 Correct 0 ms 332 KB Output is correct
27 Correct 13 ms 332 KB Output is correct
28 Correct 12 ms 460 KB Output is correct
29 Correct 13 ms 448 KB Output is correct
30 Correct 11 ms 448 KB Output is correct
31 Correct 13 ms 452 KB Output is correct
32 Correct 4 ms 332 KB Output is correct
33 Correct 5 ms 460 KB Output is correct
34 Correct 5 ms 460 KB Output is correct
35 Correct 5 ms 460 KB Output is correct
36 Correct 10 ms 460 KB Output is correct
37 Correct 0 ms 332 KB Output is correct
38 Correct 0 ms 332 KB Output is correct
39 Correct 0 ms 332 KB Output is correct
40 Correct 52 ms 412 KB Output is correct
41 Correct 48 ms 392 KB Output is correct
42 Correct 54 ms 396 KB Output is correct
43 Correct 43 ms 384 KB Output is correct
44 Correct 60 ms 388 KB Output is correct
45 Correct 1 ms 332 KB Output is correct
46 Correct 41 ms 440 KB Output is correct
47 Correct 33 ms 432 KB Output is correct
48 Correct 37 ms 448 KB Output is correct
49 Correct 2 ms 332 KB Output is correct
50 Correct 0 ms 332 KB Output is correct
51 Correct 0 ms 332 KB Output is correct
52 Correct 41 ms 404 KB Output is correct
53 Correct 17 ms 640 KB Output is correct
54 Correct 14 ms 656 KB Output is correct
55 Correct 16 ms 628 KB Output is correct
56 Correct 14 ms 620 KB Output is correct
57 Correct 17 ms 588 KB Output is correct
58 Correct 6 ms 844 KB Output is correct
59 Correct 9 ms 844 KB Output is correct
60 Correct 9 ms 844 KB Output is correct
61 Correct 12 ms 844 KB Output is correct
62 Correct 17 ms 644 KB Output is correct
63 Correct 0 ms 332 KB Output is correct
64 Correct 1 ms 332 KB Output is correct
65 Correct 7 ms 488 KB Output is correct
66 Execution timed out 5013 ms 9924 KB Time limit exceeded
67 Halted 0 ms 0 KB -