#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = double;
const int N = 501;
db dp[N][N][N]; //id, b, a
db solved[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, K;
cin >> n >> K;
vector<int> a(n), b(n), oa(n), ob(n);
int cntb = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i] >> b[i];
if (b[i] != -1) {
cntb += 1;
}
}
iota(ob.begin(), ob.end(), 0);
sort(ob.begin(), ob.end(), [&](int i, int j) {
if (b[i] == -1) return false;
if (b[j] == -1) return true;
return b[i] < b[j];
});
const db inf = 1e12;
auto solve = [&](int mx) -> db {
if (solved[mx] == 0) {
for (int j = 0; j <= mx; ++j) {
for (int k = 0; k <= K - j; ++k) {
dp[0][j][k] = inf;
}
}
dp[0][0][0] = 0;
for (int i = 1; i <= n; ++i) {
//don't take
for (int j = 0; j <= mx; ++j) {
for (int k = 0; k <= min(i, K) - j; ++k) {
dp[i][j][k] = dp[i - 1][j][k];
}
}
//take a
int idx = ob[i - 1];
for (int j = 0; j <= min(i, mx); ++j) {
for (int k = 1; k <= min(i, K) - j; ++k) {
dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j][k - 1] + a[idx] / db(mx + 1));
}
}
if (b[idx] != -1) {
for (int j = 1; j <= min(i, mx); ++j) {
for (int k = 0; k <= min(i, K) - j; ++k) {
dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j - 1][k] + b[idx] / db(j));
}
}
}
}
solved[mx] = dp[n][mx][K - mx];
}
return solved[mx];
};
int r = min(K, cntb);
int x = 0;
for (int k = 10; k > -1; --k) {
db ansL = (x >= (1 << k) ? solve(x - (1 << k)) : inf);
db ans = solve(x);
db ansR = (x + (1 << k) <= r ? solve(x + (1 << k)) : inf);
if (ansL <= ans && ansL <= ansR) {
x -= (1 << k);
} else if (ansR <= ans) {
x += (1 << k);
}
}
cout << fixed << setprecision(3) << solve(x);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
944 ms |
469468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |