This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#include <string.h>
#define N 500
#define INF 0x3f3f3f3f
double min(double a, double b) { return a < b ? a : b; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int aa[N], bb[N];
void sort(int *ii, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k)
if (bb[ii[j]] == bb[i_])
j++;
else if (bb[ii[j]] < bb[i_]) {
tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
i++, j++;
} else {
k--;
tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
}
sort(ii, l, i);
l = k;
}
}
int main() {
static int ii[N];
static char cc[N], cc_[N];
int n, k, l, l_, h, i, i_, i1, j;
double x, x_, d, d_, d1, ans;
scanf("%d%d", &n, &k);
for (i = 0; i < n; i++) {
scanf("%d%d", &aa[i], &bb[i]);
if (bb[i] == -1)
bb[i] = INF;
}
for (i = 0; i < n; i++)
ii[i] = i;
sort(ii, 0, n);
ans = INF;
x = 0;
for (l = 0; l <= k && (l == 0 || bb[ii[l - 1]] != INF); l++) {
if (l > 0)
cc[l - 1] = 1, x += (double) bb[ii[l - 1]] / l;
memcpy(cc_, cc, n * sizeof *cc);
x_ = x;
for (h = l; h < k; h++) {
for (j = 0; j < n; j++)
if (cc_[j] == 0)
break;
d = (double) bb[ii[j]] / l, d1 = INF, i1 = -1;
for (i = n - 1, l_ = l; i >= 0; i--) {
i_ = ii[i];
if (cc_[i] == 0) {
if (d1 > (d_ = (double) aa[i_] / (l + 1)))
d1 = d_, i1 = i;
} else if (cc_[i] == 1) {
if (d1 > (d_ = (double) aa[i_] / (l + 1) - (double) bb[i_] / l_ + d))
d1 = d_, i1 = i;
if (l_ > 1)
d += (double) bb[i_] / (l_ - 1) - (double) bb[i_] / l_, l_--;
}
}
x_ += d1;
if (cc_[i1] == 0)
cc_[i1] = -1;
else
cc_[i1] = -1, cc_[j] = 1;
}
ans = min(ans, x_);
}
printf("%f\n", ans);
return 0;
}
Compilation message (stderr)
Main.c: In function 'main':
Main.c:42:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | scanf("%d%d", &n, &k);
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:44:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | scanf("%d%d", &aa[i], &bb[i]);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |