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 "bits/stdc++.h"
#include <functional>
#include <iomanip>
#include <vector>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, k;
cin >> n >> k;
vector<pii> pl(n + 5);
for (int i = 1; i <= n; i++) {
cin >> pl[i].first >> pl[i].second;
if (pl[i].second == -1) pl[i].second = 1e9;
}
sort(pl.begin() + 1, pl.begin() + n + 1, [](pii& u, pii& v) {
if (u.second == v.second) return u.first < v.first;
else return u.second < v.second;
});
// suffix[i][j] = min time cost to get j region in [i, n]
vector<vector<ll>> suffix(n + 5, vector<ll>(k + 5, 1e9));
for (int i = n; i >= 1; i--) {
for (int j = 0; j <= k; j++) {
if (j > n - i + 1) break; // j > no. of region in [i, n]
if (i == n) {
if (j == 0) suffix[i][j] = 0;
else if (j == 1) suffix[i][j] = pl[i].first;
}
else {
suffix[i][j] = suffix[i + 1][j];
if (j > 0) suffix[i][j] = min(suffix[i][j], suffix[i + 1][j - 1] + pl[i].first);
}
}
}
double ans = 1e16;
for (int coop = 0; coop <= k; coop++) {
// cout << "###" << coop << endl;
// assume will get coop coop-er at the end
// dp[i][j] = in [1, i], the actual min cost to get j coop-er and (i - j) no-coop region
vector<vector<double>> dp(n + 5, vector<double>(k + 5, 1e16));
for (int i = 0; i <= k; i++) {
for (int j = 0; j <= min(coop, i); j++) {
if (i == 0) {
// miss out the case that no coop-er is needed.
dp[i][j] = 0;
}
else if (i == 1) {
if (j == 0) dp[i][j] = pl[i].first / (coop + 1.0);
if (j == 1 && pl[i].second != 1e9) dp[i][j] = pl[i].second;
}
else {
// not choose i as a coop-er
dp[i][j] = min(dp[i][j], dp[i - 1][j] + pl[i].first / (coop + 1.0));
// choosing i as a coop-er
if (j > 0 && pl[i].second != 1e9) {
dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + pl[i].second / (j - 1 + 1.0));
}
}
// cout << i << " " << j << " " << dp[i][j] << endl;
if (j == coop) {
// cout << "@" << i << " " << j << " " << dp[i][j] << " $ " << k - i << " " << suffix[i + 1][k - i] << " " << dp[i][j] + suffix[i + 1][k - i] / (coop + 1.0) << "\n";
if (i < k) {
ans = min(ans, dp[i][j] + suffix[i + 1][k - i] / (coop + 1.0));
}
else {
ans = min(ans, dp[i][j]);
}
}
}
}
}
cout << fixed << setprecision(9) << ans << "\n";
}
# | 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... |