Submission #871126

#TimeUsernameProblemLanguageResultExecution timeMemory
871126HorizonWestLet's Win the Election (JOI22_ho_t3)C++17
0 / 100
2578 ms1002724 KiB
#include <bits/stdc++.h> using namespace std; #define endl '\n' #define db double #define ll __int128 //#define int long long #define pb push_back #define fs first #define sd second #define Mod long(1e9 + 7) #define all(x) x.begin(), x.end() #define unvisited long(-1) #define Eps double(1e-9) #define _for(i, n) for(int i = 0; i < (n); i++) #define dbg(x) cout << #x ": " << x << endl; const int Max = 1e6 + 7, Inf = 1e9 + 7; void print(bool x) { cout << (x ? "YES" : "NO") << endl; } string tostring (__int128 x) { string ans = ""; while(x > 0) { ans += (x % 10 + '0'); x /= 10; } reverse(all(ans)); return ans; } void printdb(double ans, int k) { cout << setiosflags(ios::fixed) << setiosflags(ios::showpoint) << setprecision(k) << ans << endl; } int n, m; vector <pair<db, db>> v; double f(db l) { vector <vector<vector<db>>> dp(n + 2, vector <vector<db>> ( m + 2, vector <db> (m + 2, Inf) )); dp[0][1][0] = 0.000; db ans = Inf; for(db i = 0; i < n; i++) { for(db j = 1; j <= l; j++) { for(db k = 0; k <= m; k++) { dp[i+1][j][k] = min(dp[i+1][j][k], dp[i][j][k]); dp[i+1][j][k+1] = min(dp[i+1][j][k+1], dp[i][j][k] + v[i].sd / l); if(v[i].fs != Inf) { dp[i+1][j+1][k+1] = min(dp[i+1][j+1][k+1], dp[i][j][k] + v[i].fs / j); } } } ans = min(ans, dp[i+1][l][m]); } return ans; } double ternary_search(double l, double r) { double eps = 1e-2, ans = Inf; //set the error limit here while (r - l > eps) { double m1 = l + (r - l) / 3; double m2 = r - (r - l) / 3; double f1 = f((long) m1); //evaluates the function at m1 double f2 = f((long) m2); //evaluates the function at m2 //cout << l << " " << r << " " << m1 << " " << (int) f1 << " " << m2 << " " << (int) f2 << endl; ans = min(ans, min(f1, f2)); if (f1 > f2) l = m1; else r = m2; } // printdb(ans, 6); return ans; //return the min of f(x) in [l, r] } void solve() { cin >> n >> m; v.assign(n, { 0, 0 }); for(auto& u : v) { cin >> u.sd >> u.fs; if(u.fs == -1) u.fs = Inf; } sort(all(v)); //printdb(f(2.0), 5); printdb(ternary_search(1, m), 5); } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int Q = 1; //cin >> Q; while (Q--) { solve(); } return 0; }
#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...