#include <stdio.h>
#define N 2000
#define M (1 << 20)
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
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 main() {
static int ww[N], qu[N][N], cnt[N], hh[M];
static char available[N];
int n, m, k, h, i, d, b, c, c_;
long long s, s_;
scanf("%d%d", &n, &k);
for (i = 0; i < n; i++) {
scanf("%d%d", &ww[i], &d), d--;
qu[d][cnt[d]++] = i;
}
if (n <= 20) {
m = 0;
for (b = 0; b < 1 << n; b++) {
c = s = 0;
for (d = 0; d < n; d++)
for (h = 0; h < cnt[d]; h++)
if ((b & 1 << qu[d][h]) != 0) {
c++, s += ww[qu[d][h]];
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 {
c_ = 0, s_ = 0;
for (d = n - 1, c = 0; d >= 0; d--) {
for (h = 0; h < cnt[d]; h++)
available[qu[d][h]] = 1, c++;
if (c > 0) {
int i_;
i_ = -1;
for (i = 0; i < n; i++)
if (available[i] && (i_ == -1 || ww[i_] > ww[i]))
i_ = i;
available[i_] = 0, c--;
c_++, s_ += ww[i_];
}
}
printf("%d %lld\n", c_, s_);
}
return 0;
}
Compilation message
Main.c: In function 'sort':
Main.c:22:53: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
22 | else if (cc[hh[j]] > cc[h] || cc[hh[j]] == cc[h] && ss[hh[j]] < ss[h]) {
| ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
Main.c: In function 'main':
Main.c:40:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | scanf("%d%d", &n, &k);
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:42:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | scanf("%d%d", &ww[i], &d), d--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5068 KB |
Output is correct |
2 |
Correct |
6 ms |
5352 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4972 KB |
Output is correct |
5 |
Correct |
4 ms |
5324 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
4 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5068 KB |
Output is correct |
2 |
Correct |
6 ms |
5352 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4972 KB |
Output is correct |
5 |
Correct |
4 ms |
5324 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
4 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
420 KB |
Output is correct |
14 |
Correct |
5 ms |
5028 KB |
Output is correct |
15 |
Correct |
4 ms |
5324 KB |
Output is correct |
16 |
Correct |
4 ms |
5196 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
6 ms |
5324 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 |
420 KB |
Output is correct |
23 |
Correct |
4 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 |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
4956 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
140 ms |
8536 KB |
Output is correct |
2 |
Correct |
281 ms |
14880 KB |
Output is correct |
3 |
Correct |
61 ms |
4264 KB |
Output is correct |
4 |
Correct |
29 ms |
2208 KB |
Output is correct |
5 |
Correct |
221 ms |
12660 KB |
Output is correct |
6 |
Correct |
2 ms |
204 KB |
Output is correct |
7 |
Correct |
58 ms |
656 KB |
Output is correct |
8 |
Correct |
4 ms |
332 KB |
Output is correct |
9 |
Correct |
4 ms |
332 KB |
Output is correct |
10 |
Correct |
5 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
54 ms |
3688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5068 KB |
Output is correct |
2 |
Correct |
6 ms |
5352 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4972 KB |
Output is correct |
5 |
Correct |
4 ms |
5324 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
4 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
420 KB |
Output is correct |
14 |
Correct |
5 ms |
5028 KB |
Output is correct |
15 |
Correct |
4 ms |
5324 KB |
Output is correct |
16 |
Correct |
4 ms |
5196 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
6 ms |
5324 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 |
420 KB |
Output is correct |
23 |
Correct |
4 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 |
332 KB |
Output is correct |
27 |
Incorrect |
4 ms |
4956 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |