Submission #1092694

#TimeUsernameProblemLanguageResultExecution timeMemory
1092694ortsacLet's Win the Election (JOI22_ho_t3)C++17
100 / 100
822 ms2644 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define db double int INF = 0x3f3f3f3f3f3f3f3f; struct Val { int a, b, idx; Val(int x = 0, int y = 0, int z = 0) : a(x), b(y), idx(z) {} }; bool comp1(const Val& a, const Val& b) { return (make_pair(a.b, -a.a) < make_pair(b.b, -b.a)); } bool comp2(const Val& a, const Val& b) { return (make_pair(a.a, a.idx) < make_pair(b.a, b.idx)); } const int MAXN = 505; db dp[MAXN][MAXN]; int32_t main() { int n, k; cin >> n >> k; vector<Val> v(n); for (int i = 0; i < n; i++) { cin >> v[i].a >> v[i].b; if (v[i].b == -1) v[i].b = INF; v[i].idx = (i + 1); } sort(v.begin(), v.end(), comp1); v.insert(v.begin(), 0); cout << fixed << setprecision(2); db ans = INF; for (int p = 0; p <= k; p++) { for (int i = 0; i <= k; i++) { for (int j = 0; j <= k; j++) dp[i][j] = INF; } dp[0][0] = 0; for (int i = 1; i <= k; i++) { for (int j = 0; j <= i; j++) { // case 1, we get i to be a companion if (j > 0) { db case1 = dp[i - 1][j - 1] + ((db)v[i].b)/((db)j); dp[i][j] = min(dp[i][j], case1); } db case2 = dp[i - 1][j] + ((db)v[i].a)/((db)(p + 1)); dp[i][j] = min(dp[i][j], case2); } } int need = (k - p); for (int i = 0; i <= need; i++) { int sumI = 0; vector<int> rem; for (int j = (p + i + 1); j <= n; j++) { rem.push_back(v[j].a); } sort(rem.begin(), rem.end()); for (int j = 0; j < (need - i); j++) { sumI += rem[j]; } db sum = ((db)sumI)/((db)p+1); db curr = (sum + dp[p + i][p]); if (i != need) { sum -= rem.back(); rem.pop_back(); } if (curr < ans) { //cout << sum << " " << i << " " << p << " " << dp[p + 1][p] << "\n"; } ans = min(ans, curr); } } cout << ans << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...