#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 2000
#define M (1 << 20)
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int ww[N], *ei[N], eo[N], n; char banned[N];
void append(int d, int i) {
int o = eo[d]++;
if (o >= 2 && (o & o - 1) == 0)
ei[d] = (int *) realloc(ei[d], o * 2 * sizeof *ei[d]);
ei[d][o] = i;
}
int cc[M];
long long ss[M];
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_remove_first() {
int i = iq[1], j = iq[cnt--];
if (j != i)
pq[j] = 1, pq_dn(j);
pq[i] = 0;
return i;
}
void simulate(char *used, int *c__, long long *s__) {
int i, d, o, c_;
long long s_;
memset(pq, 0, n * sizeof *pq), cnt = 0;
memset(used, 0, n * sizeof *used);
c_ = s_ = 0;
for (d = n - 1; d >= 0; d--) {
for (o = eo[d]; o--; ) {
i = ei[d][o];
if (!banned[i])
pq_add(i);
}
if (cnt) {
i = pq_remove_first();
c_++, s_ += ww[i], used[i] = 1;
}
}
*c__ = c_, *s__ = s_;
}
int main() {
static int hh[M];
static char used[N], used_[N];
int m, k, h, i, d, b, o, c, c_, c2;
long long s, s_, s2;
scanf("%d%d", &n, &k);
for (d = 0; d < n; d++)
ei[d] = (int *) malloc(2 * sizeof *ei[d]);
for (i = 0; i < n; i++) {
scanf("%d%d", &ww[i], &d), d--;
append(d, i);
}
if (n <= 20) {
m = 0;
for (b = 0; b < 1 << n; b++) {
c = s = 0;
for (d = 0; d < n; d++)
for (o = eo[d]; o--; ) {
i = ei[d][o];
if ((b & 1 << i) != 0) {
c++, s += ww[i];
if (c > d + 1) {
c = -1;
goto out;
}
}
}
out:
if (c != -1)
cc[m] = c, ss[m] = s, m++;
}
for (h = 0; h < m; h++)
hh[h] = h;
sort(hh, 0, m);
for (h = 0; h < k; h++)
printf("%d %lld\n", cc[hh[h]], ss[hh[h]]);
} else {
simulate(used, &c_, &s_);
printf("%d %lld\n", c_, s_);
if (k >= 2) {
c2 = s2 = 0;
for (i = 0; i < n; i++)
if (used[i]) {
banned[i] = 1, simulate(used_, &c_, &s_), banned[i] = 0;
if (c2 < c_ || c2 == c_ && s2 > s_)
c2 = c_, s2 = s_;
}
printf("%d %lld\n", c2, s2);
}
}
return 0;
}
Compilation message
Main.c: In function 'append':
Main.c:19:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
19 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
Main.c: In function 'sort':
Main.c:34:53: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
34 | else if (cc[hh[j]] > cc[h] || cc[hh[j]] == cc[h] && ss[hh[j]] < ss[h]) {
| ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
Main.c: In function 'main':
Main.c:149:30: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
149 | if (c2 < c_ || c2 == c_ && s2 > s_)
| ~~~~~~~~~^~~~~~~~~~
Main.c:110:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
110 | scanf("%d%d", &n, &k);
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:114:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
114 | scanf("%d%d", &ww[i], &d), d--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 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 |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
247 ms |
380 KB |
Output is correct |
2 |
Correct |
228 ms |
380 KB |
Output is correct |
3 |
Correct |
195 ms |
380 KB |
Output is correct |
4 |
Correct |
215 ms |
332 KB |
Output is correct |
5 |
Correct |
232 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
3 ms |
332 KB |
Output is correct |
10 |
Correct |
99 ms |
388 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
145 ms |
8492 KB |
Output is correct |
2 |
Correct |
281 ms |
14836 KB |
Output is correct |
3 |
Correct |
63 ms |
4300 KB |
Output is correct |
4 |
Correct |
32 ms |
2160 KB |
Output is correct |
5 |
Correct |
273 ms |
12528 KB |
Output is correct |
6 |
Correct |
2 ms |
204 KB |
Output is correct |
7 |
Correct |
26 ms |
588 KB |
Output is correct |
8 |
Correct |
4 ms |
332 KB |
Output is correct |
9 |
Correct |
4 ms |
332 KB |
Output is correct |
10 |
Correct |
3 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
54 ms |
3640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 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 |
256 KB |
Output is correct |
27 |
Correct |
247 ms |
380 KB |
Output is correct |
28 |
Correct |
228 ms |
380 KB |
Output is correct |
29 |
Correct |
195 ms |
380 KB |
Output is correct |
30 |
Correct |
215 ms |
332 KB |
Output is correct |
31 |
Correct |
232 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
3 ms |
332 KB |
Output is correct |
36 |
Correct |
99 ms |
388 KB |
Output is correct |
37 |
Correct |
0 ms |
204 KB |
Output is correct |
38 |
Correct |
0 ms |
204 KB |
Output is correct |
39 |
Correct |
0 ms |
204 KB |
Output is correct |
40 |
Correct |
145 ms |
8492 KB |
Output is correct |
41 |
Correct |
281 ms |
14836 KB |
Output is correct |
42 |
Correct |
63 ms |
4300 KB |
Output is correct |
43 |
Correct |
32 ms |
2160 KB |
Output is correct |
44 |
Correct |
273 ms |
12528 KB |
Output is correct |
45 |
Correct |
2 ms |
204 KB |
Output is correct |
46 |
Correct |
26 ms |
588 KB |
Output is correct |
47 |
Correct |
4 ms |
332 KB |
Output is correct |
48 |
Correct |
4 ms |
332 KB |
Output is correct |
49 |
Correct |
3 ms |
204 KB |
Output is correct |
50 |
Correct |
0 ms |
204 KB |
Output is correct |
51 |
Correct |
0 ms |
204 KB |
Output is correct |
52 |
Correct |
54 ms |
3640 KB |
Output is correct |
53 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
54 |
Halted |
0 ms |
0 KB |
- |