#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;
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_remove_first() {
int i = iq[1], j = iq[cnt--];
if (j != i)
pq[j] = 1, pq_dn(j);
pq[i] = 0;
return i;
}
int c1; long long s1;
void greedy(char *used) {
int i, t, o;
memset(used, 0, n * sizeof *used);
cnt = 0, c1 = s1 = 0;
for (t = n - 1; t >= 0; t--) {
for (o = eo[t]; o--; ) {
i = ei[t][o];
pq_add(i);
}
if (cnt) {
i = pq_remove_first();
c1++, s1 += ww[i], used[i] = 1;
}
}
}
void get_subs(char *used, char *banned, int *sub) {
static int kk[N];
int i, i_, c, s, t, o;
memset(kk, 0, n * sizeof *kk);
c = 0;
for (t = 0; t < n; t++)
for (o = eo[t]; o--; ) {
i = ei[t][o];
if (used[i])
kk[t]++, kk[c++]--;
}
for (t = n - 2; t >= 0; t--)
kk[t] += kk[t + 1];
for (s = n - 1, t = n - 1, i_ = -1; s >= 0; s--) {
while (t >= s || kk[t + 1] > 0) {
for (o = eo[t]; o--; ) {
i = ei[t][o];
if (!banned[i] && !used[i] && (i_ == -1 || ww[i_] > ww[i]))
i_ = i;
}
t--;
}
for (o = eo[s]; o--; ) {
i = ei[s][o];
if (used[i])
sub[i] = i_;
}
}
}
char used[N + 1][N], banned[N + 1][N]; int sub[N + 1][N];
int branch(int n, int h, int c, long long s, int c_, long long s_) {
int i;
if (c < c_ || c == c_ && s > s_)
return 0;
get_subs(used[h], banned[h], sub[h]);
cc[k_] = c, ss[k_] = s, k_++;
if (k_ == k)
return 1;
memcpy(used[h + 1], used[h], n * sizeof *used[h]);
memcpy(banned[h + 1], banned[h], n * sizeof *banned[h]);
for (i = 0; i < n; i++)
if (used[h + 1][i] == 1) {
int j, c1;
long long s1;
used[h + 1][i] = 0, banned[h + 1][i] = 1;
j = sub[h][i];
if (j == -1)
c1 = c - 1, s1 = s - ww[i];
else {
c1 = c, s1 = s - ww[i] + ww[j];
used[h + 1][j] = 1;
}
if ((c1 > c_ || c1 == c_ && s1 <= s_) && branch(n, h + 1, c1, s1, c_, s_))
return 1;
used[h + 1][i] = -1, banned[h + 1][i] = 0;
if (j != -1)
used[h + 1][j] = 0;
}
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);
}
greedy(used[0]);
lower = -1, upper = (n + 2) * S;
while (upper - lower > 1) {
cs = (lower + upper) / 2, c = n - cs / S, s = cs % S;
k_ = 0;
if ((c1 > c || c1 == c && s1 <= s) && branch(n, 0, c1, s1, c, s))
upper = cs;
else
lower = cs;
}
if (lower >= 0) {
cs = lower, c = n - cs / S, s = cs % S;
if (c1 > c || c1 == c && s1 <= s)
k_ = 0, branch(n, 0, c1, s1, 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:20:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
20 | 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 'branch':
Main.c:138:24: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
138 | if (c < c_ || c == c_ && s > s_)
| ~~~~~~~~^~~~~~~~~
Main.c:159:29: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
159 | if ((c1 > c_ || c1 == c_ && s1 <= s_) && branch(n, h + 1, c1, s1, c_, s_))
| ~~~~~~~~~^~~~~~~~~~~
Main.c: In function 'main':
Main.c:185:26: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
185 | if ((c1 > c || c1 == c && s1 <= s) && branch(n, 0, c1, s1, c, s))
| ~~~~~~~~^~~~~~~~~~
Main.c:192:25: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
192 | if (c1 > c || c1 == c && s1 <= s)
| ~~~~~~~~^~~~~~~~~~
Main.c:173:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
173 | scanf("%d%d", &n, &k);
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:177:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
177 | scanf("%d%d", &ww[i], &t), t--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 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 |
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 |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 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 |
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 |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
3 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
17 |
Correct |
2 ms |
332 KB |
Output is correct |
18 |
Correct |
2 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 |
336 KB |
Output is correct |
24 |
Correct |
0 ms |
336 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 |
4 ms |
332 KB |
Output is correct |
2 |
Correct |
4 ms |
408 KB |
Output is correct |
3 |
Correct |
3 ms |
332 KB |
Output is correct |
4 |
Correct |
3 ms |
432 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 |
460 KB |
Output is correct |
9 |
Correct |
2 ms |
332 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 |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
416 KB |
Output is correct |
2 |
Correct |
27 ms |
332 KB |
Output is correct |
3 |
Correct |
28 ms |
332 KB |
Output is correct |
4 |
Correct |
27 ms |
412 KB |
Output is correct |
5 |
Correct |
28 ms |
424 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
35 ms |
460 KB |
Output is correct |
8 |
Correct |
28 ms |
436 KB |
Output is correct |
9 |
Correct |
32 ms |
508 KB |
Output is correct |
10 |
Correct |
1 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 |
25 ms |
432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
716 KB |
Output is correct |
2 |
Correct |
6 ms |
716 KB |
Output is correct |
3 |
Correct |
6 ms |
724 KB |
Output is correct |
4 |
Correct |
6 ms |
588 KB |
Output is correct |
5 |
Correct |
7 ms |
716 KB |
Output is correct |
6 |
Correct |
4 ms |
972 KB |
Output is correct |
7 |
Correct |
5 ms |
1100 KB |
Output is correct |
8 |
Correct |
4 ms |
972 KB |
Output is correct |
9 |
Correct |
5 ms |
972 KB |
Output is correct |
10 |
Correct |
5 ms |
716 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
4 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 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 |
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 |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
3 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
17 |
Correct |
2 ms |
332 KB |
Output is correct |
18 |
Correct |
2 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 |
336 KB |
Output is correct |
24 |
Correct |
0 ms |
336 KB |
Output is correct |
25 |
Correct |
0 ms |
332 KB |
Output is correct |
26 |
Correct |
0 ms |
332 KB |
Output is correct |
27 |
Correct |
4 ms |
332 KB |
Output is correct |
28 |
Correct |
4 ms |
408 KB |
Output is correct |
29 |
Correct |
3 ms |
332 KB |
Output is correct |
30 |
Correct |
3 ms |
432 KB |
Output is correct |
31 |
Correct |
4 ms |
332 KB |
Output is correct |
32 |
Correct |
2 ms |
332 KB |
Output is correct |
33 |
Correct |
2 ms |
332 KB |
Output is correct |
34 |
Correct |
2 ms |
460 KB |
Output is correct |
35 |
Correct |
2 ms |
332 KB |
Output is correct |
36 |
Correct |
2 ms |
332 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 |
28 ms |
416 KB |
Output is correct |
41 |
Correct |
27 ms |
332 KB |
Output is correct |
42 |
Correct |
28 ms |
332 KB |
Output is correct |
43 |
Correct |
27 ms |
412 KB |
Output is correct |
44 |
Correct |
28 ms |
424 KB |
Output is correct |
45 |
Correct |
1 ms |
460 KB |
Output is correct |
46 |
Correct |
35 ms |
460 KB |
Output is correct |
47 |
Correct |
28 ms |
436 KB |
Output is correct |
48 |
Correct |
32 ms |
508 KB |
Output is correct |
49 |
Correct |
1 ms |
460 KB |
Output is correct |
50 |
Correct |
0 ms |
332 KB |
Output is correct |
51 |
Correct |
0 ms |
332 KB |
Output is correct |
52 |
Correct |
25 ms |
432 KB |
Output is correct |
53 |
Correct |
6 ms |
716 KB |
Output is correct |
54 |
Correct |
6 ms |
716 KB |
Output is correct |
55 |
Correct |
6 ms |
724 KB |
Output is correct |
56 |
Correct |
6 ms |
588 KB |
Output is correct |
57 |
Correct |
7 ms |
716 KB |
Output is correct |
58 |
Correct |
4 ms |
972 KB |
Output is correct |
59 |
Correct |
5 ms |
1100 KB |
Output is correct |
60 |
Correct |
4 ms |
972 KB |
Output is correct |
61 |
Correct |
5 ms |
972 KB |
Output is correct |
62 |
Correct |
5 ms |
716 KB |
Output is correct |
63 |
Correct |
0 ms |
332 KB |
Output is correct |
64 |
Correct |
0 ms |
344 KB |
Output is correct |
65 |
Correct |
4 ms |
460 KB |
Output is correct |
66 |
Correct |
3089 ms |
11816 KB |
Output is correct |
67 |
Correct |
3330 ms |
12296 KB |
Output is correct |
68 |
Correct |
3063 ms |
11312 KB |
Output is correct |
69 |
Correct |
2929 ms |
11288 KB |
Output is correct |
70 |
Correct |
3537 ms |
12184 KB |
Output is correct |
71 |
Correct |
1716 ms |
14808 KB |
Output is correct |
72 |
Correct |
1687 ms |
20872 KB |
Output is correct |
73 |
Correct |
1355 ms |
19708 KB |
Output is correct |
74 |
Correct |
1537 ms |
19176 KB |
Output is correct |
75 |
Correct |
1875 ms |
17104 KB |
Output is correct |
76 |
Correct |
1 ms |
332 KB |
Output is correct |
77 |
Correct |
0 ms |
332 KB |
Output is correct |
78 |
Correct |
54 ms |
528 KB |
Output is correct |