# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
503381 |
2022-01-07T18:51:06 Z |
rainboy |
Akcija (COCI21_akcija) |
C |
|
5000 ms |
380 KB |
#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 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[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;
}
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 (tt[i] != -1)
pq_add(i);
}
if (cnt) {
i = pq_remove_first();
c_++, s_ += ww[i], used[i] = 1;
}
}
*c__ = c_, *s__ = s_;
}
char used[N][N]; int n_;
int branch(int n, int c_, long long s_) {
int c, j;
long long s;
simulate(used[n_++], &c, &s);
if (c < c_ || c == c_ && s > s_) {
n_--;
return 0;
}
cc[k_] = c, ss[k_] = s;
if (++k_ == k) {
n_--;
return 1;
}
for (j = 0; j < n; j++)
if (used[n_ - 1][j] && tt[j] == 0) {
int done;
tt[j] = -1, done = branch(n, c_, s_), tt[j] = n_;
if (done) {
for (j = n - 1; j >= 0; j--)
if (tt[j] == n_)
tt[j] = 0;
n_--;
return 1;
}
}
for (j = n - 1; j >= 0; j--)
if (tt[j] == n_)
tt[j] = 0;
n_--;
return 0;
}
int main() {
static int hh[K];
static char used[N], used_[N];
int h, i, d, c_, c2;
long long s_, s2;
scanf("%d%d", &n, &k), k = 4;
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 (k <= 2) {
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]) {
tt[i] = -1, simulate(used_, &c_, &s_), tt[i] = 0;
if (c2 < c_ || c2 == c_ && s2 > s_)
c2 = c_, s2 = s_;
}
printf("%d %lld\n", c2, s2);
}
} else {
long long lower = -1, upper = (n + 2) * S, cs, s;
int c;
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: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:111:24: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
111 | if (c < c_ || c == c_ && s > s_) {
| ~~~~~~~~^~~~~~~~~
Main.c: In function 'main':
Main.c:161:30: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
161 | if (c2 < c_ || c2 == c_ && s2 > s_)
| ~~~~~~~~~^~~~~~~~~~
Main.c:146:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
146 | scanf("%d%d", &n, &k), k = 4;
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:150:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
150 | scanf("%d%d", &ww[i], &d), d--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5015 ms |
380 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
16 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |