#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double lfloat;
const int MAXN = 500;
const lfloat INF = 500000;
int a[MAXN], b[MAXN];
pair<int, int> input[MAXN];
lfloat dp[MAXN+1][MAXN+1][MAXN+1];
bool cmp(pair<int, int> i, pair<int, int> j) {
if (i.second == j.second) return i.first < j.first;
if (min(i.second, j.second) == -1) {
return (i.second == -1) < (j.second == -1);
}
return i.second < j.second;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
int n, K; cin >> n >> K;
bool sub0 = true;
for (int i=0; i<n; i++) cin >> input[i].first >> input[i].second;
sort(input, input+n, cmp);
int invalid = 0;
for (int i=0; i<n; i++) {
a[i] = input[i].first; b[i] = input[i].second;
invalid += (b[i] == -1);
// cout << a[i] << ' ' << b[i] << endl;
if (b[i] != -1 and b[i] != a[i]) sub0 = false;
}
lfloat ans = INF;
if (sub0) {
ans = 0; lfloat cnt = 1;
for (int i=1; i<=K; i++) {
// cout << a[i-1] << ' ' << b[i-1] << endl;
ans += a[i-1] / cnt;
if (b[i-1] != -1) cnt++;
}
} else {
for (int c=(sub0 ? (K - invalid) : 0); c<=min(K, n - invalid); c++) {
for (int k=0; k<=K; k++) {
for (int j=0; j<=K-c; j++) dp[0][k][j] = INF;
}
dp[0][0][0] = 0;
for (int i=1; i<=n; i++) {
// if (c == 1) cout << "i=" << i << endl;
for (int k=0; k<=c; k++) {
if (K == n) {
int j = i - k;
dp[i][k][j] = min(
((k == 0 or b[i-1] == -1) ? INF : (dp[i-1][k-1][j] + (lfloat)b[i-1] / (lfloat)k)),
(j == 0 ? INF : (dp[i-1][k][j-1] + (lfloat)a[i-1] / (1 + (lfloat)c)))
);
continue;
}
for (int j=0; j<=K-c; j++) {
dp[i][k][j] = min({
dp[i-1][k][j],
((k == 0 or b[i-1] == -1) ? INF : (dp[i-1][k-1][j] + (lfloat)b[i-1] / (lfloat)k)),
(j == 0 ? INF : (dp[i-1][k][j-1] + (lfloat)a[i-1] / (1 + (lfloat)c)))
});
// if (c == 1) printf("%.5Lf ", dp[i][k][j]);
}
// if (c == 1) printf("\n");
}
// if (c == 1) printf("\n\n");
}
ans = min(ans, dp[n][c][K-c]);
}
}
printf("%.5Lf", ans);
return 0;
}
# | 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... |