This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
constexpr int maxn = 501, inf = 1e9 + 7;
constexpr double eps = 1e-4;
double dp[maxn][maxn][maxn];
double ans_vl[maxn];
double get_ans(int n, int k, vector<double> &a, vector<pair<double, int>> &s_b, int cnt_all) {
if (ans_vl[cnt_all] >= 0.5) {
return ans_vl[cnt_all];
}
for (int i = 0; i <= n; ++i) {
for (int cnt_nw = 0; cnt_nw <= i && cnt_nw <= cnt_all; ++cnt_nw) {
for (int cnt_used = 0; cnt_used <= k; ++cnt_used) {
dp[i][cnt_nw][cnt_used] = inf;
}
}
}
dp[0][0][0] = 0;
double ans = inf;
for (int i = 0; i < n; ++i) {
for (int cnt_nw = 0; cnt_nw <= i && cnt_nw <= cnt_all; ++cnt_nw) {
for (int cnt_used = cnt_nw; cnt_used < k; ++cnt_used) {
if (s_b[i].first < 10000 && cnt_nw < cnt_all) {
dp[i + 1][cnt_nw + 1][cnt_used + 1] = min(dp[i + 1][cnt_nw + 1][cnt_used + 1], dp[i][cnt_nw][cnt_used] + s_b[i].first / (cnt_nw + 1));
if (cnt_used + 1 == k && cnt_nw + 1 == cnt_all) {
ans = min(ans, dp[i + 1][cnt_nw + 1][cnt_used + 1]);
}
}
dp[i + 1][cnt_nw][cnt_used + 1] = min(dp[i + 1][cnt_nw][cnt_used + 1], dp[i][cnt_nw][cnt_used] + a[s_b[i].second] / (cnt_all + 1));
if (cnt_used + 1 == k && cnt_nw == cnt_all) {
ans = min(ans, dp[i + 1][cnt_nw][cnt_used + 1]);
}
dp[i + 1][cnt_nw][cnt_used] = min(dp[i + 1][cnt_nw][cnt_used], dp[i][cnt_nw][cnt_used]);
}
}
}
ans_vl[cnt_all] = ans;
return ans;
}
void solve() {
int n, k;
cin >> n >> k;
vector<double> a(n), b(n);
vector<pair<double, int>> s_b;
for (int i = 0; i < n; ++i) {
cin >> a[i] >> b[i];
if (b[i] > 0) {
s_b.emplace_back(b[i], i);
} else {
s_b.emplace_back(inf, i);
}
}
sort(s_b.begin(), s_b.end());
double ans = get_ans(n, k, a, s_b, 0);
int l = 0, r = k;
while (l < r - 3) {
int m1 = l + (r - l) / 3;
int m2 = r - (r - l) / 3;
double ans1 = get_ans(n, k, a, s_b, m1);
double ans2 = get_ans(n, k, a, s_b, m2);
ans = min(ans, min(ans1, ans2));
if (ans1 >= ans2) {
l = m1;
} else {
r = m2;
}
}
for (int i = l + 1; i < r; ++i) {
ans = min(ans, get_ans(n, k, a, s_b, i));
}
cout << ans << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.precision(25);
cout << fixed;
int t = 1;
// cin >> t;
while (t--) {
solve();
}
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... |